hibernate.cfg.xml文件内容:
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=UTC true root 1234 org.hibernate.dialect.MySQL8Dialect true true update org.hibernate.c3p0.internal.C3P0ConnectionProvider 5 20 120 3000 4 thread
Customer.hbm.xml文件内容:
test.java:
package com.myxq.test;
import com.myxq.domain.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTest {
@Test
public void test(){
// 1.加载Hibernate的核心配置文件
Configuration configuration = new Configuration().configure();
// 2.创建一个SessionFactory对象:类似于JDBC中连接池
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.通过SessionFactory获取到Session对象:类似于JDBC中Connection
Session session = sessionFactory.openSession();
// 5.编写代码
Customer customer = new Customer();
customer.setCust_name("测试");
customer.setCust_level("123");
session.save(customer);
System.out.println(customer.toString());
// 6.资源释放
session.close();
sessionFactory.close();
}
}
重点部分:
1.配置
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=UTC true org.hibernate.dialect.MySQL8Dialect
2.jar包版本:需要使用版本8以上
mysql-connector-java-8.0.13.jar



