<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    spring 與 hibernate 整合(事務(wù))

    Posted on 2008-05-09 13:47 G_G 閱讀(3157) 評論(0)  編輯  收藏 所屬分類: hibernateSpringAOP
    參考:第?9?章?事務(wù)管理 - Spring Framework reference 2.0.5 參考手冊中文版
    http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09.html

    先從配置文件開始:
    源碼:springAop.rar

    需要jar
    <?xml?version="1.0"?encoding="UTF-8"?>
    <classpath>
    ????
    <classpathentry?kind="src"?path="java"/>
    ????
    <classpathentry?kind="con"?path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    ????
    <classpathentry?kind="lib"?path="lib/aspectjrt.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/aspectjweaver.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/spring.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/spring-sources.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/commons-logging-1.0.4.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/cglib-nodep-2.1_3.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/hibernate3.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/log4j-1.2.11.jar"/>
    ????
    <classpathentry?kind="con"?path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
    ????
    <classpathentry?kind="lib"?path="lib/dom4j-1.6.1.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/commons-collections-2.1.1.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/mysql.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/jta.jar"/>
    ????
    <classpathentry?kind="lib"?path="lib/antlr-2.7.6.jar"/>
    ????
    <classpathentry?kind="output"?path="bin"/>
    </classpath>


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

    ????
    <!--?demo?start?-->
    ????
    <import?resource="demo_spring.xml"?/>
    ????
    <!--?demo?end?-->

    ????
    <bean?id="dataSource"
    ????????class
    ="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ????????
    <property?name="driverClassName"
    ????????????value
    ="com.mysql.jdbc.Driver">
    ????????
    </property>
    ????????
    <property?name="url"
    ????????????value
    ="jdbc:mysql://localhost:3306/aop?characterEncoding=utf8">
    ????????
    </property>
    ????????
    <property?name="username"?value="root"></property>
    ????????
    <property?name="password"?value=""></property>
    ????
    </bean>

    ????
    <!--?hibernate3?sessionFactory??-->
    ????
    <bean?id="sessionFactory"
    ????????class
    ="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    ????????
    <!--?此次??為?spring?事務(wù)需要使用?dataSource?;為空事務(wù)由Hibernian自己維護?-->
    ????????
    <property?name="dataSource"?ref="dataSource"?/>
    ????????
    <property?name="configLocation"
    ????????????value
    ="classpath:hibernate.cfg.xml"?/>
    ????
    </bean>



    ????
    <!--?事務(wù)適配器?-->
    ????
    <bean?id="txManager"
    ????????class
    ="org.springframework.orm.hibernate3.HibernateTransactionManager">
    ????????
    <property?name="sessionFactory"?ref="sessionFactory"?/>
    ????
    </bean>






    ????
    <!--?aop?與事務(wù)聯(lián)系?aopBean<->txAdvice??-->
    ????
    <aop:config>
    ????????
    <!--?邏輯攔截?-->
    ????????
    <aop:pointcut?id="demoAopBean"
    ????????????expression
    ="execution(*?demo*.*.*(..))"?/>
    ????????
    <aop:advisor?advice-ref="demoTxAdvice"
    ????????????pointcut-ref
    ="demoAopBean"?/>
    ????
    </aop:config>

    ????
    <!--?事務(wù)原子?具體方法進(jìn)行什么事務(wù)?-->
    ????
    <tx:advice?id="demoTxAdvice"?transaction-manager="txManager">
    ????????
    <tx:attributes>
    ????????????
    <tx:method?name="get*"?propagation="SUPPORTS"
    ????????????????read-only
    ="true"?rollback-for="NoProductInStockException"?/>
    ????????????
    <tx:method?name="save*"?propagation="REQUIRED"
    ????????????????rollback-for
    ="NoProductInStockException"?/>
    ????????????
    <tx:method?name="update*"?propagation="REQUIRED"
    ????????????????rollback-for
    ="NoProductInStockException"?/>
    ????????????
    <tx:method?name="remove*"?propagation="REQUIRED"
    ????????????????rollback-for
    ="NoProductInStockException"?/>
    ????????????
    <tx:method?name="*"?propagation="SUPPORTS"
    ????????????????rollback-for
    ="NoProductInStockException"?/>
    ????????
    </tx:attributes>
    ????
    </tx:advice>



    ????
    <!--?daoCalss?:?extends?HibernateDaoSupport?implements?BeanDao?-->
    ????
    <bean?id="beanDao"
    ????????class
    ="demo.springHibernate.dao.imp.BeanDaoImp">
    ????????
    <property?name="sessionFactory">
    ????????????
    <ref?bean="sessionFactory"></ref>
    ????????
    </property>
    ????
    </bean>


    ????
    <bean?id="helloAction"?class="demo.struts2Spring.HelloWorld"
    ????????scope
    ="prototype">
    ????????
    <property?name="bds"?ref="beanDao"></property>
    ????
    </bean>
    ????
    </beans>


    hibernate 配置
    <?xml?version="1.0"?encoding="UTF-8"?>
    <!DOCTYPE?hibernate-configuration?PUBLIC
    ????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
    ????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
    >
    <hibernate-configuration>
    <session-factory?name="asdf">
    ????
    <property?name="hibernate.dialect">mysql</property>
    ????
    <property?name="myeclipse.connection.profile">
    ????????com.mysql.jdbc.Driver
    ????
    </property>
    ????
    <property?name="connection.url">
    ????????jdbc:mysql://localhost/aop
    ????
    </property>
    ????
    <property?name="show_sql">true</property>
    ????
    ????
    <property?name="connection.username">root</property>
    ????
    <property?name="connection.password"></property>
    ????
    <property?name="connection.driver_class">
    ????????com.mysql.jdbc.Driver
    ????
    </property>
    ????
    <property?name="dialect">
    ????????org.hibernate.dialect.MySQLDialect
    ????
    </property>
    ????
    ????
    <mapping?resource="bean/UnitBean.hbm.xml"?/>
    ????
    </session-factory>
    </hibernate-configuration>


    dao 類(接口)
    package?dao.imp;

    import?java.util.List;

    import?org.springframework.orm.hibernate3.support.HibernateDaoSupport;

    import?bean.UnitBean;

    import?dao.BeanDao;

    public?class?BeanDaoImp?extends?HibernateDaoSupport?implements?BeanDao{
    ????
    public?void?addBean(UnitBean?unitBean)?{
    ????????
    this.getHibernateTemplate().save(unitBean);
    ????}

    ????
    public?List<UnitBean>?getBeanByAll()?{
    ????????
    return?this.getHibernateTemplate().find("?from?"+UnitBean.class.getName());
    ????}

    ????
    public?void?removeBean(long?beanId)?{
    ????????
    this.getHibernateTemplate().delete(
    ????????????????getHibernateTemplate().get(UnitBean.
    class,?beanId)
    ????????????);
    ????}
    ????
    }

    Main 類
    package?unit;

    import?org.springframework.context.ApplicationContext;
    import?org.springframework.context.support.ClassPathXmlApplicationContext;

    import?dao.BeanDao;
    import?bean.UnitBean;

    public?class?Main?{
    ????
    public?static?void?main(String[]?args)?{
    ???????????ApplicationContext?ctx?
    =?new?ClassPathXmlApplicationContext("beans.xml");
    ???????????BeanDao?dao?
    =?(BeanDao)?ctx.getBean("beanDao");
    ???????????UnitBean?bean?
    =?new?UnitBean();
    ???????????bean.setName(
    "xx");
    ???????????bean.setPass(
    "11");
    ???????????dao.addBean(bean);
    ???????????
    ???????????
    for(UnitBean?unitBean?:?dao.getBeanByAll()?){
    ???????????????System.out.println(?unitBean.getId()?);
    ???????????}
    ???????????
    ???????????dao.removeBean(bean.getId());
    ???????????
    ????}
    }
    結(jié)果:
    Hibernate: insert into bean (name, pass) values (?, ?)
    Hibernate: select unitbean0_.id as id0_, unitbean0_.name as name0_, unitbean0_.pass as pass0_ from bean unitbean0_
    1
    Hibernate: select unitbean0_.id as id0_0_, unitbean0_.name as name0_0_, unitbean0_.pass as pass0_0_ from bean unitbean0_ where unitbean0_.id=?
    Hibernate: delete from bean where id=?






    主站蜘蛛池模板: 亚洲无码黄色网址| 中文字幕无线码免费人妻| 亚洲不卡av不卡一区二区| 在线中文高清资源免费观看| 久久精品电影免费动漫| 国产福利在线观看永久免费| 亚洲AV无码一区二区一二区| 亚洲手机中文字幕| 久久精品国产精品亚洲艾草网| 亚洲高清成人一区二区三区| 免费的一级片网站| 最近免费中文字幕视频高清在线看 | 亚洲人成网站看在线播放| 亚洲不卡中文字幕无码| 亚洲一区二区三区自拍公司| 亚洲成av人片不卡无码久久| 成人国产mv免费视频| 成年在线网站免费观看无广告| 91精品免费在线观看| 亚洲成人免费在线| 国产精品99久久免费观看| 在线观看片免费人成视频无码| 伊人久久大香线蕉免费视频| 三级网站免费观看| a级毛片视频免费观看| 久久国产精品免费| 国产午夜精品免费一区二区三区| 免费看少妇高潮成人片| 国产午夜无码片免费| 国产无遮挡无码视频免费软件 | 亚洲国产精品福利片在线观看| 中文亚洲成a人片在线观看| 三上悠亚亚洲一区高清| 亚洲自偷自偷偷色无码中文| 国产亚洲精品a在线观看| 自拍偷自拍亚洲精品被多人伦好爽| 国产亚洲成人久久| 亚洲国产精品va在线播放 | 最近2019免费中文字幕6| 在线观看免费av网站| 在线视频精品免费|