一. 搭建環境
創建一個工程,需要到如以下的jar包:
還需要配置文件log4j.properties和hibernate.cfg.xml。
這個是hibernate.cfg.xml的內容:
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?createDatabaseIfNotExist=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="org/hibernate/test/legacy/Simple.hbm.xml" />
</session-factory>
</hibernate-configuration>
dialect和show_sql是指定了顯示什麼方言和在控制檯顯示sql語句。二. 第一個程序
創建實體類User,具有以下屬性:
private int id;
private String username;
private String sex;
private int age;
private Date birthday;
private double salary;
創建User.hbm.xml文件,將其放在與實體同一級目錄下:<?xml version="1.0"?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:
GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the
lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.abstractembeddedcomponents.cid">
<class name="com.thr.hibernate.entity.User" table="T_USER">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username" />
<property name="sex" />
<properties name="age" />
<property name="birthday" />
<property name="salary" />
</class>
</hibernate-mapping>
class配置實體類的全名,table配置數據庫表明,id表示的是主鍵,其它的是User的屬性。最後將創建好的User.hbm.xml配置到hibernate.cfg.xml文件中:
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/thr/hibernate/entity/User.hbm.xml" />
配置hbm2ddl.auto的參數update表示如果表不存在的情況下,自動創建表;存在的話就自動更新。創建測試類測試一下:
1. 添加
public void test1() {
User user = new User();
user.setUsername("張三");
user.setSex("男");
user.setAge(27);
user.setBirthday(new Date());
user.setSalary(100.5);
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 創建SessionFactory(創建連接池)
SessionFactory factory = cfg.buildSessionFactory();
// 創建Session
Session session = factory.openSession();
// 創建及開啓事務對象
Transaction trans = null;
try {
trans = session.beginTransaction();
// 添加User實體對象
session.save(user);
trans.commit();
} catch (HibernateException e) {
trans.rollback();
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
2. 查詢
/**
* 根據ID查詢
*/
public void testQuery() {
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 創建SessionFactory(創建連接池)
SessionFactory factory = cfg.buildSessionFactory();
// 創建Session
Session session = factory.openSession();
// 創建及開啓事務對象
Transaction trans = null;
try {
trans = session.beginTransaction();
User user = (User) session.load(User.class, 1);
System.out.println(user);
trans.commit();
} catch (HibernateException e) {
trans.rollback();
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
3. 刪除
/**
* 根據ID刪除
*/
public void testDelete() {
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 創建SessionFactory(創建連接池)
SessionFactory factory = cfg.buildSessionFactory();
// 創建Session
Session session = factory.openSession();
// 創建及開啓事務對象
Transaction trans = null;
try {
trans = session.beginTransaction();
User user = (User) session.load(User.class, 1);
session.delete(user);
System.out.println(user);
trans.commit();
} catch (HibernateException e) {
trans.rollback();
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
4. 更新
/**
* 根據ID更新
*/
public void testUpdate() {
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 創建SessionFactory(創建連接池)
SessionFactory factory = cfg.buildSessionFactory();
// 創建Session
Session session = factory.openSession();
// 創建及開啓事務對象
Transaction trans = null;
try {
trans = session.beginTransaction();
User user = (User) session.load(User.class, 2);
user.setSex("女");
user.setUsername("小雪");
session.update(user);
trans.commit();
} catch (HibernateException e) {
trans.rollback();
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
5. 查詢集合
/**
* 查詢集合
*/
public void testQueryAll() {
// 解析hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 創建SessionFactory(創建連接池)
SessionFactory factory = cfg.buildSessionFactory();
// 創建Session
Session session = factory.openSession();
// 創建及開啓事務對象
Transaction trans = null;
try {
trans = session.beginTransaction();
Query query = session.createQuery("from User");
List<User> users = query.list();
for (User user : users) {
System.out.println(user);
}
trans.commit();
} catch (HibernateException e) {
trans.rollback();
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}