整合思路是,使DAO继承HibernateTemplate这个类
HibernateTemplate这个类提供了setSessionFactory()方法用于注入SessionFactory
通过spring获取DAO的时候,注入SessionFactory.
步骤1:pojo
步骤2:hbm
步骤3:DAO
步骤4:applicationContext.xml
步骤5:测试查询
步骤6:测试增 删 改
步骤 1 : pojo
package com.how2java.pojo;
public class Category {
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
private int id;
private String name;
}
|
步骤 2 : hbm
<? xml version = "1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
< hibernate-mapping package = "com.how2java.pojo" >
< class name = "Category" table = "category_" >
< id name = "id" column = "id" >
< generator class = "native" >
</ generator >
</ id >
< property name = "name" />
</ class >
</ hibernate-mapping >
|
步骤 3 : DAO
DAO继承HibernateTemplete,而HibernateTemplete类里有一个setSessionFactory用于接收sessionFactory的注入
package com.how2java.dao;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class CategoryDAO extends HibernateTemplate{
}
|
步骤 4 : applicationContext.xml
创建dao的时候,会注入sessionfactory
创建sessionFactory的时候会注入数据源ds
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xmlns:tx = "http://www.springframework.org/schema/tx"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
< bean name = "c" class = "com.how2java.pojo.Category" >
< property name = "name" value = "yyy" />
</ bean >
< bean name = "dao" class = "com.how2java.dao.CategoryDAO" >
< property name = "sessionFactory" ref = "sf" />
</ bean >
< bean name = "sf"
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name = "dataSource" ref = "ds" />
< property name = "mappingResources" >
< list >
< value >com/how2java/pojo/Category.hbm.xml</ value >
</ list >
</ property >
< property name = "hibernateProperties" >
< value >
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hbm2ddl.auto=update
</ value >
</ property >
</ bean >
< bean name = "ds"
class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
< property name = "url" value = "jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8" />
< property name = "username" value = "root" />
< property name = "password" value = "admin" />
</ bean >
</ beans >
|
步骤 5 : 测试查询
因为CategoryDAO 继承了HibernateTemplate,所以可以直接使用
find来查询所有的Category
![测试查询](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9zdGVwaW1hZ2V3bS5ob3cyai5jbi8yMjYucG5n?x-oss-process=image/format,png)
package com.how2java.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.dao.CategoryDAO;
import com.how2java.pojo.Category;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
CategoryDAO dao = (CategoryDAO) context.getBean( "dao" );
List<Category> cs= dao.find( "from Category c" );
System.out.println(cs);
}
}
|
步骤 6 : 测试增 删 改
因为CategoryDAO 继承了HibernateTemplate,所以可以直接使用
1. save 增加
2. get 获取
3. update 修改
4. delete 删除
package com.how2java.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.dao.CategoryDAO;
import com.how2java.pojo.Category;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
CategoryDAO dao = (CategoryDAO) context.getBean( "dao" );
Category c = new Category();
c.setName( "category yyy" );
//增加
dao.save(c);
//获取
Category c2 = dao.get(Category. class , 1 );
//修改
c2.setName( "category zzz" );
dao.update(c2);
//删除
dao.delete(c2);
}
}
|
更多内容,点击了解: https://how2j.cn/k/spring-hibernate/spring-hibernate-integration/95.html