標籤:ssh
(1)引入jar包
(2)配置
(3)測試
1、引入jar包
|
引入mysql jar包
mysql-connector-java-5.1.38-bin.jar |
|
引入c3p0 jar包
c3p0-0.9.1.2.jar |
|
引入hibernate相關jar包 (hibernate-distribution-3.6.0.Final)
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
|
2、配置
2.1、添加實體類:Person.java
package com.rk.test.entity;/** * 實體層Person類 DTO * * */public class Person {private String pId;private String pName;private int pVersion;public String getpId() {return pId;}public void setpId(String pId) {this.pId = pId;}public String getpName() {return pName;}public void setpName(String pName) {this.pName = pName;}public int getpVersion() {return pVersion;}public void setpVersion(int pVersion) {this.pVersion = pVersion;}@Overridepublic String toString() {return "Person [pId=" + pId + ", pName=" + pName + ", pVersion="+ pVersion + "]";}}
2.2、添加對應檔:Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.rk.test.entity" auto-import="true"><class name="Person" table="T_Person"><id name="pId" column="id" type="string" length="32"><generator class="uuid.hex"></generator></id><version name="pVersion" column="version" type="integer"></version><property name="pName" column="name" type="string"></property></class></hibernate-mapping>
2.3、添加Hibernate設定檔:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <!-- 通常,一個session-factory節點代表一個資料庫 --> <session-factory> <!-- 1. 資料庫連接配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///tax_sys</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property><!-- 資料庫方言配置, hibernate在啟動並執行時候,會根據不同的方言產生符合當前資料庫文法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相關配置 --><!-- 2.1 顯示hibernate在運行時候執行的sql語句 --><property name="hibernate.show_sql">true</property><!-- 2.2 格式化sql --><property name="hibernate.format_sql">false</property><!-- 2.3 自動建表 --><property name="hibernate.hbm2ddl.auto">update</property><!-- 配置session的建立方式:線程方式建立session對象 --><property name="hibernate.current_session_context_class">thread</property><!-- 3. 載入所有映射--><mapping resource="com/rk/test/entity/Person.hbm.xml"/> </session-factory></hibernate-configuration>
3、測試
測試兩方面:第一是能從資料庫讀取一條資料,第二是能向資料庫儲存一條資料
package com.rk.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Before;import org.junit.Test;import com.rk.test.entity.Person;public class TestHibernate {private SessionFactory sf;@Beforepublic void init(){sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();}@Testpublic void test(){Session session = sf.getCurrentSession();session.beginTransaction();Person p = (Person) session.get(Person.class, "4028d081564a762001564a76221e0000");System.out.println(p);Person p2 = new Person();p2.setpName("Tomcat");session.save(p2);session.getTransaction().commit();}}
SSH系列:(3)Hibernate