mysql8.0.12+hibernate5.4.1 的一些配置
目录
说明:我不吃螃蟹谁来吃,我不下地狱谁下,头铁!
整体目录结构

第一步 创建数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for cst_customer
-- ----------------------------
DROP TABLE IF EXISTS `cst_customer`;
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

第二步 创建java项目,导入相应的jar包
创建java项目,项目名字hibernateDemo
在项目下创建lib目录,导入jar包
找到hibernate-release-5.4.1.Final.zip,把压缩包lib/required下的jar包全部导入

导入数据库的连接jar包mysql-connector-java-8.0.12.jar
第三步 创建数据库对应的java类
Customer.java
package com.zhujunwei.domain;
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 ; //移动电话
public Customer() {
}
public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
String cust_phone, String cust_mobile) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_source = cust_source;
this.cust_industry = cust_industry;
this.cust_level = cust_level;
this.cust_phone = cust_phone;
this.cust_mobile = cust_mobile;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
+ ", cust_mobile=" + cust_mobile + "]";
}
}
第四步 创建hibernate映射文件
<?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.zhujunwei.domain.Customer" table="cst_customer">
<!-- 主键的设置 -->
<id name="cust_id" column="cust_id">
<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核心配置文件
注意事项
由于采用的是mysql8,在url配置上要加入属性信息 ?useSSL=false&serverTimezone=Asia/Shanghai
<property name="hibernate.connection.url">jdbc:mysql:///hibernatedemo1?useSSL=false&serverTimezone=Asia/Shanghai</property>
方言的配置要使用MySQL8Dialect,如果是MySQLDialect我测试时不能自动创建表
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
最终配置
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.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernatedemo1?useSSL=false&serverTimezone=Asia/Shanghai</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- hibernate方言配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- 可选配置(打印sql) -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 没有表则自动创建 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件路径 -->
<mapping resource="com/zhujunwei/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第六步 创建测试类并运行
package com.zhujunwei.domain;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.jupiter.api.Test;
public class HibernateTest {
@Test
public void demo1() {
//1、加载Hibernate的核心配置文件:hibernate.cfg.xml
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("love you.");
session.save(customer);
//6、事务提交
transaction.commit();
//7、资源释放
session.close();
}
}
第七步 结果分析并查看数据库
控制台信息 没有采用日志,前面信息会有点乱,后面可以看到sql的执行语句
| 三月 19, 2019 12:01:55 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.4.1.Final} 三月 19, 2019 12:01:56 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 三月 19, 2019 12:01:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 三月 19, 2019 12:01:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql:///hibernatedemo1?useSSL=false&serverTimezone=Asia/Shanghai] 三月 19, 2019 12:01:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} 三月 19, 2019 12:01:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 三月 19, 2019 12:01:57 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 三月 19, 2019 12:01:57 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect 三月 19, 2019 12:01:58 下午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@4d774249] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 三月 19, 2019 12:01:58 下午 org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] Hibernate: insert into cst_customer (cust_name, cust_source, cust_industry, cust_level, cust_phone, cust_mobile) values (?, ?, ?, ?, ?, ?) |
数据库结果查看

