实体类:

  1. package com.spring.model;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5. import javax.persistence.Table;
  6.  
  7. @Entity
  8. @Table(name="t_dog")
  9. public class DogPet {
  10.  
  11. private int id;
  12. private String name;
  13. private int age;
  14. private String kind;
  15. private String sex;
  16. private String health;
  17.  
  18. @Id
  19. public int getId() {
  20. return id;
  21. }
  22. public void setId(int id) {
  23. this.id = id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. public int getAge() {
  32. return age;
  33. }
  34. public void setAge(int age) {
  35. this.age = age;
  36. }
  37. public String getKind() {
  38. return kind;
  39. }
  40. public void setKind(String kind) {
  41. this.kind = kind;
  42. }
  43. public String getSex() {
  44. return sex;
  45. }
  46. public void setSex(String sex) {
  47. this.sex = sex;
  48. }
  49. public String getHealth() {
  50. return health;
  51. }
  52. public void setHealth(String health) {
  53. this.health = health;
  54. }
  55.  
  56. public String toString()
  57. {
  58. return id+"--"+name+"--"+kind+"--"+age+"--"+health;
  59. }
  60. }

接口Service:

  1. package com.spring.service;
  2.  
  3. public interface DogPetService {
  4. public void queryAllDogPets();
  5. public void saveDogPetByDataSource();
  6. public void saveDogPetByHibernate();
  7. }

实现类ServiceImpl:

  1. package com.spring.service.impl;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import javax.annotation.Resource;
  7.  
  8. import org.springframework.stereotype.Controller;
  9.  
  10. import com.spring.service.DogPetService;
  11. import com.spring.dao.DogPetDAO;
  12. import com.spring.model.DogPet;
  13.  
  14. @Controller(value="DogPetService")
  15. public class DogPetServiceImpl implements DogPetService{
  16. private DogPetDAO dogPetDAO;
  17.  
  18. public DogPetDAO getDogPetDAO() {
  19. return dogPetDAO;
  20. }
  21.  
  22. @Resource(name="dogPetDAO")
  23. public void setDogPetDAO(DogPetDAO dogPetDAO) {
  24. this.dogPetDAO = dogPetDAO;
  25. }
  26.  
  27. @Override
  28. public void queryAllDogPets() {
  29. List<DogPet> list = dogPetDAO.queryAllDogPets();
  30. if(list != null)
  31. {
  32. for(DogPet d:list)
  33. {
  34. System.out.println(d.toString());
  35. }
  36. }
  37. }
  38.  
  39. public void saveDogPetByDataSource()
  40. {
  41. DogPet d1 = new DogPet();
  42. d1.setId(new Random().nextInt(1000));
  43. d1.setName("dog1");
  44. d1.setAge(4);
  45. d1.setKind("buladuo");
  46. d1.setSex("B");
  47. d1.setHealth("good");
  48. dogPetDAO.saveDogPetByDataSource(d1);
  49. }
  50.  
  51. public void saveDogPetByHibernate()
  52. {
  53. DogPet d1 = new DogPet();
  54. d1.setId(new Random().nextInt(1000));
  55. d1.setName("dog1");
  56. d1.setAge(4);
  57. d1.setKind("buladuo");
  58. d1.setSex("B");
  59. d1.setHealth("good");
  60. dogPetDAO.saveDogPetByHibernate(d1);
  61. }
  62. }

Service调用的DAO:

  1. package com.spring.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import javax.annotation.Resource;
  10. import javax.sql.DataSource;
  11.  
  12. import org.hibernate.Session;
  13. import org.hibernate.SessionFactory;
  14. import org.springframework.stereotype.Service;
  15.  
  16. import com.spring.model.DogPet;
  17.  
  18. @Service(value="dogPetDAO")
  19. public class DogPetDAO {
  20.  
  21. private DataSource dataSource;
  22. private SessionFactory sessionFactory;
  23.  
  24. public List<DogPet> queryAllDogPets()
  25. {
  26. List<DogPet> list = new ArrayList<DogPet>();
  27.  
  28. DogPet d1 = new DogPet();
  29. d1.setId(1111);
  30. d1.setName("dog1");
  31. d1.setAge(4);
  32. d1.setKind("buladuo");
  33. d1.setSex("B");
  34. d1.setHealth("good");
  35. DogPet d2 = new DogPet();
  36. d2.setId(2222);
  37. d2.setName("dog2");
  38. d2.setAge(3);
  39. d2.setKind("buladuo");
  40. d2.setSex("G");
  41. d2.setHealth("good");
  42.  
  43. list.add(d1);
  44. list.add(d2);
  45.  
  46. return list;
  47. }
  48.  
  49. public void saveDogPetByDataSource(DogPet dog)
  50. {
  51. Connection connection = null;
  52. try
  53. {
  54. connection = dataSource.getConnection() ;
  55. PreparedStatement pstmt = connection.prepareStatement("insert into t_dog values(?,?,?,?,?,?)");
  56. pstmt.setInt(1, dog.getId());
  57. pstmt.setString(2, dog.getName());
  58. pstmt.setInt(3, dog.getAge());
  59. pstmt.setString(4, dog.getKind());
  60. pstmt.setString(5, dog.getSex());
  61. pstmt.setString(6, dog.getHealth());
  62. pstmt.execute();
  63. }
  64. catch (SQLException e)
  65. {
  66. e.printStackTrace();
  67. }
  68. finally
  69. {
  70. try
  71. {
  72. connection.close();
  73. }
  74. catch (SQLException e)
  75. {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80.  
  81. /*public void saveDogPetByHibernate(DogPet dog)
  82. {
  83. Session session = null;
  84. try
  85. {
  86. session = sessionFactory.openSession();
  87. session.beginTransaction();
  88. session.save(dog);
  89. session.getTransaction().commit();
  90. }
  91. catch (Exception e)
  92. {
  93. e.printStackTrace();
  94. }
  95. finally
  96. {
  97. session.close();
  98. }
  99. }*/
  100.  
  101. public void saveDogPetByHibernate(DogPet dog)
  102. {
  103. Session session = sessionFactory.getCurrentSession();
  104. session.save(dog);
  105. }
  106.  
  107. public DataSource getDataSource() {
  108. return dataSource;
  109. }
  110.  
  111. @Resource
  112. public void setDataSource(DataSource dataSource) {
  113. this.dataSource = dataSource;
  114. }
  115.  
  116. public SessionFactory getSessionFactory() {
  117. return sessionFactory;
  118. }
  119.  
  120. @Resource
  121. public void setSessionFactory(SessionFactory sessionFactory) {
  122. this.sessionFactory = sessionFactory;
  123. }
  124. }

配置文件beans.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  10. <context:annotation-config/>
  11.  
  12. <context:component-scan base-package="com.spring"></context:component-scan>
  13.  
  14. </beans>

配置文件dao.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  12. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  13.  
  14. <!-- 利用xml配置声明式事物管理 begin-->
  15. <bean
  16. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  17. <property name="locations">
  18. <value>classpath:jdbc.properties</value>
  19. </property>
  20. </bean>
  21.  
  22. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  23. destroy-method="close">
  24. <property name="driverClassName" value="${driver}" />
  25. <property name="url" value="${url}" />
  26. <property name="username" value="${user}" />
  27. <property name="password" value="${password}" />
  28. </bean>
  29.  
  30. <bean id="sessionFactory"
  31. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource" />
  33. <property name="annotatedClasses">
  34. <list>
  35. <value>com.spring.model.DogPet</value>
  36. </list>
  37. </property>
  38. <property name="hibernateProperties">
  39. <value>
  40. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  41. </value>
  42. </property>
  43. </bean>
  44.  
  45. <bean id="txManager"
  46. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  47. <property name="sessionFactory" ref="sessionFactory" />
  48. </bean>
  49.  
  50. <tx:advice id="txAdvice" transaction-manager="txManager">
  51. <tx:attributes>
  52. <tx:method name="query*" read-only="true" />
  53. <tx:method name="*" />
  54. </tx:attributes>
  55. </tx:advice>
  56.  
  57. <aop:config>
  58. <aop:pointcut id="dogPetServiceOperation"
  59. expression="execution(* com.spring..*(..))" />
  60. <aop:advisor advice-ref="txAdvice" pointcut-ref="dogPetServiceOperation" />
  61. </aop:config>
  62.  
  63. <!-- 利用xml配置声明式事物管理 end-->
  64. </beans>

test类:

  1. package com.spring.test;
  2.  
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. import com.spring.service.DogPetService;
  8.  
  9. public class ServiceTest {
  10.  
  11. @Test
  12. public void saveDogPetByHibernate()
  13. {
  14. ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml","dao.xml"});
  15. DogPetService dogPetService = (DogPetService)ctx.getBean("DogPetService");
  16. dogPetService.saveDogPetByHibernate();
  17. }
  18.  
  19. }

spring_150901_hibernate_transaction_xml的更多相关文章

随机推荐

  1. signal和sigaction 分析

    1:signal 函数 原型: sighandler_t signal(int signum, sighandler_t handler)      typedef void (*sighandler ...

  2. ZABBIX 3.0 配置监控MYSQL性能【OK】

    Zabbix3.0自带了MySQL插件来监控mysql数据库的模板,只需要配置好agent客户端,然后在web端给主机增加模板就行了. 参考:http://www.cnblogs.com/keving ...

  3. 算法专题-STL篇

    这篇文章着重记录c++中STL的用法.主要粗略的介绍其用法,以知识点的形式呈现其功能,不会深入源码分析其工作原理. 排序和检索. sort(a,a+n),对a[0]往后的n个元素(包括a[0])进行排 ...

  4. HTML入门(三)后台系统显示页面_框架标签

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. C11工具类:字符转换

    1.数值类型和字符串转换 1.1 数值转换为字符 std::string to_string(int value); std::string to_string(long value); std::s ...

  6. Counting Pair

    Counting Pair Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 112 Tried: 1209 Submit Status Best ...

  7. 超酷动态图片展示墙JS特效制作方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. rxjs自定义operator

    rxjs自定义operator

  9. 【BZOJ】1040: [ZJOI2008]骑士 环套树DP

    [题意]给定n个人的ai和bi,表示第i个人能力值为ai且不能和bi同时选择,求能力值和最大的选择方案.n<=10^6. [算法]环套树DP(基环树) [题解]n个点n条边——基环森林(若干环套 ...

  10. Docker 配置国内镜像拉取中心,Configure docker to use faster registries in China.

    Networking in China is really bad when it comes to using some cloud based tools like docker, it's us ...