Spring DAO之Hibernate
HibernateDaoSupport - Hibernate数据访问对象的基类。 需要一个SessionFactory,同时为子类提供 HibernateTemplate。也可以选择直接通过 提供一个HibernateTemplate来初始化, 这样就可以重用后者的设置,例如SessionFactory, flush模式,异常翻译器(exception translator)等等。
本节主要讨论Sping对HibernateTemplate的支持。
下面是个例子:
drop
table
if
exists
user;
/*==============================================================*/
/* Table: user */
/*==============================================================*/
create table user
(
id bigint AUTO_INCREMENT not null,
name varchar(24),
age int,
primary key (id)
);
/*==============================================================*/
/* Table: user */
/*==============================================================*/
create table user
(
id bigint AUTO_INCREMENT not null,
name varchar(24),
age int,
primary key (id)
);
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:37:34
* Note: Hiberante实体类
*/
public class User {
private Integer id;
private String name;
private Integer age;
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:37:34
* Note: Hiberante实体类
*/
public class User {
private Integer id;
private String name;
private Integer age;
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
xml
version
="1.0"
encoding
="utf-8"
?>
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< hibernate-mapping >
< class name ="com.lavasoft.springnote.ch06_hbm_02deTx.User"
table ="user" >
< id name ="id" column ="id" >
< generator class ="native" />
id >
< property name ="name" column ="name" />
< property name ="age" column ="age" />
class >
hibernate-mapping >
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< hibernate-mapping >
< class name ="com.lavasoft.springnote.ch06_hbm_02deTx.User"
table ="user" >
< id name ="id" column ="id" >
< generator class ="native" />
id >
< property name ="name" column ="name" />
< property name ="age" column ="age" />
class >
hibernate-mapping >
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:37:43
* Note: DAO接口
*/
public interface IUserDAO {
public void insert(User user);
public User find(Integer id);
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:37:43
* Note: DAO接口
*/
public interface IUserDAO {
public void insert(User user);
public User find(Integer id);
}
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:15:55
* Note: DAO实现
*/
public class UserDAO implements IUserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public void insert(User user) {
hibernateTemplate.save(user);
System.out.println( "所保存的User对象的ID:"+user.getId());
}
public User find(Integer id) {
User user =(User) hibernateTemplate.get(User. class, id);
return user;
}
}
import org.springframework.orm.hibernate3.HibernateTemplate;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:15:55
* Note: DAO实现
*/
public class UserDAO implements IUserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public void insert(User user) {
hibernateTemplate.save(user);
System.out.println( "所保存的User对象的ID:"+user.getId());
}
public User find(Integer id) {
User user =(User) hibernateTemplate.get(User. class, id);
return user;
}
}
xml
version
="1.0"
encoding
="UTF-8"
?>
"http://www.springframework.org/dtd/spring-beans.dtd">
< beans >
< bean id ="dataSource"
class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name ="driverClassName" >
< value >com.mysql.jdbc.Driver value >
property >
< property name ="url" >
< value >jdbc:mysql://localhost:3306/springdb value >
property >
< property name ="username" >
< value >root value >
property >
< property name ="password" >
< value >leizhimin value >
property >
bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method ="close" >
< property name ="dataSource" >
< ref bean ="dataSource" />
property >
< property name ="mappingResources" >
< list >
< value >com/lavasoft/springnote/ch06_hbm_02proTx/User.hbm.xml value >
list >
property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
prop >
props >
property >
bean >
< bean id ="userDAO" class ="com.lavasoft.springnote.ch06_hbm_02proTx.UserDAO" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
property >
bean >
beans >
"http://www.springframework.org/dtd/spring-beans.dtd">
< beans >
< bean id ="dataSource"
class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name ="driverClassName" >
< value >com.mysql.jdbc.Driver value >
property >
< property name ="url" >
< value >jdbc:mysql://localhost:3306/springdb value >
property >
< property name ="username" >
< value >root value >
property >
< property name ="password" >
< value >leizhimin value >
property >
bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method ="close" >
< property name ="dataSource" >
< ref bean ="dataSource" />
property >
< property name ="mappingResources" >
< list >
< value >com/lavasoft/springnote/ch06_hbm_02proTx/User.hbm.xml value >
list >
property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
prop >
props >
property >
bean >
< bean id ="userDAO" class ="com.lavasoft.springnote.ch06_hbm_02proTx.UserDAO" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
property >
bean >
beans >
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:16:34
* Note: 测试类、客户端
*/
public class SpringHibernateDemo {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext( "D:\\_spring\\src\\com\\lavasoft\\springnote\\ch06_hbm_02proTx\\bean-hbm_tx.xml");
// 建立DAO物件
IUserDAO userDAO = (IUserDAO) context.getBean( "userDAO");
User user = new User();
user.setName( "caterpillar");
user.setAge( new Integer(30));
userDAO.insert(user);
user = userDAO.find( new Integer(1));
System.out.println( "name: " + user.getName());
}
}
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-23 15:16:34
* Note: 测试类、客户端
*/
public class SpringHibernateDemo {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext( "D:\\_spring\\src\\com\\lavasoft\\springnote\\ch06_hbm_02proTx\\bean-hbm_tx.xml");
// 建立DAO物件
IUserDAO userDAO = (IUserDAO) context.getBean( "userDAO");
User user = new User();
user.setName( "caterpillar");
user.setAge( new Integer(30));
userDAO.insert(user);
user = userDAO.find( new Integer(1));
System.out.println( "name: " + user.getName());
}
}
运行结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
所保存的User对象的ID:18
name: jdbctemplate
Process finished with exit code 0
log4j:WARN Please initialize the log4j system properly.
所保存的User对象的ID:18
name: jdbctemplate
Process finished with exit code 0
以上对数据的操作都没有添加事务,后面会专门介绍。