Advertisement
Guest User

SpringTestExample

a guest
Mar 31st, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  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:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  8.  
  9. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  10. destroy-method="close">
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  12. <property name="url" value="jdbc:mysql://localhost:3306/time_tracker" />
  13. <property name="username" value="root" />
  14. <property name="password" value="root" />
  15. </bean>
  16.  
  17. <!-- Hibernate 3 XML SessionFactory Bean definition-->
  18. <!-- <bean id="hibernate3SessionFactory"
  19. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  20. <property name="dataSource" ref="dataSource" />
  21. <property name="mappingResources">
  22. <list>
  23. <value>person.hbm.xml</value>
  24. </list>
  25. </property>
  26. <property name="hibernateProperties">
  27. <value>
  28. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  29. </value>
  30. </property>
  31. </bean> -->
  32.  
  33. <!-- Hibernate 3 Annotation SessionFactory Bean definition-->
  34. <bean id="hibernate4AnnotatedSessionFactory"
  35. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  36. <property name="dataSource" ref="dataSource" />
  37. <property name="annotatedClasses">
  38. <list>
  39. <value>TestItem</value>
  40. </list>
  41. </property>
  42. <property name="hibernateProperties">
  43. <props>
  44. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  45. <prop key="hibernate.current_session_context_class">thread</prop>
  46. <prop key="hibernate.show_sql">false</prop>
  47. </props>
  48. </property>
  49. </bean>
  50.  
  51. <bean id="testItemDAO" class="TestItemDAOImpl">
  52. <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
  53. </bean>
  54. </beans>
  55.  
  56. ---------------------------------------------------------------------------------------------
  57.  
  58. import org.hibernate.Session;
  59. import org.hibernate.SessionFactory;
  60. import org.hibernate.Transaction;
  61.  
  62. import java.util.List;
  63.  
  64. public class TestItemDAOImpl implements TestItemDAO {
  65.  
  66. private SessionFactory sessionFactory;
  67.  
  68. public void setSessionFactory(SessionFactory sessionFactory) {
  69. this.sessionFactory = sessionFactory;
  70. }
  71.  
  72. public void save(TestItem t) {
  73. Session session = this.sessionFactory.openSession();
  74. Transaction tx = session.beginTransaction();
  75. session.persist(t);
  76. tx.commit();
  77. session.close();
  78. }
  79.  
  80. @SuppressWarnings("unchecked")
  81. public List<TestItem> list() {
  82.  
  83. Session session = this.sessionFactory.openSession();
  84. List<TestItem> itemList = session.createQuery("from TestItem").list();
  85. session.close();
  86. return itemList;
  87. }
  88. }
  89.  
  90. ---------------------------------------------------------------------------------------------
  91.  
  92. import javax.persistence.*;
  93. import java.sql.Date;
  94.  
  95. @Entity
  96. @Table(name="test")
  97. public class TestItem {
  98.  
  99. @Id
  100. @Column(name="id")
  101. @GeneratedValue(strategy=GenerationType.IDENTITY)
  102. private int id;
  103.  
  104. private String value1;
  105. private Date value2;
  106.  
  107. public Date getValue2() {
  108. return value2;
  109. }
  110.  
  111. public void setValue2(Date value2) {
  112. this.value2 = value2;
  113. }
  114.  
  115. public String getValue1() {
  116. return value1;
  117. }
  118.  
  119. public void setValue1(String value1) {
  120. this.value1 = value1;
  121. }
  122.  
  123. public int getId() {
  124. return id;
  125. }
  126.  
  127. public void setId(int id) {
  128. this.id = id;
  129. }
  130. }
  131.  
  132. ---------------------------------------------------------------------------------------------
  133.  
  134. import java.sql.Date;
  135. import java.util.List;
  136.  
  137. import org.springframework.context.support.ClassPathXmlApplicationContext;
  138.  
  139. public class HelloController {
  140.  
  141. public static void main(String[] args) {
  142.  
  143. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  144.  
  145. TestItemDAO itemDAO = context.getBean(TestItemDAO.class);
  146.  
  147. TestItem item = new TestItem();
  148. item.setId(0);
  149. item.setValue1("dummy_value");
  150. item.setValue2(new Date(20150809));
  151.  
  152. itemDAO.save(item);
  153.  
  154. System.out.println("Person::"+item);
  155.  
  156. List<TestItem> list = itemDAO.list();
  157.  
  158. for(TestItem p : list){
  159. System.out.println("Person List::"+p);
  160. }
  161. //close resources
  162. context.close();
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement