<?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>

<!-- 以下為跟資料庫連線之相關設定 ,由beans.xml取代-->

<!--         <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> -->

<!--         <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;database=java</property> -->

<!--         <property name="hibernate.connection.username">sa</property> -->

<!--         <property name="hibernate.connection.password">passw0rd</property> -->

       

       

        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

 

    <!-- ↓要用getCurrentSession要把她跟thread連在一起 -->

        <property name="hibernate.current_session_context_class">thread</property>

        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

    <!-- ↑此為預設,不重要 -->

        <property name="hibernate.show_sql">true</property>

 

    <!-- ↓注意這是mapping -->

        <mapping resource="model/CustomerBean.hbm.xml" />

        <mapping resource="model/ProductBean.hbm.xml" />

    </session-factory>

</hibernate-configuration>



<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 

 

    <bean id="dataSource"

       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />

       <property name="url"

           value="jdbc:sqlserver://localhost:1433;database=java"></property>

       <property name="username" value="sa"></property>

       <property name="password" value="passw0rd"></property>

    </bean>

 

    <!-- 用以取代hibernate.cfg.xml檔的 方法一: -->

    <!-- LocalSessionFactoryBean 中有一個方法 destroy() -->

    <bean id="sessionFactory" destroy-method="destroy"

       class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

       <property name="dataSource" ref="dataSource"></property>

       <property name="configLocation">

           <value>classpath:hibernate.cfg.xml</value>

       </property>

    </bean>

 

    <!-- 方法二之一: -->

    <!-- <bean id="sessionFactory" destroy-method="destroy" -->

    <!-- class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> -->

    <!-- <property name="dataSource" ref="dataSource"></property> -->

    <!-- <property name="mappingResources"> --><!-- setMappingResourcesmap到需要產生的hbm.xml -->

    <!-- <list> -->

    <!-- <value>model/CustomerBean.hbm.xml</value> -->

    <!-- <value>model/ProductBean.hbm.xml</value> -->

    <!-- </list> -->

    <!-- </property> -->

    <!-- </bean> -->

 

    <!-- 方法二之二 -->

    <!-- <bean id="sessionFactory" destroy-method="destroy" -->

    <!-- class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> -->

    <!-- <property name="dataSource" ref="dataSource"></property> -->

    <!-- <property name="hibernateProperties"> -->

    <!-- <props> -->

    <!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> -->

    <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->

    <!-- <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> -->

    <!-- <prop key="hibernate.show_sql">true</prop> -->

    <!-- </props> -->

    <!-- </property> -->

    <!-- </bean> -->

 

 

    <bean id="CustomerDAOHibernate" class="model.dao.CustomerDAOHibernate">

       <constructor-arg ref="sessionFactory"></constructor-arg>

    </bean>

    <bean id="CustomerService" class="model.CustomerService">

       <constructor-arg ref="CustomerDAOHibernate"></constructor-arg>

    </bean>

    <bean id="ProductDAOHibernate" class="model.dao.ProductDAOHibernate">

       <constructor-arg ref="sessionFactory"></constructor-arg>

    </bean>

    <bean id="ProductService" class="model.ProductService">

       <constructor-arg ref="ProductDAOHibernate"></constructor-arg>

    </bean>

</beans>



package model;

 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

//import model.misc.HibernateUtil;

 

public class ProductBean {

    private int id;

    private String name;

    private double price;

    private java.util.Date make;

    private int expire;

 

    public static void main(String[] args) {

       ApplicationContext context = new ClassPathXmlApplicationContext(

              "beans.xml");

       SessionFactory sessionFactory = (SessionFactory) context

              .getBean("sessionFactory");

      

       try {

           Session session=sessionFactory.getCurrentSession();

           session.beginTransaction();

           ProductBean select = (ProductBean) session.load(ProductBean.class, 10);

           System.out.println(select);

       } finally {

           ((ConfigurableApplicationContext) context).close();

       }

 

       // try {

       // HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

        // Session session =

       // HibernateUtil.getSessionFactory().getCurrentSession();

       //

       // ProductBean select = (ProductBean) session.load(ProductBean.class,

       // 1);

       // System.out.println(select);

       // //

       // //

       // //

       //

       // HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();

       // } finally {

       // HibernateUtil.closeSessionFactory();

       // }

    }

 

