欢迎光临
我们一直在努力

SSH系列:(3)Hibernate

(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();

	}
}

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。