Spring+hibernate+C 3 P 0+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");

 }

}