您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        (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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。