新增hibernate的相關jar包!

實體類:

  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 DataSource getDataSource() {
  102. return dataSource;
  103. }
  104.  
  105. @Resource
  106. public void setDataSource(DataSource dataSource) {
  107. this.dataSource = dataSource;
  108. }
  109.  
  110. public SessionFactory getSessionFactory() {
  111. return sessionFactory;
  112. }
  113.  
  114. @Resource
  115. public void setSessionFactory(SessionFactory sessionFactory) {
  116. this.sessionFactory = sessionFactory;
  117. }
  118.  
  119. }

配置檔案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. <bean
  15. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  16. <property name="locations">
  17. <value>classpath:jdbc.properties</value>
  18. </property>
  19. </bean>
  20.  
  21. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  22. destroy-method="close">
  23. <property name="driverClassName" value="${driver}" />
  24. <property name="url" value="${url}" />
  25. <property name="username" value="${user}" />
  26. <property name="password" value="${password}" />
  27. </bean>
  28.  
  29. <bean id="sessionFactory"
  30. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  31. <property name="dataSource" ref="dataSource" />
  32. <property name="annotatedClasses">
  33. <list>
  34. <value>com.spring.model.DogPet</value>
  35. </list>
  36. </property>
  37. <property name="hibernateProperties">
  38. <value>
  39. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  40. </value>
  41. </property>
  42. </bean>
  43. </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.saveDogPetByDataSource();
  17. //dogPetService.saveDogPetByHibernate();
  18. dogPetService.saveDogPetByHibernate();
  19. }
  20. }