您好,登錄后才能下訂單哦!
(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; } @Override public 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; @Before public void init() { sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); } @Test public 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(); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。