目录结构如图,
1.用MyEclipse建立一个Maven-Java项目,然后给出pom配置,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xuebaosoft.hibernate4</groupId>
<artifactId>hibernate4-maven-conf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hibernate4-maven-conf</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.8.Final</version>
</dependency>
<!-- Hibernate uses jboss-logging for logging, for the tutorials we will
use the sl4fj-simple backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
2.环境准备好以后写一个pojo-UserModel.java,
package modelTest;
public class UserModel {
private String uuid;
private int userId;
private String name;
private int age;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.配置hibernate数据源环境xml--hibernate.cfg.xml,
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.191.1:3306/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="UserModel.hbm.xml"/>
</session-factory>
</hibernate-configuration>
解释下:
show_sql为true表示在执行数据库操作的时候sql语句会以log的形式打印在console;
hbm2ddl.auto为create表示先删除指定的实体-关系表(不管存在不存在都先执行删除操作),然后再进行操作,这个也算是hibernate的主要实用优势之一,不用你写sql建表了。
mapping这里对应了实体-关系表的配置文件
4.实体-关系表的配置--UserModel.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
'-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
<hibernate-mapping>
<class name="modelTest.UserModel" table="tbl_user">
<id name="userId">
<generator class="native" />
</id>
<property name="uuid"></property>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
解释下,
name为modelTest.UserModel对应的是那个POJO,table为tbl_user表示对应数据库的那个表
id标识userId字段为主键,其余的都不是主键,generator为native或者是increment表示自增,这里有篇帖子希望了解区别的可以参考下:**(帖子我忘了,找个机会补),总之native是本地化的方案,可能会使用increment进行代替,可以理解为native是一个抽象父类,increment是一个实现它的具体类,但具体类不止increment一个,这里自增主键用native和increment都是可以的(官方demo用的就是increment)
这样配置好以后就可以进行测试了。
5.使用JUnit进行测试,
package foo;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import modelTest.UserModel;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class AppTest extends TestCase {
private SessionFactory sessionFactory;
public AppTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
protected void tearDown() throws Exception {
if (sessionFactory != null) {
sessionFactory.close();
}
}
public static Test suite() {
return new TestSuite(AppTest.class);
}
public void testApp() {
UserModel um = new UserModel();
um.setUuid("1");
um.setName("name1");
um.setAge(1);
Session s = sessionFactory.openSession();
Transaction t = s.beginTransaction();
s.save(um);
t.commit();
}
}
6.分析一下,
首先数据库mysql会删除tbl_user这张表,然后重建,之后就是插入一条记录
关键代码也就是
sessionFactory = new Configuration().configure().buildSessionFactory();
然后用这个sessionFactory去open一个session,
Session s = sessionFactory.openSession();
再之后就用这个session利用hibernate操作数据库,就是这么一个过程,
Transaction t = s.beginTransaction();
s.save(um);
t.commit();
其中这个POJO就是让这个session的save使用的,这样就会在ORM的框架下插入一条记录。
文章的意义也就是记录下Hibernate4用的哪些Jar包,还有基本的配置过程。
来源:oschina
链接:https://my.oschina.net/u/555061/blog/506049