码迷,mamicode.com
首页 > 编程语言 > 详细

使用spring集成hibernate

时间:2015-10-30 12:32:04      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/tx/spring-context-3.2.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 12 13 <!-- 方法一,直接配置hibernate.cfg.xml --> 14 <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 15 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 16 </bean> 17 <!-- 方法二使用dataSource数据源 --> 18 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 19 <!-- 数据库连接 --> 20 <property name="url" value="jdbc:oracle:thin:@localhost:1521:jbit"></property> 21 <property name="username" value="rong"></property> 22 <property name="password" value="rong"></property> 23 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
      </property> 24 </bean> 25 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 26 <property name="dataSource" ref="dataSource"></property> 27 <!-- 添加配置 --> 28 <property name="hibernateProperties"> 29 <props> 30 <prop key="dialect" >org.hibernate.dialect.OracleDialect</prop> 31 <prop key="show_sql" >true</prop> 32 <prop key="format_sql" >true</prop> 33 </props> 34 </property> 35 <!-- 关系映射 --> 36 <property name="mappingResource"> 37 <list> 38 <value>cn/bdqn/jboa/entity/CheckResult.hbm.xml</value> 39 <value>cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml</value> 40 <value>cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml</value> 41 <value>cn/bdqn/jboa/entity/Department.hbm.xml</value> 42 <value>cn/bdqn/jboa/entity/Dictionary.hbm.xml</value> 43 <value>cn/bdqn/jboa/entity/Employee.hbm.xml</value> 44 <value>cn/bdqn/jboa/entity/Position.hbm.xml</value> 45 </list> 46 </property> 47 </bean> 48 49 </beans>

hibernate中的hibernate.cfg.xml

 1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6 
 7 <session-factory>
 8     <property name="connection.url">
 9         jdbc:oracle:thin:@localhost:1521:jbit
10     </property>
11     <property name="connection.username">rong</property>
12     <property name="connection.password">rong</property>
13     <property name="connection.driver_class">
14         oracle.jdbc.driver.OracleDriver
15     </property>
16     <property name="dialect">
17         org.hibernate.dialect.OracleDialect
18     </property>
19     <property name="show_sql">true</property>
20     <property name="format_sql">true</property>
21     <mapping resource="cn/bdqn/jboa/entity/CheckResult.hbm.xml" />
22     <mapping resource="cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml" />
23     <mapping resource="cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml" />
24     <mapping resource="cn/bdqn/jboa/entity/Department.hbm.xml" />
25     <mapping resource="cn/bdqn/jboa/entity/Dictionary.hbm.xml" />
26     <mapping resource="cn/bdqn/jboa/entity/Employee.hbm.xml" />
27     <mapping resource="cn/bdqn/jboa/entity/Position.hbm.xml" />
28 </session-factory>
29 
30 </hibernate-configuration>

更改后,调用事务就变得很简单了

1 public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
2 
3     @Override
4     public Employee findById(Serializable id) {
5         // TODO Auto-generated method stub
6         return this.getHibernateTemplate().get(Employee.class, id);
7     }
8 
9 }

创建接口类

1 public interface ClaimVoucherDao {
2     public List<ClaimVoucher> find(int first,int pageSize);
3 }

 

回调机制

 1 /**
 2  * 报销单类
 3  * @author Administrator
 4  *
 5  */
 6 public class ClaimVoucherDaoImpl extends HibernateDaoSupport 
 implements ClaimVoucherDao{ 7 8 @SuppressWarnings("unchecked") 9 @Override 10 public List<ClaimVoucher> find(final int first, final int pageSize) { 11 12 13 return this.getHibernateTemplate().executeFind(new HibernateCallback() { 14 15 @Override 16 public Object doInHibernate(Session arg0) 17 throws HibernateException, SQLException { 18 // TODO Auto-generated method stub 19 return arg0.createQuery(" from ClaimVoucher c") 20 .setFirstResult(first) 21 .setMaxResults(pageSize) 22 .list(); 23 } 24 }); 25 } 26 }

在xml中添加dao的实例

1 <!-- dao -->
2     <bean id="userDao" class="cn.bdqn.jboa.dao.UserDaoImpl">
3         <property name="sessionFactory" ref="sessionFactory"></property>
4     </bean>
5     <bean id="claimVoucherDao" class="cn.bdqn.jboa.dao.ClaimVoucherDaoImpl">
6         <property name="sessionFactory" ref="sessionFactory"></property>
7     </bean>

测试类

public class testClaim {
    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
       ClaimVoucherDao claimVoucherDao = (ClaimVoucherDao) ctx.getBean("claimVoucherDao");
        System.out.println(claimVoucherDao.find(0, 3));
        System.out.println(claimVoucherDao.find(4, 3));
    }
}

 

使用spring集成hibernate

标签:

原文地址:http://www.cnblogs.com/xuerong/p/4922832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!