Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/time_tracker" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <!-- Hibernate 3 XML SessionFactory Bean definition-->
- <!-- <bean id="hibernate3SessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingResources">
- <list>
- <value>person.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
- </value>
- </property>
- </bean> -->
- <!-- Hibernate 3 Annotation SessionFactory Bean definition-->
- <bean id="hibernate4AnnotatedSessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="annotatedClasses">
- <list>
- <value>TestItem</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="hibernate.current_session_context_class">thread</prop>
- <prop key="hibernate.show_sql">false</prop>
- </props>
- </property>
- </bean>
- <bean id="testItemDAO" class="TestItemDAOImpl">
- <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
- </bean>
- </beans>
- ---------------------------------------------------------------------------------------------
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import java.util.List;
- public class TestItemDAOImpl implements TestItemDAO {
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- public void save(TestItem t) {
- Session session = this.sessionFactory.openSession();
- Transaction tx = session.beginTransaction();
- session.persist(t);
- tx.commit();
- session.close();
- }
- @SuppressWarnings("unchecked")
- public List<TestItem> list() {
- Session session = this.sessionFactory.openSession();
- List<TestItem> itemList = session.createQuery("from TestItem").list();
- session.close();
- return itemList;
- }
- }
- ---------------------------------------------------------------------------------------------
- import javax.persistence.*;
- import java.sql.Date;
- @Entity
- @Table(name="test")
- public class TestItem {
- @Id
- @Column(name="id")
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- private String value1;
- private Date value2;
- public Date getValue2() {
- return value2;
- }
- public void setValue2(Date value2) {
- this.value2 = value2;
- }
- public String getValue1() {
- return value1;
- }
- public void setValue1(String value1) {
- this.value1 = value1;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
- ---------------------------------------------------------------------------------------------
- import java.sql.Date;
- import java.util.List;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class HelloController {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
- TestItemDAO itemDAO = context.getBean(TestItemDAO.class);
- TestItem item = new TestItem();
- item.setId(0);
- item.setValue1("dummy_value");
- item.setValue2(new Date(20150809));
- itemDAO.save(item);
- System.out.println("Person::"+item);
- List<TestItem> list = itemDAO.list();
- for(TestItem p : list){
- System.out.println("Person List::"+p);
- }
- //close resources
- context.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement