spring整合hibernate

src下的beans.xml文件

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context
="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

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

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="cn.wyzc"></context:component-scan>
<!--如果是注解方式用添加这一行下面-->
<context:annotation-config></context:annotation-config> 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1" /> <property name="user" value="root" /> <property name="password" value="root" /> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="1" /> <!--连接池中保留的最小连接数。 --> <property name="minPoolSize" value="1" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5" /> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60" /> </bean> <!--其他版本这里的版本号,甚至可能类要修改--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>cn/wyzc/entity/Emp.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect <!--其他版本这里要改版本号,甚至是类--> hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=false hiberante.cache.provider_class=org.hibernate.cache.internal.NoCacheProvider </value> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--使用基于注解方式配置事务 --> <tx:annotation-driven transaction-manager="txManager" /> </beans>

备注c3p0 property:

  //初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 initialPoolSize  
    cpds.setInitialPoolSize(initialPoolSize);   
    //连接池中保留的最大连接数。Default: 15 maxPoolSize   
    cpds.setMaxPoolSize(maxPoolSize);
    //连接池中保留的最小连接数。   
    cpds.setMinPoolSize(minPoolSize);
    //获得连接的最大等待毫秒数。Default: 1000 acquireRetryDelay
    cpds.setAcquireRetryDelay(acquireRetryDelay);
    //最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 maxIdleTime   
    cpds.setMaxIdleTime(maxIdleTime);
    //当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 acquireIncrement   
    //cpds.setAcquireIncrement(3);   
    //每60秒检查所有连接池中的空闲连接。Default: 0 idleConnectionTestPeriod   
    //cpds.setIdleConnectionTestPeriod(60);
    //连接关闭时默认将所有未提交的操作回滚。Default: false autoCommitOnClose   
    //cpds.setAutoCommitOnClose(true);
    //JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0
    //cpds.setMaxStatements(1);

    //maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数
    //cpds.setMaxStatementsPerConnection(100);

    //定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:测试的表必须在初始数据源的时候就存在。Default: null preferredTestQuery  
    //cpds.setPreferredTestQuery("select sysdate from dual");   
    // 因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的   
    // 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable   
    // 等方法来提升连接测试的性能。Default: false testConnectionOnCheckout   
    //cpds.setTestConnectionOnCheckout(true);
    //如果设为true那么在取得连接的同时将校验连接的有效性。Default: false testConnectionOnCheckin   
    //cpds.setTestConnectionOnCheckin(true);   
    //定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 acquireRetryAttempts   
    //cpds.setAcquireRetryAttempts(30);     
    //获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效   
    //保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试   
    //获取连接失败后该数据源将申明已断开并永久关闭。Default: false breakAfterAcquireFailure   
    //cpds.setBreakAfterAcquireFailure(false);   

  //两次连接中间隔时间,单位毫秒。Default: 1000 acquireRetryDelay
  cpds.setAcquireRetryDelay(60000);
View Code

 

数据层entity下的className.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 映射文件 -->
<hibernate-mapping package="cn.wyzc.entity">
<class name="Emp">
<!-- Id生成策略 -->
<id name="id" type="integer">
  <generator class="assigned" />   <!--手动分配,native是自动分配-->
</id>
<property name="name" type="string"/>
<property name="pass" type="string"/>

</class>
</hibernate-mapping>

业务层service实现类

如果是spring注解方式:  数据层用@repository 业务层@service  未知层@Component   已知层@resource   不用get/set(),可在类,构造,方法上标注的@autowired

事务标注@transactional(标注类事务应用所有方法,标注哪个方法应用在哪个方法)

@Service("empService")
public class EmpServiceImpl implements EmpService {
    @Resource
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public void addEmp(Emp emp) {
        sessionFactory.getCurrentSession().save(emp);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Emp findById(int id) {
        return (Emp)sessionFactory.getCurrentSession().get(Emp.class, id);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<Emp> findAll() {
        return (List<Emp>)sessionFactory.getCurrentSession().find("from Emp");
    }

}
public class EmpServiceImpl implements EmpService

jar包:

spring的jar包集合+hibernate的jar包集合 (两者的版本需要同一个级别)

 

posted @ 2016-06-21 21:35  乱世_独自美  阅读(96)  评论(0)    收藏  举报