    @Override

    public String toString() {

       return "model.ProductBean [" + id + "," + name + "," + price + ","

              + make + "," + expire + "]";

    }

 

    public int getId() {

       return id;

    }

 

    public void setId(int id) {

       this.id = id;

    }

 

    public String getName() {

       return name;

    }

 

    public void setName(String name) {

       this.name = name;

    }

 

    public double getPrice() {

       return price;

    }

 

    public void setPrice(double price) {

       this.price = price;

    }

 

    public java.util.Date getMake() {

       return make;

    }

 

    public void setMake(java.util.Date make) {

       this.make = make;

    }

 

    public int getExpire() {

       return expire;

    }

 

    public void setExpire(int expire) {

       this.expire = expire;

    }

 

    @Override

    public boolean equals(Object obj) {

       if (obj != null && (obj instanceof ProductBean)) {

           ProductBean temp = (ProductBean) obj;

           if (this.id == temp.id) {

              return true;

           }

       }

       return false;

    }

}



package model.dao;

 

import java.util.Date;

import java.util.List;

 

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import model.ProductBean;

import model.ProductDAO;

public class ProductDAOHibernate implements ProductDAO {

    private SessionFactory sessionFactory = null;

 

    public ProductDAOHibernate(SessionFactory sessionFactory) {

       this.sessionFactory = sessionFactory;

    }

 

    public static void main(String[] args) {

       ApplicationContext context = new ClassPathXmlApplicationContext(

              "beans.xml");

       SessionFactory sessionFactory = (SessionFactory) context

              .getBean("sessionFactory");

      

       try {

           sessionFactory.getCurrentSession().beginTransaction();

            ProductDAO dao =(ProductDAO) context.getBean("ProductDAOHibernate");

            //new ProductDAOHibernate(HibernateUtil.getSessionFactory());

            ProductBean bean = dao.select(2);

            System.out.println(bean);

          

           sessionFactory.getCurrentSession().getTransaction().commit();

       } finally {

           //close這個就不用特別close sessionFactory

           ((ConfigurableApplicationContext) context).close();

       }

 

 

 

       // try {

       // HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

       //

       // ProductDAOHibernate dao =

       // new ProductDAOHibernate(HibernateUtil.getSessionFactory());

       // ProductBean bean = dao.select(1);

       // System.out.println(bean);

       //

       // HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();

       // } finally {

       // HibernateUtil.closeSessionFactory();

       // }

    }

 

    public Session getSession() {

       return sessionFactory.getCurrentSession();

    }

 

    @Override

    public ProductBean select(int id) {

       return (ProductBean) this.getSession().get(ProductBean.class, id);

    }

 

    @Override

    public List<ProductBean> select() {

       Query query = this.getSession().createQuery("from ProductBean");

       return (List<ProductBean>) query.list();

    }

 

    @Override

    public ProductBean insert(ProductBean bean) {

       ProductBean result = (ProductBean) this.getSession().get(

              ProductBean.class, bean.getId());

       if (result == null) {

           this.getSession().save(bean);

           return bean;

       }

       return null;

    }

 

    @Override

    public ProductBean update(String name, double price, Date make, int expire,

           int id) {

       ProductBean result = (ProductBean) this.getSession().get(

              ProductBean.class, id);

       if (result != null) {

           result.setName(name);

           result.setPrice(price);

           result.setMake(make);

           result.setExpire(expire);

       }

       return result;

    }

 

    @Override

