pring + hibernate+ C3P0+MYSQLITeye - 牛牛娱乐

pring + hibernate+ C3P0+MYSQLITeye

2019-01-13 17:38:44 | 作者: 轩昂 | 标签: 依据,装备,本书 | 浏览: 2392

1) applicationContext.xml

---------------------

?xml version="1.0" encoding="UTF-8"?
!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"

beans
!-- 引进参数装备文件 --
bean id="propertyConfigurer"
  property name="locations"
  list
  value jdbc.properties /value
  /list
  /property
/bean

!-- 装备数据源  --
bean id="dataSource"
  property name="driverClass" value ${jdbc.driverClass} /value /property
  property name="jdbcUrl" value ${jdbc.url} /value /property
  property name="user" value ${jdbc.user} /value /property
  property name="password" value ${jdbc.password} /value /property
  property name="initialPoolSize" value ${jdbc.initialPoolSize} /value /property
  property name="minPoolSize" value ${jdbc.minPoolSize} /value /property
  property name="maxPoolSize" value ${jdbc.maxPoolSize} /value /property
/bean

!-- spring Hibernate SessionFactory --
bean id="sessionFactory"
  property name="dataSource"
  ref bean="dataSource" /
  /property
  property name="hibernateProperties"
  props
  prop key="hibernate.dialect" org.hibernate.dialect.MySQLDialect /prop
  prop key="hibernate.show_sql" true /prop
  prop key="hibernate.format_sql" true /prop
  prop key="hibernate.jdbc.fetch_size" 80 /prop
  prop key="hibernate.jdbc.batch_size" 35 /prop
  /props
  /property
  property name="mappingResources"
  list  
  value com/spring/demo/bean/mapping/Book.hbm.xml /value  
  /list
  /property
/bean

!-- 业务管理器 --
bean id="transactionManager"
  property name="sessionFactory"
  ref local="sessionFactory" /
  /property
/bean

!-- 业务署理拦截器的装备 --
bean id="baseTransactionProxy" abstract="true"
  property name="transactionManager"
  ref bean="transactionManager" /
  /property
  property name="transactionAttributes"
  props
  prop key="insert*" PROPAGATION_REQUIRED /prop
  prop key="update*" PROPAGATION_REQUIRED /prop
  prop key="delete*" PROPAGATION_REQUIRED /prop
  prop key="*" PROPAGATION_REQUIRED,readOnly /prop
  /props
  /property
/bean

bean id="hibernate_BookDao"
  property name="sessionFactory"
  ref bean="sessionFactory"/
  /property
/bean

bean id="hibernate_BookManager" parent="baseTransactionProxy"
  property name="target"
  bean
  property name="dao"
  ref bean="hibernate_BookDao"/
  /property
  /bean
  /property
/bean

/beans

---------------------

(2) jdbc.properties
## 这个事衔接mysql数据库的详细参数装备,
## 楼主要依据自己的数据来装备,不行直接运用
---------------------

jdbc.driver >
jdbc.url=jdbc:mysql://localhost:3306/springdemo?useUnicode=true characterEncoding=utf-8

jdbc.user=root
jdbc.password=root
jdbc.initialPoolSize=1
jdbc.minPoolSize=1
jdbc.maxPoolSize=10

---------------------

阐明:applicationContext.xml 应用了jdbc.properties文件

,我自己测验,没有问题,并且是spring装备式业务的。

需求JAR包

spring.jar
c3p0.jar
mysql.jar
cglib.jar
asm.jar
antlr.jar
hibernate3.jar
jta.jar
dom4j.jar
commons-collections.jar
commons-logging.jar

-----------

Hibernate_BookDao.java

--------------

package com.spring.demo.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.spring.demo.bean.Book;

/**
*
* @author wangyudong
*
*/
public class Hibernate_BookDao extends HibernateDaoSupport {

/**
  * 增加一本书
  * @param book
  */
public void insertBook(Book book){
  this.getHibernateTemplate().save(book);
}

/**
  * 修正一本书
  * @param book
  */
public void updateBook(Book book){
  this.getHibernateTemplate().update(book);
}

/**
  * 依据ID来删去一本书
  * @param id
  */
public void deleteBook(Integer id){
  Book book = new Book();
  book.setId(id);
  this.getHibernateTemplate().delete(book);
}

/**
  * 依据ID来查询书目标
  * @param id
  * @return
  */
public Book selectBookById(Integer id){
  String hql = "from Book b where b.id=?";
  Object[] params = new Object[]{id};
  List list = this.getHibernateTemplate().find(hql, params );
 
  if(list!=null list.size() 0){
  return (Book)list.get(0);
  }
  return null ;
}

/**
  * 依据书名来查询相关书记载列表
  * @param name
  * @return
  */
public List selectBookByName(String name){
 
  String hql = "from Book b where b.name like ?";
  Object[] params = new Object[]{"%"+name+"%"};
  return this.getHibernateTemplate().find(hql, params);
}

}

