1、创建数据库和数据表(本例使用的数据库为Mysql)
2、配置Hibernate需要的jar包和Hibernate.cfg.xml,HibernateUtil.java
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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- 配置使用的驱动jar包 -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernatetest</property><!-- 配置连接数据库的url -->
<property name="connection.username">root</property><!-- 配置连接数据库的用户名 -->
<property name="connection.password">655334</property><!-- 配置连接数据库的密码 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- 配置Hibernate的方言 -->
<property name="current_session_context_class">thread</property><!-- 创建的session会绑定到当前线程 -->
<property name="show_sql">true</property><!-- Hibernate输出执行的sql -->
<property name="format_sql">true</property><!-- Hibernate将输出的sql格式化 -->
<property name="hbm2ddl.auto">update</property><!-- 根据映射文件去和数据库中的表对应起来,如果不一致,就更新表的结构 -->
<mapping resource="com/mrw/test1/People.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package com.mrw.test1;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSesssionFactory();
private static SessionFactory buildSesssionFactory(){
try {
return new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
</span>
3、准备实体类People.java,和相对应的映射文件People.hbm.xml,并把People.hbm.xm加入到Hibernate.cfg.xml中
People.java
package com.mrw.test1;
public class People {
private Long id;
private String name;
private int age;
//省略get\set方法
}
People.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>
<class name="com.mrw.test1.People" table="People">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"></property>
</class>
</hibernate-mapping>
4、增加数据
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
People people=new People();
people.setName("善良的男子");
people.setAge(100);
session.save(people);
tx.commit();
5、查询数据
根据主键查询
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
People people=(People)session.get(People.class,Long.valueOf(1));//Long.valueOf(1)将Int类型转换为Long,与类中数据类型匹配不然会报错
System.out.println(people.getName());
tx.commit();
HQL查询
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery("from People where name=?");
query.setParameter(0,"善良的男子");
List<People> list=query.list();
for(People people:list)
{
System.out.println("-------"+people.getName());
}
tx.commit();
6、删除数据
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
People people=(People) session.get(People.class,Long.valueOf(1));
session.delete(people);
tx.commit();
7、修改数据
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.beginTransaction();
People people=(People)session.get(People.class,Long.valueOf(2));
people.setName("健康的男子");
tx.commit();