SSH系列:(3)Hibernate
短信预约 -IT技能 免费直播动态提醒
(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;
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();
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341