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根目录是: ...
随机推荐
- 运行jar包时,在命令行中指定依赖的jar包和主类
在一次实验过程中,使用maven打包java项目为jar包,打出来的myexp.jar包只有7KB(我的实验项目正常打出来的包不小于60MB).这时,运行java -jar myexp.jar报错&q ...
- 解密prompt系列48. DeepSeek R1 & Kimi 1.5长思维链 - RL Scaling
春节前DeepSeek R1和Kimi1.5炸翻天了,之前大家推测的O1的实现路径,多数都集中在MCTS推理优化,以及STaR等样本自优化方案等等,结果DeepSeek和Kiim直接出手揭示了reas ...
- 安川Yaskawa机器人DX100示教器维修的方法
安川Yaskawa机器人DX100示教器维修的优劣势分析 安川Yaskawa机器人示教编程,工业机器人维修,即操作人员经过安川机器人示教器,ABB机器人保养,手动操控机器人的关节运动,以使机器人运动到 ...
- Linux - 内核版本升级
测试时间:2024年5月15日,本文测试CentOS7.9的内核版本升级 测试结论:不要选择手动编译的方式!!! 一.使用第三方仓库(ELRepo) (1)升级前内核查看(3.10.0-1160.el ...
- Ansible - [04] 关于sudo的一些配置
sudo sudo,以超级管理员或其他人的身份执行命令 基本流程 管理员需要先授权(修改/etc/sudoers文件) 普通用户以sudo的形式执行命令 可以通过sudo -l查看授权情况 配置sud ...
- Windows 提权-SeImpersonatePrivilege 特权
本文通过 Google 翻译 SeImpersonatePrivilege – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校 ...
- Flink学习(四) Flink Table & SQL 实现wordcount Java版本
Flink Table & SQL WordCountFlink SQL 是 Flink 实时计算为简化计算模型,降低用户使用实时计算门槛而设计的一套符合标准 SQL 语义的开发语言. 一个完 ...
- openpyxl 写入字典
def write(self,data_path, sheetname,value): index = len(value) workbook = openpyxl.Workbook() sheet ...
- Windows 提权-内核利用_1
本文通过 Google 翻译 Kernel Exploits Part 1 – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校 ...
- 三分钟教学:手把手教你实现Arduino发布第三方库
三分钟教学:手把手教你实现Arduino发布第三方库 原文链接: 手把手教你实现Arduino发布第三方库 摘要 Arduino 发布第三方库的流程包括:构建库的基本框架后将其打包并上传至 GitHu ...