目录
整体目录结构
第一步 创建数据库
第二步 创建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项目,项目名字hibernateDemo
在项目下创建lib目录,导入jar包
找到hibernate-release-5.4.1.Final.zip,把压缩包lib/required下的jar包全部导入
导入数据库的连接jar包mysql-connector-java-8.0.12.jar
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 + "]";
}
}
注意事项
由于采用的是mysql8,在url配置上要加入属性信息 ?useSSL=false&serverTimezone=Asia/Shanghai
jdbc:mysql:///hibernatedemo1?useSSL=false&serverTimezone=Asia/Shanghai
方言的配置要使用MySQL8Dialect,如果是MySQLDialect我测试时不能自动创建表
org.hibernate.dialect.MySQL8Dialect
最终配置
hibernate.cfg.xml
com.mysql.cj.jdbc.Driver
jdbc:mysql:///hibernatedemo1?useSSL=false&serverTimezone=Asia/Shanghai
root
123456
org.hibernate.dialect.MySQL8Dialect
true
true
update
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 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 INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 三月 19, 2019 12:01:57 下午 org.hibernate.dialect.Dialect 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 (?, ?, ?, ?, ?, ?) |
数据库结果查看