创建数据库
CREATE DATABASE IF NOT EXISTS `hibernate_day1` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `hibernate_day1`;
-- 导出 表 hibernate_day1.t_user 结构
CREATE TABLE IF NOT EXISTS `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL DEFAULT '0',
`password` varchar(30) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
编写实体类
package org.ccit.com.domain;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-02-06 12:22
**/
public class User {
private int uid;
private String username;
private String password;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在实体类同级目录下创建实体类名.hbm.xml映射配置文件
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- name: 模型全名称
table: 对应表名-->
<class name="org.ccit.com.domain.User" table="t_user">
<!-- name:模型类属性名 column:表中的字段名-->
<id name="uid" column="id">
<!-- generator:id的生成策略 native:如果是mysql数据库 id会自动增长-->
<generator class="native"></generator>
</id>
<!-- 如果模型的属性和数据库的列明相同 可省略不写-->
<property name="username"></property>
<property name="password"></property>
</class>
</hibernate-mapping>
创建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>
<!-- 1、配置数据库连接的4个参数 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<!-- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_day1</property>-->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day1?serverTimezone=UTC&useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 是否显示sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 是否自动提交事务 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 2、配置JavaBean与表的映射文件 -->
<mapping resource="org/ccit/com/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
编写Test类测试
import org.ccit.com.domain.User;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;
import java.sql.Connection;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-02-06 13:09
**/
public class HibernateTest01 {
@Test
public void test01() {
//保存用户数据
//1 获取核心配置文件对象 默认是加载src下的hibernate.cfg.xml文件
Configuration configure = new Configuration().configure();
//2 创建会话工厂
SessionFactory sessionFactory = configure.buildSessionFactory();
//创建会话
Session session = sessionFactory.openSession();
//开启事务
Transaction transaction = session.getTransaction();
transaction.begin();
//保存 直接把对象保存到数据库
User user = new User();
user.setUsername("LIANG");
user.setPassword("2233");
session.save(user);
//4 提交事务
transaction.commit();
//5 关闭会话
session.close();
//6 关闭工厂
sessionFactory.close();
}
}