使用环境
hibernate 5.2 mysql8.0 jdk1.8 mysql-connector 8
导入依赖
编辑hibernate.cfg.xml文件
//数据库方言
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
//mysql8.0的连接写法
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
//?前为数据库名
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/homework?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true</property>
//用户名和密码
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">765489</property>
<property name="show_sql">true</property>
//映射
<mapping resource="pojo/User.hbm.xml"/>
创建实体类
在pojo包中创建User.java
package pojo;
import java.util.Date;
public class User {
private int id;
private String name;
private String gender;
private int age;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
创建数据库的表
设置主键为自增
编辑User.hbm.xml
在pojo包中导入User.hbm.xml文件,编写文件内容
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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="pojo.User" table="T_USER">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="gender"/>
<property name="age"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
创建usermanager进行测试
在dao包下创建usermanager.java
package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import pojo.User;
import java.sql.Date;
public class managerUser {
public static void main(String[] args) {
SessionFactory sessionFactory = null;
//创建Con对象:对应hibernate的基本配置信息和对象关系映射
Configuration config = new Configuration().configure();
//创建一个SessionFactoryRegistry对象hibernate的任何配置和服务都需要在该对象中注册后才有用
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
sessionFactory = config.buildSessionFactory(standardRegistry);
//创建并打开会话
Session session =sessionFactory.openSession();
//开启事务
org.hibernate.Transaction transaction = session.beginTransaction();
User u = new User();
u.setName("skadi");
u.setGender("女");
u.setAge(18);
u.setBirthday(Date.valueOf("2022-2-2"));
session.save(u);
//提交事务
transaction.commit();
//关闭session
session.close();
//关闭sessionfactory
sessionFactory.close();
}
}
插入数据如下
配置途中出现java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException,检查发现是jdk版本过高,使用jdk9以下版本即可。