mysql8.0.12+hibernate5.4.1 的一些配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32965187/article/details/88658531

目录

整体目录结构

第一步 创建数据库

第二步 创建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
        (?, ?, ?, ?, ?, ?)

数据库结果查看

猜你喜欢

转载自blog.csdn.net/qq_32965187/article/details/88658531