所谓spring对hibernate的集成,其实它就做了两方面的工作:
1.提供了可以在配置文件中进行配置的完成了对数据源和SessionFactory信息进行封装的bean类。比如:对数据源进行封装的类BasicDataSource,对SessionFactory进行封装的类LocalSessionFactoryBean。
2.提供了对hibernate的一些公共操作进行了封装的类。比如org.springframework.orm.hibernate3.HibernateTemplate。这里,HibernateTemplate并没有完成任何实质性的功能,它也只是对Hibernate中session方法的对应调用。并且,对于Hibernate中session的大多数方法,在HibernateTemplate中都有与之对应的方法。它还提供了一个通用的方法excute()来处理一般情况,但是要求传入一个HibernateCallback接口类型的对象,使程序员可以在实现方法doInHibernate(Session session)中借助spring给提供的session来自己进行Hibernate操作。总之,spring就是要省去我们手动创建hibernate中session的操作以及事务管理。
实体类:
public
class Teacher
implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String email;
private Date birthday;
private Double salary;
//各属性的setter.getter方法
}
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String email;
private Date birthday;
private Double salary;
//各属性的setter.getter方法
}
映射文件:
<
hibernate-mapping
package
="com.cernet.spring.hibernate.entity"
>
< class name ="Teacher" table ="s_spring_teacher" >
< id name ="id" column ="id" >
< generator class ="sequence" >
< param name ="sequence" >s_spring_teacher_id_seq </ param >
</ generator >
</ id >
< property name ="name" column ="name" type ="string" > </ property >
< property name ="email" column ="email" type ="string" > </ property >
< property name ="birthday" column ="birthday" type ="date" > </ property >
< property name ="salary" column ="salary" type ="double" > </ property >
</ class >
</ hibernate-mapping >
< class name ="Teacher" table ="s_spring_teacher" >
< id name ="id" column ="id" >
< generator class ="sequence" >
< param name ="sequence" >s_spring_teacher_id_seq </ param >
</ generator >
</ id >
< property name ="name" column ="name" type ="string" > </ property >
< property name ="email" column ="email" type ="string" > </ property >
< property name ="birthday" column ="birthday" type ="date" > </ property >
< property name ="salary" column ="salary" type ="double" > </ property >
</ class >
</ hibernate-mapping >
Dao实现类:
public
class TeacherDaoImpl
implements TeacherDao {
private HibernateTemplate template = null;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
@Override
public void delete(Integer id) {
Teacher t = (Teacher) template.get(Teacher. class, id); // 到数据库中查指定id对应的记录
if (t != null) {
template.delete(t);
}
}
@Override
public void delete(Teacher t) {
if (t.getId() != null) {
template.delete(t);
}
}
@Override
public Teacher getTeacherById(Integer id) {
return (Teacher) template.get(Teacher. class, id);
}
@SuppressWarnings( "unchecked")
@Override
public List<Teacher> getTeachersByName(String name) {
String hql = "from Teacher as t where t.name=?";
Object args[] = { name }; //对应占位符的参数值
return (List<Teacher>) template.find(hql, args);
}
@SuppressWarnings( "unchecked")
@Override
public List<Teacher> getTeachers() {
String hql = "from Teacher";
return template.find(hql);
}
@Override
public void insert(Teacher t) {
template.saveOrUpdate(t);
}
@Override
public void update( final Teacher t) {
template.execute( new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
session.saveOrUpdate(t); //这里借助spring给提供的session来完成hibernate操作
return null;
}
});
}
}
private HibernateTemplate template = null;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
@Override
public void delete(Integer id) {
Teacher t = (Teacher) template.get(Teacher. class, id); // 到数据库中查指定id对应的记录
if (t != null) {
template.delete(t);
}
}
@Override
public void delete(Teacher t) {
if (t.getId() != null) {
template.delete(t);
}
}
@Override
public Teacher getTeacherById(Integer id) {
return (Teacher) template.get(Teacher. class, id);
}
@SuppressWarnings( "unchecked")
@Override
public List<Teacher> getTeachersByName(String name) {
String hql = "from Teacher as t where t.name=?";
Object args[] = { name }; //对应占位符的参数值
return (List<Teacher>) template.find(hql, args);
}
@SuppressWarnings( "unchecked")
@Override
public List<Teacher> getTeachers() {
String hql = "from Teacher";
return template.find(hql);
}
@Override
public void insert(Teacher t) {
template.saveOrUpdate(t);
}
@Override
public void update( final Teacher t) {
template.execute( new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
session.saveOrUpdate(t); //这里借助spring给提供的session来完成hibernate操作
return null;
}
});
}
}
配置文件:
<beans>
<!-- 配置数据源信息 -->
<bean id= "dataSource"
class= "org.apache.commons.dbcp.BasicDataSource">
<property name= "driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name= "url">
<value>jdbc:oracle:thin:@localhost:1521:orcl10</value>
</property>
<property name= "username">
<value>scott</value>
</property>
<property name= "password">
<value>yf123</value>
</property>
</bean>
<!-- 配置Hibernate的SessionFactory信息 -->
<bean id= "sessionFactory"
class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name= "dataSource">
<ref local= "dataSource" />
</property>
<property name= "hibernateProperties">
<props>
<prop key= "hibernate.show_sql"> true</prop>
<prop key= "hibernate.format_sql"> true</prop>
<prop key= "hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name= "mappingResources">
<list>
<value>
com/cernet/spring/hibernate/entity/Teacher.hbm.xml
</value>
</list>
</property>
</bean>
<!-- 配置spring给提供的Hibernate模板类 -->
<bean id= "template"
class= "org.springframework.orm.hibernate3.HibernateTemplate">
<property name= "sessionFactory">
<ref bean= "sessionFactory" />
</property>
</bean>
<bean id= "dao"
class= "com.cernet.spring.hibernate.dao.TeacherDaoImpl">
<property name= "template">
<ref bean= "template" />
</property>
</bean>
</beans>
<!-- 配置数据源信息 -->
<bean id= "dataSource"
class= "org.apache.commons.dbcp.BasicDataSource">
<property name= "driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name= "url">
<value>jdbc:oracle:thin:@localhost:1521:orcl10</value>
</property>
<property name= "username">
<value>scott</value>
</property>
<property name= "password">
<value>yf123</value>
</property>
</bean>
<!-- 配置Hibernate的SessionFactory信息 -->
<bean id= "sessionFactory"
class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name= "dataSource">
<ref local= "dataSource" />
</property>
<property name= "hibernateProperties">
<props>
<prop key= "hibernate.show_sql"> true</prop>
<prop key= "hibernate.format_sql"> true</prop>
<prop key= "hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name= "mappingResources">
<list>
<value>
com/cernet/spring/hibernate/entity/Teacher.hbm.xml
</value>
</list>
</property>
</bean>
<!-- 配置spring给提供的Hibernate模板类 -->
<bean id= "template"
class= "org.springframework.orm.hibernate3.HibernateTemplate">
<property name= "sessionFactory">
<ref bean= "sessionFactory" />
</property>
</bean>
<bean id= "dao"
class= "com.cernet.spring.hibernate.dao.TeacherDaoImpl">
<property name= "template">
<ref bean= "template" />
</property>
</bean>
</beans>
本文转自NightWolves 51CTO博客,原文链接:
http://blog.51cto.com/yangfei520/251236
,如需转载请自行联系原作者