使用spring集成hibernate
2015-10-30 11:36
495 查看
<?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>
更改后,调用事务就变得很简单了
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{ @Override public Employee findById(Serializable id) { // TODO Auto-generated method stub return this.getHibernateTemplate().get(Employee.class, id); } }
创建接口类
public interface ClaimVoucherDao { public List<ClaimVoucher> find(int first,int pageSize); }
回调机制
/** * 报销单类 * @author Administrator * */ public class ClaimVoucherDaoImpl extends HibernateDaoSupport implements ClaimVoucherDao{ @SuppressWarnings("unchecked") @Override public List<ClaimVoucher> find(final int first, final int pageSize) { return this.getHibernateTemplate().executeFind(new HibernateCallback() { @Override public Object doInHibernate(Session arg0) throws HibernateException, SQLException { // TODO Auto-generated method stub return arg0.createQuery(" from ClaimVoucher c") .setFirstResult(first) .setMaxResults(pageSize) .list(); } }); } }
在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)); } }
相关文章推荐
- 用java实现 数三退出游戏(2)约瑟夫环
- hdu 1001 初学java代码
- Spring与Mybatis的整合方法有哪些
- Java 构造函数
- java基础学习笔记(5)
- iOS AsyncSocket 与 Java Netty 的简单socket使用
- java基础学习笔记(4)
- java基础学习笔记(3)
- 遍历map的四种方法
- java.exe重命名之后无法被 jvisualvm、jconsole监控的问题
- java lock condition
- java基础学习笔记(2)
- maven_spring_cxf
- [Java并发包学习九]Java中的阻塞队列
- 从头认识java-6.3 组合使用聚合和继承
- [Java并发包学习八]深度剖析ConcurrentHashMap
- 从头认识java-6.3 组合使用聚合和继承
- [Java并发包学习七]解密ThreadLocal
- 集合类说明及区别
- javax.mail 调用企业邮箱发送邮件