spring实现泛型hibernate



泛型Hibernate Dao接口的实现

  1. 定义一个泛型接口IHibernateDao<T,PK extends Serilizable>

/**

 * @author Administrator

 * 接口里的方法可以自行定义

 * @param <T>

 * @param <PK>

 */

public interfaceIHibernateDao<T, PK extends Serializable>

  1. 定义一个HibernateDao的实现接口,通过spring来管理hibernatesession的事务处理,自行定义一些通过的方法,方便后续开发人员直接调用接口开发简单的数据库操作(如:分页查询,自定义sql查询,通过条件过滤查询)

    1. 分页查询需要的参数过滤条件、排序字段、每页大小以及页码

  1. return (List<T>) this.getHibernateTemplate().execute(

new HibernateCallback(){

public Object doInHibernate(Session session) throws HibernateException, SQLException

{

Criteria criteria = buildCriteria(session, filters, sorts);

int index = (pageNo - 1)*pageSize;

return   criteria.setFirstResult(index)

.setMaxResults(pageSize)

.list();

    }

});

  1. Criteriacriteria = session.createCriteria(entityClass);

  2. criteria.add(Restrictions.eq(property,value));

  3. criteria.addOrder(Order.desc(properties));

     

     

  1. 对象映射文件中自定义查询语句(hqlsql

    <query name="getUserByName">

               <![CDATA[from User]]>

    </query>

HibernateDao的方法实现

 

  1. return (List<T>) this.getHibernateTemplate().execute(

newHibernateCallback(){

public ObjectdoInHibernate(Session session) throws HibernateException, SQLException

{

Query query = session.getNamedQuery(queryName);

        return query.list();   

}

});

 

 

 

 

 

  1. Xml的定义,采用spring来管理hibernatesession

<?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:aop="http://www.springframework.org/schema/aop"

     xmlns:tx="http://www.springframework.org/schema/tx"

     xmlns:context="http://www.springframework.org/schema/context"

     xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

         http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd">

    

     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

         <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>

         <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:sno"/>

         <property name="user" value="sna"/>

         <property name="password" value="sna"/>

     </bean>

    

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

         <property name="dataSource" ref="dataSource"/>

         <property name="hibernateProperties">

              <props>

                   <prop key="hibernate.dialect">

org.hibernate.dialect.Oracle10gDialect

</prop>  

                   <prop key="hibernate.show_sql">true</prop>             

              </props>          

         </property>

         <property name="mappingDirectoryLocations">

              <list>

                   <value>classpath:com/sno/hibernate/</value>       

              </list>

         </property>

     </bean>

    

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

         <property name="sessionFactory" ref="sessionFactory"/>

     </bean>

    

     <tx:advice id="txAdvice" transaction-manager="txManager">

              <tx:attributes>

                   <tx:method name="get*" read-only="true"/>

                   <tx:method name="*" rollback-for="Exception"/>

              </tx:attributes>

     </tx:advice>

    

     <bean id="baseDao" class="com.sno.core.dao.HibernateDaoImpl">

         <property name="sessionFactory" ref="sessionFactory"/>

     </bean>

    

     <bean id="UserDao" class="com.sno.dao.UserDaoImpl" parent="baseDao">

         <property name="entityClass" value="com.sno.model.User"/>

     </bean>

</beans>

 

 

 

  1. 新建表对应需要新增一个实体类

    1.自增实体类对应的实体DaoUserDao extendsIHibernateDao<User, String>

UserDaoImpl extends HibernateDaoImpl<User, String> implementsUserDao

2.新增实体类的对象映射文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

  "-//Hibernate/HibernateMapping DTD 3.0//EN"

  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

  <class name="com.sno.model.User" table="SNO_E_USER">

       <id name="id" column="id" type="string">

           <generator class="uuid.hex"></generator>

       </id>

       <property name="name"  type="string">

           <column name="name"></column>

       </property>

       <property name="remark"  type="string">

           <column name="remark"></column>

       </property>

  </class>

</hibernate-mapping>

  1. 测试Spring+Hibernate<T>

@Test

public void test(){

ApplicationContextac =  new  ClassPathXmlApplicationContext("

classpath*:hibernate.cfg.xml");   

        UserDao udao = (UserDao) ac.getBean( "UserDao" );

        List<User> user = udao.getUserInfo();

        User u1 = new User();

        u1.setId("aa");

        u1.setName("abc");

        u1.setRemark("abcefgs");

        udao.save(u1);

        if(null == user){

            System.out.println("fff");

        }else{

            System.out.println("bbbb" + user.size());

        }

        for(User u : user){

            System.out.println(u.getId());

        }

}

11.png

Maven搭建

以下是maven所有依赖包

<!-- hibernate -->

         <dependency>

              <groupId>org.hibernate</groupId>

              <artifactId>hibernate-core</artifactId>

              <version>3.6.1.Final</version>

         </dependency>

         <dependency>

              <groupId>org.javassist</groupId>

              <artifactId>javassist</artifactId>

              <version>3.15.0-GA</version>

         </dependency>

 

         <!-- spring mvc -->

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-webmvc</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

 

         <!-- spring3 -->

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-core</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-context</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-jdbc</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-beans</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-web</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-expression</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-orm</artifactId>

              <version>3.2.0.RELEASE</version>

         </dependency>

         <!-- Oracle驱动包 -->

         <dependency>

              <groupId>com.oracle</groupId>

              <artifactId>ojdbc14</artifactId>

              <version>10.2.0.1.0</version>

         </dependency>

         <dependency>

              <groupId>c3p0</groupId>

              <artifactId>c3p0</artifactId>

              <version>0.9.1.2</version>

         </dependency>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值