Spring整合Hibernate 使用 声明式事务管理

java实体类

 public class Dept {
    private int deptNo;
    private String dname;
    private String loc;

    @Override
    public String toString() {
        return "Dept{" +
                "deptNo=" + deptNo +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }

    public int getDeptNo() {
        return deptNo;
    }

    public void setDeptNo(int deptNo) {
        this.deptNo = deptNo;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

映射文件配置

<!-- 映射配置 -->
<hibernate-mapping package="com.xt.e_transcation_hibernate">
    <class name="Dept">
        <id name="deptNo" column="deptno">
            <generator class="assigned"></generator>
        </id>
        <property name="loc"></property>
        <property name="dname"></property>
    </class>
</hibernate-mapping>

Hibernate 主配置文件

<hibernate-configuration>
    <session-factory>
        <!-- 显示sql语句-->
        <property name="show_sql">true</property>
        <!-- 格式化 sql 语句 -->
        <property name="format_sql">true</property>
        <!-- 引入配置文件-->
        <mapping resource="com/xt/e_transcation_hibernate/Dept.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

spring文件配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
      ">

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="maxPoolSize" value="6"></property>
        <property name="initialPoolSize" value="3"></property>
    </bean>

    <!-- 配置hibernate sqlSessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载 hibernate主配置文件
            也可以通过spring方式配置 hibernate文件 两者选一种即可
        -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

        <!-- 所有配置交给spring进行管理-->
        <property name="hibernateProperties">
            <props>
                <!-- 格式化sql -->
                <prop key="hibernate.format_sql">true</prop>
                <!-- 显示sql -->
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <!-- 读取映射文件 -->
                <value>classpath:com/xt/e_transcation_hibernate/Dept.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置 dao -->
    <bean id="deptDao" class="com.xt.e_transcation_hibernate.DeptDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--  配置 service -->
    <bean id="deptService" class="com.xt.e_transcation_hibernate.DeptService">
        <property name="deptDao" ref="deptDao"></property>
    </bean>

    <!-- 配置 事务管理  -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--  事务通知配置 -->
    <tx:advice id="txAdvice" transaction-manager="txManager" >
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!-- 事务 aop 配置 -->
    <aop:config>
        <!-- 切入点表达式-->
        <aop:pointcut id="pt" expression="execution( * com.xt.e_transcation_hibernate.DeptService.*(..))"></aop:pointcut>
        <!-- 应用通知 + 切入点 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>
</beans>

Dao类

public class DeptDao {
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void insert(Dept dept){
        //spring 与 hibernate 整合 使用 getCurrentSession方式获取session   getSession是无效的
        Session session = sessionFactory.getCurrentSession();
        session.save(dept);
    }
}

service类

public class DeptService {
    private DeptDao deptDao;

    public DeptDao getDeptDao() {
        return deptDao;
    }

    public void setDeptDao(DeptDao deptDao) {
        this.deptDao = deptDao;
    }

    public void save(){
        Dept dept = new Dept();
        dept.setDeptNo(51);
        dept.setDname("研发12部门");
        dept.setLoc("912");

        Dept dept2 = new Dept();
        dept2.setDeptNo(52);
        dept2.setDname("研发52部门");
        dept2.setLoc("952");

        deptDao.insert(dept);
       /* int i = 1/0;
        deptDao.insert(dept2);*/
    }
}

到此 spring 整合 hibernate 完毕,并且配置好声明式事务,可以在service中进行测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值