Customer.java

/**
* 客户管理的实体类
* @author zhang
*CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
*/

public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
//-----省略了get和set方法
}

Customer.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.hibernate.demo1.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id">
<!-- 主键生成策略 -->
<!-- native increment identity -->
<generator class="native"/>
</id>

<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml-------------要放在项目的src下

<?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>
<!-- 连接数据库的基本参数 -->
<!-- hibernate-release-5.0.7.Final\project\etc\hibernate.properties -->
<!-- localhost:3306 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- ==========可选配置========= -->
<!-- 打印sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">create</property>

<!-- 设置事务隔离级别 -->
<!--
1 read uncommitted :脏读、不可重复读、虚读都会发生
2 read committed :解决脏读 (oracle默认)
4 repeatable read :解决脏读和不可重复读(mysql默认)
8 serializable :解决所有问题。串行执行、效率低
-->
<property name="hibernate.connection.isolation">4</property>
<!-- 配置当前线程绑定的Session -->
<!-- 不用session.close() -->
<property name="hibernate.current_session_context_class">thread</property>

<!-- 与表映射的类的全路径,把.换成/ -->
<mapping resource="com/hibernate/demo1/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

HibernateDemo1.java

/**
* hibernate的入门案例
*
*/
public class HibernateDemo1 {

@Test
//保存客户的案例
public void demo1() {
//1.加载Hibernate的核心配置文件
Configuration configuration=new Configuration().configure();
//2.创建一个SessionFactory对象:类似于JDBC连接池
SessionFactory sessionFactory=configuration.buildSessionFactory();
//3.通过SessionFactory获取Session对象:类似于JDBC的Connection
Session session=sessionFactory.openSession();
//4.手动开启事务
Transaction transaction= session.beginTransaction();
//5.编写代码

Customer customer=new Customer();
customer.setCust_name("王东");

session.save(customer);

//6.事务提交
transaction.commit();
//7.资源释放
session.close();
sessionFactory.close();
}
}

简单的增删改查

HibernateDemo2.java

/**
* hibernate的入门案例
*
*/
public class HibernateDemo2 {

@Test
//保存客户的案例
public void demo1() {
//1.加载Hibernate的核心配置文件
Configuration configuration=new Configuration().configure();
//2.创建一个SessionFactory对象:类似于JDBC连接池
SessionFactory sessionFactory=configuration.buildSessionFactory();
//3.通过SessionFactory获取Session对象:类似于JDBC的Connection
Session session=sessionFactory.openSession();//非线程安全,所以不能放在全局变量
//4.手动开启事务
Transaction transaction= session.beginTransaction();
//5.编写代码

Customer customer=new Customer();
customer.setCust_name("王二");

session.save(customer);

//6.事务提交
transaction.commit();
//7.资源释放
session.close();
sessionFactory.close();
}

@Test
//查询
/**
* get和load的区别
*/
public void demo2() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();

/**
* get
* 立即加载,执行到这行代码,马上去查询
* 查询后返回是真实对象本身
* 查询找不到,返回null
*
* load
* 延迟加载(lazy懒加载),执行到这行代码不会发送sql语句,当使用这个对象的时候才会发送sql语句
* 查询后返回的是代理对象,javassist-3.18.1-GA.jar
* 查询找不到,返回ObjectNotFoundException
*/

//使用get方法查询
// Customer customer=session.get(Customer.class, 1L);//发送sql语句
// System.out.println(customer);

//使用load方法查询
Customer customer=session.load(Customer.class, 2L);
System.out.println(customer);

tx.commit();
session.close();
}

@Test
//修改操作
public void demo3() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();

//直接创建对象,进行修改
// Customer customer=new Customer();
// customer.setCust_id(1L);
// customer.setCust_name("王聪");
// session.update(customer);
//先查询,再修改(推荐)
Customer customer=session.get(Customer.class, 1L);
customer.setCust_name("王小贱");
session.update(customer);

tx.commit();
session.close();
}

@Test
//删除操作
public void demo4() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();

//直接创建对象,删除
// Customer customer=session.get(Customer.class, 1L);
// customer.setCust_id(1L);
// session.delete(customer);
//先查询,在删除(推荐)------
Customer customer=session.get(Customer.class, 2L);
session.delete(customer);

tx.commit();
session.close();
}

@Test
//保存或更新
public void demo5() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();

// Customer customer=new Customer();
// customer.setCust_name("汪峰");
// session.saveOrUpdate(customer);

Customer customer=new Customer();
customer.setCust_id(3L);
customer.setCust_name("李凤");
session.saveOrUpdate(customer);

tx.commit();
session.close();
}

@Test
//查询所有
public void demo6() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
//接收HQL:Hibernate Query Language 面向对象的查询语言
// Query query= session.createQuery("from Customer");
// List<Customer> list=query.list();
// for (Customer customer : list) {
// System.out.println(customer);
// }

//接收sql
SQLQuery query=session.createSQLQuery("select * from cst_customer");
List<Object[]> list=query.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}

tx.commit();
session.close();
}
}

获取session的一个工具类

HibernateUtils.java

/**
* Hibernate的工具类
*
*/
public class HibernateUtils {

public static final Configuration cfg;
public static final SessionFactory sf;

static{
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
}

public static Session openSession(){
return sf.openSession();
}

public static Session getCurrentSession() {
return sf.getCurrentSession();//需要配置
}
}