1.导入jar包
1.1导入Hibernate相关jar包
此处使用maven来获得jar包,在pom.xml中添加的依赖为
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
1.2导入数据库连接jar包
此处使用mysql,在pom.xml中添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
2.编写hibernate总配置文件:hibernate.cfg.xml
2.1结构分析
2.1.1dtd信息
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
2.1.2数据库连接信息
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
2.1.3方言(也就是使用哪种数据库)
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
2.1.4其他配置信息
显示sql:
<property name="show_sql">true</property>
格式化sql显示:
<property name="hibernate.format_sql">true</property>
事务自动提交: <property name="connection.autocommit">true</property>
事务自动提交: <property name="connection.autocommit">true</property>
通过配置文件生成数据库表:
<!--
create:先删除表,再创建
update:如果没有则创建,如果有则更新(适合开发时候使用)
create-drop:初始化时创建表,SessionFactory执行close()时删除表
validate:验证表结构是否一致,如果不一致,就抛异常
-->
<property name="hibernate.hbm2ddl.auto">update</property>
2.1.5实体映射文件声明
<mapping resource="cn/com/cpf/pojo/User.hbm.xml"/>
一般
和pojo也在同一个包中,所以在此处写路径可以直接复制pojo的qualified name
2.2配置例子
<?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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="jdbc.batch_size">20</property>
<property name="connection.autocommit">true</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<mapping resource="cn/com/cpf/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
一般放在src/main/resources目录下
3.编写实体映射文件*.hbm.xml
作用:就是为了实现实体bean和数据库表之间的映射
<?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="cn.com.cpf.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
4.编写实体pojo
public class User {
private int id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
5.单元测试
<span style="white-space:pre"> </span>@Test
public void testHibernate()
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user.setName("vvv");
session.save(user);
User u = (User) session.get(User.class, 1);
System.out.println(u.getName());
tx.commit();
session.close();
}