zoukankan      html  css  js  c++  java
  • 使用spring集成hibernate

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/tx/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
    
        <!-- 方法一,直接配置hibernate.cfg.xml -->
        <bean id="sessionFactory" 
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>
        <!-- 方法二使用dataSource数据源 -->
      <!--创建sessionFactoryt -->

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"></bean>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <!-- 数据库连接 -->
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:jbit"></property>
            <property name="username" value="rong"></property>
            <property name="password" value="rong"></property>
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
          </property>
        </bean>
        <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <!-- 添加配置 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="dialect" >org.hibernate.dialect.OracleDialect</prop>
                    <prop key="show_sql" >true</prop>
                    <prop key="format_sql" >true</prop>
                </props>
            </property>
            <!-- 关系映射 -->
            <property name="mappingResource">
                <list>
                    <value>cn/bdqn/jboa/entity/CheckResult.hbm.xml</value>
                    <value>cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml</value>
                    <value>cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml</value>
                    <value>cn/bdqn/jboa/entity/Department.hbm.xml</value>
                    <value>cn/bdqn/jboa/entity/Dictionary.hbm.xml</value>
                    <value>cn/bdqn/jboa/entity/Employee.hbm.xml</value>
                    <value>cn/bdqn/jboa/entity/Position.hbm.xml</value>
                </list>
            </property>
        </bean>
        
    </beans>

    hibernate中的hibernate.cfg.xml

    <?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));
        }
    }
  • 相关阅读:
    [置顶] 输入子系统
    多线程知识点总结
    mybatis知识点总结
    redis知识点及常见面试题
    spring知识点(面试题)
    【linux】vim常用命令
    Linux常用命令大全
    Java 位运算(移位、位与、或、异或、非)
    图论之堆优化的Prim
    BZOJ3261 最大异或和 解题报告(可持久化Trie树)
  • 原文地址:https://www.cnblogs.com/xuerong/p/4922832.html
Copyright © 2011-2022 走看看