    public boolean delete(int id) {

       ProductBean bean = (ProductBean) this.getSession().get(

              ProductBean.class, id);

       if (bean != null) {

           this.getSession().delete(bean);

           return true;

       }

       return false;

    }}



package model;

 

import java.util.ArrayList;

import java.util.List;

 

import org.hibernate.SessionFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import model.dao.ProductDAOHibernate;

//import model.misc.HibernateUtil;

 

public class ProductService {

    private ProductDAO productDao;

    public ProductService(ProductDAO dao) {

       this.productDao = dao;

    }

    public static void main(String[] args) {

      

       ApplicationContext context = new ClassPathXmlApplicationContext(

              "beans.xml");

       SessionFactory sessionFactory = (SessionFactory) context

              .getBean("sessionFactory");

      

       try {

           sessionFactory.getCurrentSession().beginTransaction();

           ProductService service = (ProductService) context.getBean("ProductService");

           List<ProductBean> beans = service.select(null);

           System.out.println("beans="+beans);

          

           sessionFactory.getCurrentSession().getTransaction().commit();

       } finally {

           //close這個就不用特別close sessionFactory

           ((ConfigurableApplicationContext) context).close();

       }

//     try {

//         HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

//

//         ProductDAO dao = new ProductDAOHibernate(HibernateUtil.getSessionFactory());

//         ProductService service = new ProductService(dao);

//         List<ProductBean> beans = service.select(null);

//         System.out.println("beans="+beans);

//

//        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();

//     } finally {

//         HibernateUtil.closeSessionFactory();

//     }

    }

    public List<ProductBean> select(ProductBean bean) {

       List<ProductBean> result = null;

       if(bean!=null && bean.getId()!=0) {

           ProductBean temp = productDao.select(bean.getId());

           if(temp!=null) {

              result = new ArrayList<ProductBean>();

              result.add(temp);

           }

       } else {

           result = productDao.select();

       }

       return result;

    }

    public ProductBean insert(ProductBean bean) {

       ProductBean result = null;

       if(bean!=null) {

           result = productDao.insert(bean);

       }

       return result;

    }

    public ProductBean update(ProductBean bean) {

       ProductBean result = null;

       if(bean!=null) {

           result = productDao.update(bean.getName(), bean.getPrice(),

                  bean.getMake(), bean.getExpire(), bean.getId());

       }

       return result;

    }

    public boolean delete(ProductBean bean) {

       boolean result = false;

       if(bean!=null) {

           result = productDao.delete(bean.getId());

       }

       return result;

    }

 

}



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

 

    <groupId>org.iiiedu.samuel</groupId>

    <artifactId>ProjHibernate</artifactId>

    <version>1.0</version>

    <packaging>jar</packaging>

 

    <name>ProjHibernate</name>

    <url>http://maven.apache.org</url>

 

    <properties>

       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

       <java-version>1.8</java-version>

    </properties>

 

    <dependencies>

   

       <!-- http://mvnrepository.com/artifact/org.springframework/spring-orm -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-orm</artifactId>

           <version>4.2.6.RELEASE</version>

       </dependency>

 

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context</artifactId>

           <version>4.2.6.RELEASE</version>

       </dependency>

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-jdbc</artifactId>

           <version>4.2.6.RELEASE</version>

       </dependency>

       <dependency>

           <groupId>org.hibernate</groupId>

           <artifactId>hibernate-core</artifactId>

           <version>5.2.0.Final</version>

       </dependency>

       <dependency>

           <groupId>com.microsoft.sqlserver.jdbc</groupId>

           <artifactId>sqljdbc</artifactId>

           <version>1.0</version>

       </dependency>

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>4.12</version>

           <scope>test</scope>

       </dependency>

    </dependencies>

    <build>

       <plugins>

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-compiler-plugin</artifactId>

              <version>3.5</version>

              <configuration>

                  <source>${java-version}</source>

                  <target>${java-version}</target>

              </configuration>

           </plugin>

       </plugins>

    </build>

</project>

 

創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()