/**
* dao 接口
* @author w7
*
*/
public interface PersonDao {
public void savePerson(Person person);
public void queryPerson();
public void queryPersonCallback();
}
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>
<class name="com.itheima09.spring.hibernate.transaction.domain.Person">
<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>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/itheima09_hibernate
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<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">
<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>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<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>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="personDao"
class="com.itheima09.spring.hibernate.transaction.dao.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory2" />
</property>
</bean>
<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: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 sessionFactory2 = (SessionFactory)context.getBean("sessionFactory2");
}
}