您的位置:首页 > 编程语言 > Java开发

spring和Hibernate 整合

2016-02-14 18:38 405 查看


/**
* dao 接口
* @author w7
*
*/
public interface PersonDao {
public void savePerson(Person person);
public void queryPerson();
public void queryPersonCallback();
}


/*
* dao 接口实现  ,并使用 HibernateDaoSupport的模板类来操作数据库
*/
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{

/**
* 添加
*/
public void savePerson(Person person) {
this.getHibernateTemplate().save(person);
}

/**
* 查询
*/
public void queryPerson(){
List<Person> pList = this.getHibernateTemplate().find("from Person");
pList = this.getHibernateTemplate().find("from Person where name=?", "aa");
pList = this.getHibernateTemplate().find("from Person where name=? and description=?", new Object[]{"aa","asfd"});

System.out.println(pList.size());
}

/**
*
* hibernate 的回调方法接口:HibernateCallback
* 实现这个接口就可以得到hibernate session的对象了,通过得到的session来编写业务逻辑
* 当使用hibernate 提供的模板类操作数据库时,不能满足业务需求时,则利用这个接口
* 来编写业务逻辑,这里的业务逻辑最终由hibernate 来执行
*/
public void queryPersonCallback() {
this.getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException,
SQLException {

//编写业务逻辑

return null;
}
});
}
}


/**
* person类
* @author w7
*
*/
public class Person implements Serializable{
private Long pid;
private String name;
private String description;

public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

//Person.hbm.xml配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
描述一个持久化类
name属性为持久化类的全名
table 该持久化类对应的表名  默认情况下为类名
catalog 为数据库的名称
-->
<class name="com.itheima09.spring.hibernate.transaction.domain.Person">
<!--
id对应表中的主键
name为持久化类中属性的名称
length 为对应数据库表中相应字段的长度
column  属性的名称对应的表的字段名称   不写则默认和属性的名称一致
-->
<id name="pid" length="5" type="java.lang.Long" column="pid">
<!--
主键的生成器
-->
<generator class="increment"></generator>
</id>

<property name="name" column="name" type="java.lang.String" length="20">
</property>

<property name="description" column="description" type="java.lang.String" length="50">
</property>
</class>
</hibernate-mapping>


/**
* service 接口
* @author w7
*
*/
public interface PersonService {
public void savePerson(Person person);
}


/**
* service 接口实现
* @author w7
*
*/
public class PersonServiceImpl implements PersonService{
private PersonDao personDao;

public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

public void savePerson(Person person) {
this.personDao.savePerson(person);
}
}


// hibernate.cfg.xml配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<!--
一个session-factory代表一个数据库
-->
<session-factory>

<!--
链接数据库的用户名
-->
<property name="connection.username">root</property>

<!--
链接数据库的密码
-->
<property name="connection.password">root</property>

<!--
链接数据库的驱动
-->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>

<!--
链接数据库的url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/itheima09_hibernate
</property>

<!--
方言
告诉hibernate用什么样的数据库
-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<!--
validate 加载hibernate时,验证数据库的结构
update  加载hibernate时,检查数据库,如果表不存在,则创建,如果存在,则更新
create  每次加载hiberante,都会创建表
create-drop  每次加载hiberante,创建,卸载hiberante时,销毁
-->
<property name="hbm2ddl.auto">update</property>

<!--
显示sql语句
-->
<property name="show_sql">true</property>

<!--
格式化sql语句
-->
<property name="format_sql">true</property>

<!--
加载映射文件
-->
<mapping
resource="com/itheima09/spring/hibernate/transaction/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
这里写代码片


//applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 引入dataSource 把dao层和service层的类导入进来 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<!--
数据库源
-->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!--
sessionFactory第一种配置
sessionFactory:用于操作数据库操作
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>

<!-- 导入映射文件(*.hbm.xml)所在的路径 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/itheima09/spring/hibernate/transaction/domain
</value>
</list>
</property>

<!-- 其他配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<!--
sessionFactory第二种配置
-->
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!--
dao
-->
<bean id="personDao"
class="com.itheima09.spring.hibernate.transaction.dao.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory2" />
</property>
</bean>

<!--
service
-->
<bean id="personService"
class="com.itheima09.spring.hibernate.transaction.service.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao" />
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory2"/>
</property>
</bean>

<!--
事务声明式策略
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*"
isolation="DEFAULT"
propagation="REQUIRED"
read-only="false"/>
</tx:attributes>
</tx:advice>

<!--
事务通知 aop
-->
<aop:config>
<aop:pointcut
expression="execution(* com.itheima09.spring.hibernate.transaction.service.*.*(..))"
id="perform" />

<aop:advisor advice-ref="tx" pointcut-ref="perform" />
</aop:config>
</beans>


/**
* 测试dao层
*/
@Test
public void testQueryPerson(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao personDao = (PersonDao)context.getBean("personDao");
personDao.queryPerson();
}

/**
* 测式service层
* @author w7
*
*/
public class PersonServiceTest {
@Test
public void testSavePerson(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService personService = (PersonService)context.getBean("personService");
Person person = new Person();
person.setName("aa");
personService.savePerson(person);
}
}

/**
* 测试 sessionFactory
* @author w7
*
*/
public class SessionFactoryTest {
@Test
public void testSessionFactory(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");

SessionFactory sessionFactory2 = (SessionFactory)context.getBean("sessionFactory2");
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: