1. 程式人生 > >Hibernate環境maven搭建

Hibernate環境maven搭建

1     配置pom檔案

<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>com.train</groupId>
  <artifactId>bookstore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
    <dependencies>
<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    
    <!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>

<!-- Hibernate framework -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>3.6.10.Final</version>
</dependency>


<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<!-- Hibernate library dependecy start -->
<!-- 新增Log4J依賴 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.6.4</version>
        </dependency>
 
        <!-- 新增javassist -->
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.0.GA</version>
        </dependency>
<!-- Hibernate library dependecy end -->
  </dependencies>

</project>

2. 配置web.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>

3.配置hibernate.cfg.xml檔案

<?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>
     
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">88888888</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore_db</property>
        <property name="hibernate.connection.username">root</property> 
        <property name="show_sql">true</property>
      <mapping resource="com/train/bookstore/domain/Publisher1_1.hbm.xml"></mapping>
      
             
    </session-factory>
</hibernate-configuration>

4.  配置habernateUtil.java檔案

package com.train.bookstore.util;


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {


private static final SessionFactory sessionFactory = buildSessionFactory();


    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }


    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
    }


}

5.建立一個簡單的Javabean檔案

package com.train.bookstore.util;


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {


private static final SessionFactory sessionFactory = buildSessionFactory();


    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }


    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
    }


}

6.配置其對應的 class.hbm.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.train.bookstore.domain">
<class name="Publisher1_1" table="publisher1_1">
<id name="code" type="string" column="pub_code" />
<property name="name" type="string" column="pub_NAME" />
<property name="address" type="string" column="pub_address"/>
</class>
</hibernate-mapping>

7.建立見得demo類進行傳輸提交

package com.train.bookstore;


import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.train.bookstore.domain.Publisher1_1;
import com.train.bookstore.util.HibernateUtil;


public class Demo {


public static void main(String[] args) {

Session session = HibernateUtil.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();
Publisher1_1 publisher = (Publisher1_1)session.load(Publisher1_1.class, "pub001");

publisher.setName("abc pub");
publisher.setAddress("shenzhen");
session.update(publisher);

//Publisher1_1 pub = (Publisher1_1)session.load(Publisher1_1.class, "pub005");
//session.delete(pub);
tran.commit();


//Publisher1_1 publisher = (Publisher1_1)session.get(Publisher1_1.class, "pub001");
//System.out.println(publisher);
//Query query = session.createQuery("from Publisher1_1");//hql
//List<Publisher1_1> publisers = query.list();
//for (Publisher1_1 publisher1_1 : publisers) {
//System.out.println(publisher1_1);
//}
//




}


}