hibernate核心配置文件
一般会在src下创建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="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test?serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 可选配置-->
<!-- 连接池-->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="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">update</property>
<!-- 自动更新表结构-->
<property name="hbm2ddl.auto">update</property>
<!-- 配置数据库方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 映射 必要的-->
<mapping resource="com/xxx/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
user实体类
package com.xxx.pojo;
public class User {
private Integer id;
private String name;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
创建实体配置文件User.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.xxx.pojo">
<!-- 配置实体类id与表id对应-->
<class name="User" table="user_table">
<!-- name 是实体类id-->
<!-- column 生成的表id,不写则使用实体类id名称-->
<id name="id" type="java.lang.Integer">
<!-- 设置表id增长策略,native代表生成id为主键且自动增长-->
<generator class="native"/>
</id>
<!-- 其他属性和表字段对应-->
<!-- name 实体类名称-->
<!-- column 表字段名称 不写则与name值相同-->
<property name="name"/>
<property name="sex"/>
</class>
</hibernate-mapping>
添加操作
7步骤
- 加载hibernate核心配置文件
- 创建SessionFactory对象
- 通过SessionFactory对象创建session
- 开启事务
- 编写crud
- 提交事务
- 关闭资源
package com.xxx;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.xxx.pojo.User;
public class UserManager {
public static void main(String[] args) throws Exception{
// 步骤
// 加载hibernate核心配置文件
// configuration的创建一般会在src下读取hibernate.cfg.xml配置文件(也可以自定义位置),
// 然后创建对象,把配置文件加载到对象里(加载核心配置文件)
Configuration conf = new Configuration();
conf.configure();
// 创建SessionFactory对象
// configuration对象创建sessionFactory对象,
// 在创建的过程中根据配置文件中的数据库配置和映射配置来对数据库的表进行创建,
// 由于在这个过程中,每次创建sessionFactory对象都要就行数据库创建或更新,
// 比较耗资源,所以一个项目一般创建一个,即使用工具类,编写静态代码块来实现
SessionFactory sessionFactory = conf.buildSessionFactory();
// 通过SessionFactory对象创建session
Session sess = sessionFactory.openSession();
// 开启事务
Transaction startTask = sess.beginTransaction();
// 编写crud
User user = new User();
user.setName("wade");
user.setSex("bad boy");
sess.save(user);
// 提交事务
startTask.commit();
// 关闭资源建议使用finally关闭
sessionFactory.close();
}
}
configuration的创建
configuration的创建一般会在src下读取hibernate.cfg.xml配置文件(也可以自定义位置),然后创建对象,把配置文件加载到对象里(加载核心配置文件)
configuration对象创建sessionFactory对象
configuration对象创建sessionFactory对象,在创建的过程中根据配置文件中的数据库配置和映射配置来对数据库的表进行创建
session
1、session类似jdbc中的connection
2、session中有不同方法实现crud操作
修改:update()
添加:save(),saveOrUpdate()
删除:delete()
根据id查找:get(),load()
数据库操作对象:create(),createSQLQuery()
条件查询:createCriteria()
3、session是单线程对象,不能共用
Transaction事务对象
提交方法commit()
回滚rollback()
事务的特性:原子性,一致性,隔离性,持久性