hibernate_demo
参考:ORM----hibernate入门Demo(无敌详细版) - Old-凯 - 博客园 (cnblogs.com)
Hibernate - 基础入门详解_51CTO博客_hibernate入门
hmb.xml:Hibernate框架之hbm.xml映射文件(详解)_hibernate映射文件详解_hestyle的博客-CSDN博客
新建testdb数据库,创建tb_users表:

模块整体目录结构:

pom.xml最初只添加了hibernate、mysql的依赖,其他jar包依赖是根据运行报错,一点点加上的。mysql依赖的版本要对应安装的mysql版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hibernate_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.2.10.Final</hibernate.version>
<hibernate.validator.version>5.4.0.Final</hibernate.validator.version>
<mysql.driver.version>8.0.21</mysql.driver.version>
</properties>
<dependencies>
<!-- Hibernate包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
User.java
package com.hmb;
public class User {
public User() {
}
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
private Integer id;
private String name;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
User.hmb.xml
<?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.hmb.User" table="tb_users">
<id name="id" column="userid">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string"/>
<property name="pwd" column="pwd" type="string"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml要放在resources目录下
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connect.driver_class">com.mysql.jdbc.Driver</property>
<!--testdb为数据库名-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb</property>
<!-- 填登录数据库的账号、密码-->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">******</property>
<property name="format_sql">true</property>
<mapping resource="com/hmb/User.hmb.xml"/>
</session-factory>
</hibernate-configuration>
Main.java
package com.hmb;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
User user = new User(3, "wangwu", "123456");
session.save(user);
transaction.commit();
session.clear();
factory.close();
}
}
运行效果:

查看数据表成功新增了数据

其他:
运行时如果报错”java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String“,可添加jvm启动参数”--add-opens java.base/java.lang=ALL-UNNAMED“:


浙公网安备 33010602011771号