第一步:导包(项目整个架构):
第二步:创建实体类及其配置文件
Customer
package com.qiuqiu.pojo;
public class Customer {
private int cus_id;
private String cus_name;
private int cus_age;
//省略get与set方法及其构造方法
}
Customer.hbm.xml(注意名字格式为:类名.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.qiuqiu.pojo">
<class name="Customer" table="customer">
<id column="cus_id" name="cus_id">
<generator class="native"></generator>
</id>
<property name="cus_name" column="cus_name"></property>
<property name="cus_age" column="cus_age"></property>
</class>
</hibernate-mapping>
第三步:编写核心配置文件 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>
<!-- 数据库连接地址配置url -->
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<!-- 数据库用户名配置 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库密码设置 -->
<property name="hibernate.connection.password">123</property>
<!-- hibernate方言配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- ddl类型配置为create -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- sql语句相关配置 -->
<!-- 配置显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<mapping resource="com/qiuqiu/pojo/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(注意方言不要弄错,不然会报错)
第四步:测试
package com.qiuqiu.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.qiuqiu.pojo.Customer;
import com.qiuqiu.utils.SessionUtils;
public class MyTest {
//保存用户
@Test
public void sessionTest(){
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session openSession = factory.openSession();
Transaction transaction = openSession.beginTransaction();
Customer customer = new Customer("张艳杰", 20);
openSession.save(customer);
transaction.commit();
openSession.close();
factory.close();
}
//查询id为1的对象
@Test
public void selectOne(){
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session openSession = factory.openSession();
Customer customer = openSession.get(Customer.class, 1);
System.out.println(customer.toString());
openSession.close();
factory.close();
}
//修改id为1的信息
@Test
public void testModify(){
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session openSession = factory.openSession();
Customer customer = openSession.get(Customer.class, 1);
customer.setCus_name("修改后的名字");
Transaction transaction = openSession.beginTransaction();
openSession.update(customer);
transaction.commit();
openSession.close();
factory.close();
}
//删除id为1的对象
@Test
public void delCustomer(){
Session session = SessionUtils.getSession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class, 1);
session.delete(customer);
transaction.commit();
}
}
utils:
package com.qiuqiu.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionUtils {
static Configuration configure = new Configuration().configure();
static SessionFactory sessionFactory = configure.buildSessionFactory();
public static Session getSession() {
Session openSession = sessionFactory.openSession();
return openSession;
}
}
运行结果输出显示:
测试hibernate的一级缓存
一级缓存:
@Test
public void selectOne(){
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session openSession = factory.openSession();
System.out.println("第一次开始查询id为2的用户");
Customer customer = openSession.get(Customer.class, 2);
System.out.println(customer.toString());
System.out.println("第二次开始查询id为2的用户");
Customer customer1 = openSession.get(Customer.class, 2);
System.out.println(customer1.toString());
openSession.close();
factory.close();
}
可以看到我们进行了一次查询