spring + hibernate+ C3P0+MYSQL配置

阅读更多

看下面的几个文件:
可能贴的顺序上有点问题:



(1) applicationContext.xml 

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





 
 
  
   
    jdbc.properties
   
  
 
 
 
 
  ${jdbc.driverClass}
  ${jdbc.url}
  ${jdbc.user}
  ${jdbc.password}
  ${jdbc.initialPoolSize}
  ${jdbc.minPoolSize}
  ${jdbc.maxPoolSize}
 

 
 
 
  
   
  
  
   
    org.hibernate.dialect.MySQLDialect
    true
    true
    80
    35
   
  
   
           
    com/spring/demo/bean/mapping/Book.hbm.xml                                                
    
   
 
 
 
 
 
 
  
   
  
 
 
 
 
 
  
   
  
  
   
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED
    PROPAGATION_REQUIRED,readOnly
   
  
 
 
 
 
 
 
  
   
  
 
 
 
 
  
   
    
     
    
   
  
 
 
 


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

(2) jdbc.properties
## 这个事连接mysql数据库的具体参数配置,
## 楼主要根据自己的数据来配置,不可直接使用
---------------------

jdbc.driverClass=com.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 ; i2){
    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
---------------




    
        
            
            
        
       
        
       
    

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



#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");

 }

}

你可能感兴趣的:(Hibernate,Spring,MySQL,配置管理,Bean)