目录

整体目录结构

第一步 创建数据库

第二步 创建java项目,导入相应的jar包

第三步 创建数据库对应的java类

第四步 创建hibernate映射文件

第五步 创建hibernate核心配置文件

第六步 创建测试类并运行

第七步 结果分析并查看数据库


说明:我不吃螃蟹谁来吃,我不下地狱谁下,头铁!

整体目录结构

第一步 创建数据库

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&amp;serverTimezone=Asia/Shanghai

<property name="hibernate.connection.url">jdbc:mysql:///hibernatedemo1?useSSL=false&amp;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&amp;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 的一些配置的更多相关文章

  1. 安装mysql8.0.12

    安装mysql8.0.12 https://blog.csdn.net/zwj1030711290/article/details/80039780 问题1:忘记记录日志打印的密码就把窗口给关了 解决 ...

  2. MySQL8.0.12 安装及配置、读写分离,主从复制

    一.安装 1.从网上下载MySQL8.0.12版本,下载地址:https://dev.mysql.com/downloads/mysql/ 2. 下载完成后解压 我解压的路径是:D:\Java\mys ...

  3. 基础环境系列:MySQL8.0.12

    机型与版本:windows10(64-bits) Mysql环境配置:mysql8.0.12 一.MySQL安装 Mysql的安装有两种方法,一种是通过.msi一种是通过压缩包.穷呢,大家就老实下社区 ...

  4. 安装mysql8.0.12以及修改密码和Navicat的连接

    mysql8.0+与安装其他版本不同一.安装mysql8.0.121.到官网https://www.mysql.com/  下载mysql-8.0.12-winx64.zip(不要.mis),直接解压 ...

  5. MySql-8.0.12 安装教程

    MySql-8.0.12 安装教程随笔https://www.cnblogs.com/CrazyDemo/p/9409995.html MySQL 安装https://m.runoob.com/mys ...

  6. windows系统-phpstudy升级mysql8.0.12安装教程及修改密码和安装注意事项

    1.下载安装包,下载地址:mysql8.0.12  .如果你想要下载其它版本可以选择:mysql历史版本地址. 2.下载好,删除phpstudy的mysql目录.如果数据重要的,注意备份数据!同意把m ...

  7. Linux安装MySQL8.0.12之二进制安装

    运行环境:centos 7.5 + mysql8.0.12 1.下载官方打包好的二进制安装包: wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysq ...

  8. 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 ...

  9. 【MySQL-123】MySQL8.0.12 安装于Win10

    参考blog:MySQL8.0.12 安装及配置 [坑一]输入net start mysql时,MYSQL服务无法启动. 问题:第三步my.ini文件编码错误. 解决方案:https://blog.c ...

  10. mysql-8.0.12安装和配置

    1.下载Mysql8.0.12压缩包.下载地址:https://dev.mysql.com/downloads/file/?id=480557 2.解压文件到本地指定目录.这里我的mysql根目录是: ...

随机推荐

  1. 绝了,一招解决DeepSeek 提示“服务器繁忙,请稍后再试” 卡顿问题!(保姆级教程)

    大家好,我是狂师. 现在 AI 圈里讨论最多的话题就是:"国产之光DeepSeek了". 但用过的人也知道,是真的卡.动不动就提示:"服务器繁忙,请稍后再试" ...

  2. 解决Webstorm Nodejs console.log("这是中文") 控制台乱码

    设置文件编码 自定义vm选项文件 添加 文件最后一行添加 -Dfile.encoding=UTF-8 3.修改注册表 Windows+R --> regedit --> 计算机\HKEY_ ...

  3. Linux挂载U盘,SD卡

    Linux挂载U盘,SD(TF)卡 1.插入U盘,执行如下指令后能看到设备则说明连接成功 sudo fdisk -l #查看外接设备名称,一般为/dev/sd...,这里假设为/dev/sdc1 2. ...

  4. Android 监听短信数据库过滤获取短信内容上传至服务器

    前言 Android 监听短信的方式有两种 1.监听短信数据库,数据库发生改变时回调. 2.监听短信广播 其中第二种方式由于国内各厂家的定制Android 可能导致无响应 目前测试 魅族 无法监听到短 ...

  5. 职场软素质&算法工程师的硬素质--卓越的职场人需要的42种能力

    经过自己在实际的工作中摸爬滚打,个人觉得一些方面的能力是非常重要的,可以让自己在职场中快速的脱颖而出,因此,从硬实力,软实力两个方面进行总结如下: 软实力: (1)解决问题的能力 (2)预估风险的能力 ...

  6. mySql跳过行数获取多少行

    LIMIT :需要获取多少条记录 OFFSET :跳过前面的多少行记录从后面开始获取 SELECT * FROM USER LIMIT 32 OFFSET 1 只获取12行记录 跳过第一条记录 SEL ...

  7. Easyexcel(1-注解使用)

    版本依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</a ...

  8. Linux下yum安装mysql 遇到的问题Can't open and lock privilege tables: Table 'mysql.user' doesn't exist 错误

    今天在linux下安装mysql时 执行service mysqld start时, mysql总是启动失败 后来查看mysql日志:/var/log/mysqld.log,发现有个Can't ope ...

  9. CORS 跨域请求一种解决方案

    平常工作难遇到这类问题, 一般搭建新系统或搭建系统二时需要复用系统一一些前后端能力, 可能会遇到跨域拦截问题. 这里提供一种基于服务器解决方案. 更多其他方案, 详细细节可自行查阅更多资料, 写一些前 ...

  10. websocket: the client is not using the websocket protocol: ‘upgrade’ token not found in ‘Connection’ head,客户端没有使用websocket协议:'upgrade'令牌未在'Connection'头中找到

    错误分析 websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connec ...