--------------

(2) Hibernate_BookManager.java

---------------

package com.spring.demo.manager;

import java.util.List;

import com.spring.demo.bean.Book;
import com.spring.demo.dao.Hibernate_BookDao;

public class Hibernate_BookManager {

private Hibernate_BookDao dao ;

/**
  * 批量增加书本,假如列表长度超越3,就抛出反常
  * @param books
  */
public void insertBooks(List books){
 
  for(int i=0 ; i books.size() ;i++){
  if(i 2){
  throw new RuntimeException("中止业务反常,当列表长度大于3的时分成心抛出,看看业务是否回滚");
  }
  Book book = (Book)books.get(i);
  dao.insertBook(book);
  }
}

/**
  * 增加一本书
  * @param book
  */
public void insertBook(Book book){
  this.dao.insertBook(book);
}

/**
  * 修正一本书
  * @param book
  */
public void updateBook(Book book){
  this.dao.updateBook(book);
}

/**
  * 依据ID来删去一本书
  * @param id
  */
public void deleteBook(Integer id){
  this.dao.deleteBook(id);
}

/**
  * 依据ID来查询书目标
  * @param id
  * @return
  */
public Book selectBookById(Integer id){
  return this.dao.selectBookById(id);
}

/**
  * 依据书名来查询相关书记载列表
  * @param name
  * @return
  */
public List selectBookByName(String name){
  return this.dao.selectBookByName(name);
}

// getter and setter //
public Hibernate_BookDao getDao() {
  return dao;
}

public void setDao(Hibernate_BookDao dao) {
  this.dao = dao;
}
}

---------------

(3)Book.java

--------------
package com.spring.demo.bean;

import java.io.Serializable;

/*

DROP TABLE IF EXISTS book ;

CREATE TABLE book(
id int(11) NOT NULL auto_increment COMMENT primary key,
  name varchar(60) default NULL COMMENT book name, 
  PRIMARY KEY  (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=my book table;

*/

public class Book implements Serializable {

public Book(){}

public Book(String name){
  this.name = name;
}

/**
  * ID
  */
private Integer id ;

/**
  * 书名
  */
private String name ;

// getter and setter
public Integer getId() {
  return id;
}

public void setId(Integer id) {
  this.id = id;
}

public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
}

}
--------------

(4)Book.hbm.xml
---------------
?xml version="1.0" encoding="utf-8"?
!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"

hibernate-mapping
  class name="com.spring.demo.bean.Book" table="BOOK"
  id name="id" type="integer" length="11"
  column name="id" /
  generator /
  /id
 
  property name="name" column="NAME" type="string" length="60" /property
 
  /class
/hibernate-mapping
---------------

#mysql的造表句子,指定了数据库springdemo#
---------------------
MYSQL 的BOOK 表创立句子

CREATE DATABASE IF NOT EXISTS springdemo;

USE springdemo;

DROP TABLE IF EXISTS book ;

CREATE TABLE book(
id int(11) NOT NULL auto_increment COMMENT primary key,
  name varchar(60) default NULL COMMENT book name, 
  PRIMARY KEY  (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=my book table;

---------------------

下面是我的测验办法:
===========

package com.spring.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.demo.bean.Book;
import com.spring.demo.manager.Hibernate_BookManager;

public class HibernateDemo {

/**
  * @param args
  */
public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
 
  Hibernate_BookManager manager = (Hibernate_BookManager)context.getBean("hibernate_BookManager");
 
  List books = new ArrayList();

books.add(new Book("book_01"));
  books.add(new Book("book_02"));
  books.add(new Book("book_03"));
  books.add(new Book("book_04"));
 
  manager.insertBooks(books);
 
  System.out.println("OK");

}

}
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1
  • 2

    UTLITeye

    数据,目录,文件
  • 3
  • 4
  • 5

    java与函数式编程ITeye

    函数,编程,一些
  • 6
  • 7
  • 8
  • 9
  • 10