HibernateDaoSupport的使用
一、HibernateDaoSupport概述
HibernateDaoSupport同样也是Spring提供的、为Hibernate访问数据库的DAO组件:该类主要提供如下两个方法,方便DAO的实现:
1、public final HiberanteTemplate getHibernateTemplate()
2、public final void setSessionFactory(SessionFactory sessionFactory)
其中setSessionFactory方法用来接收Spring的ApplicationContext的依赖注入,可接收配置在Spring的SessionFactory实例,getHibernateTemplate方法则用来生成HibernateTeplate模板对象来完成数据库访问。
同样常用HibernateDaoSupport中的getSession方法来获取Session对象对数据库进行操作,相对HibernateTemplate对象而言,HibernateDaoSupport应用更加灵活。
接下来,看看如何开发Spring整合Hibernate的程序(XML版本)
一、导入项目jar包
需要到的jar包括:
1)hibernate相关jar
2)SpringIOC和AOP相关jar
3)Spring-ORM整合jar
4)mysql驱动程序
二、准备数据库表和实体类
创建数据库:
create database spring;
use spring;
创建表:
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
package com.yiidian.domain;
import java.io.Serializable;
/**
* @author http://www.yiidian.com
*
*/
public class Account implements Serializable{
private Integer id;
private String name;
private Float money;
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 Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", money=" + money
+ "]";
}
}
三、编写Account类的Hibernate映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
http://www.yiidian.com
-->
<hibernate-mapping>
<class name="com.yiidian.domain.Account" table="account">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="money" column="money"></property>
</class>
</hibernate-mapping>
四、编写Dao接口和实现
AccountDao接口:
package com.yiidian.dao;
import java.util.List;
import com.yiidian.domain.Account;
/**
*
* @author http://www.yiidian.com
*
*/
public interface AccountDao {
public List<Account> findAll();
}
AccountDaoIpml:
package com.yiidian.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.yiidian.dao.AccountDao;
import com.yiidian.domain.Account;
/**
* @author http://www.yiidian.com
*
*/
public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao {
@Override
public List<Account> findAll() {
return this.getHibernateTemplate().loadAll(Account.class);
}
}
五、配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="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.MySQL5Dialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/yiidian/domain/Account.hbm.xml</value>
</list>
</property>
</bean>
<bean id="accountDao" class="com.yiidian.dao.impl.AccountDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
六、编写测试类
package com.yiidian.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiidian.dao.AccountDao;
import com.yiidian.domain.Account;
/**
* @author http://www.yiidian.com
*
*/
public class HibernateTemplateDemo {
public static void main(String[] args) {
// 1.获取Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.根据id获取bean对象
AccountDao dao = (AccountDao)ac.getBean("accountDao");
List<Account> list = dao.findAll();
for (Account account : list) {
System.out.println(account);
}
}
}
七、运行结果
热门文章
优秀文章