mysql8.0.12+hibernate5.4.1 的一些配置的更多相关文章
- 安装mysql8.0.12
安装mysql8.0.12 https://blog.csdn.net/zwj1030711290/article/details/80039780 问题1:忘记记录日志打印的密码就把窗口给关了 解决 ...
- MySQL8.0.12 安装及配置、读写分离,主从复制
一.安装 1.从网上下载MySQL8.0.12版本,下载地址:https://dev.mysql.com/downloads/mysql/ 2. 下载完成后解压 我解压的路径是:D:\Java\mys ...
- 基础环境系列:MySQL8.0.12
机型与版本:windows10(64-bits) Mysql环境配置:mysql8.0.12 一.MySQL安装 Mysql的安装有两种方法,一种是通过.msi一种是通过压缩包.穷呢,大家就老实下社区 ...
- 安装mysql8.0.12以及修改密码和Navicat的连接
mysql8.0+与安装其他版本不同一.安装mysql8.0.121.到官网https://www.mysql.com/ 下载mysql-8.0.12-winx64.zip(不要.mis),直接解压 ...
- MySql-8.0.12 安装教程
MySql-8.0.12 安装教程随笔https://www.cnblogs.com/CrazyDemo/p/9409995.html MySQL 安装https://m.runoob.com/mys ...
- windows系统-phpstudy升级mysql8.0.12安装教程及修改密码和安装注意事项
1.下载安装包,下载地址:mysql8.0.12 .如果你想要下载其它版本可以选择:mysql历史版本地址. 2.下载好,删除phpstudy的mysql目录.如果数据重要的,注意备份数据!同意把m ...
- Linux安装MySQL8.0.12之二进制安装
运行环境:centos 7.5 + mysql8.0.12 1.下载官方打包好的二进制安装包: wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysq ...
- Mac安装mysql8.0.12
···shell 下载 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-macos10.13-x86_64.tar.gz ...
- 【MySQL-123】MySQL8.0.12 安装于Win10
参考blog:MySQL8.0.12 安装及配置 [坑一]输入net start mysql时,MYSQL服务无法启动. 问题:第三步my.ini文件编码错误. 解决方案:https://blog.c ...
- mysql-8.0.12安装和配置
1.下载Mysql8.0.12压缩包.下载地址:https://dev.mysql.com/downloads/file/?id=480557 2.解压文件到本地指定目录.这里我的mysql根目录是: ...
随机推荐
- [HEOI2014]大工程 题解
发现可以直接建立虚树. 设 \(dp_{u,0/1/2}\) 表示第 \(u\) 个节点的子树内,所有选中节点到它的距离之和/选中节点中到它的最短距离/选中节点中到它的最长距离,\(as_{u,0/1 ...
- JS经纬度坐标转换
var GPS = { PI : 3.14159265358979324, x_pi : 3.14159265358979324 * 3000.0 / 180.0, delta : function ...
- Elasticsearch搜索引擎学习笔记(一)
核心概念 ES -> 数据库 索引index -> 表 文档 document -> 行(记录) 字段 fields -> 列 安装Elasticsearch 1. 上传后解压 ...
- 绝了!k3s (k8s) 安装 ollama 运行 deepseek 全流程揭秘,yaml全公开
k3s (k8s) 环境搭建与 ollama 相关 yaml 文件部署 在容器编排的世界中,k3s (k8s) 无疑是备受瞩目的存在.此次聚焦在 k3s (k8s) 环境下安装 ollama,并实现运 ...
- Vulnhub-Hackme
一.靶机搭建 选择扫描虚拟机 选择路径即可 二.信息收集 靶机信息 Name: hackme: 1 Date release: 18 Jul 2019 难度:初级,目标是通过web漏洞获得有限的权限访 ...
- SQL注入之WAF绕过注入
绕过WAF: WAF防御原理: 简单来说waf就是解析http请求,检测http请求中的参数是否存在恶意的攻击行为,如果请求中的参数和waf中的规则库所匹配,那么waf则判断此条请求为攻击行为并进行阻 ...
- vim中文乱码 vim字符集设置
vim中文乱码 vim字符集设置 vim的设置一般放在/etc/vimrc文件中,不过,建议不要修改它.可以修改~/.vimrc文件(默认不存在,可以自己新建一个),写入所希望的设置. set fil ...
- 如何通过 MCP 将你的 Supabase 数据库连接到 Cursor
Cursor + MCP + Supabase. 图片来自作者 在过去几周里,MCP(Model Context Protocol,模型上下文协议)在许多 AI 相关的在线社区和论坛里大火.开发者和技 ...
- 几个技巧,教你去除文章的 AI 味!
最近有不少朋友在利用 AI 写毕业设计论文,几秒钟一篇文章就刷出来的,爽的飞起. 结果万万没想到,人家论文查重服务也升级了,是不是用 AI 写的论文大概率都能被查出来... 这可如何是好啊?救救我救救 ...
- SQL Server如何跟踪自动统计信息更新?
SQL Server数据库中,我们都清楚统计信息对于优化器来说非常重要.一般情况下,我们会开启"自动更新统计信息"(Auto Update Statistics)这个选项,以便数据 ...