项目环境
JDK 1.8
maven 3.6
Hibernate 5.5.0
MySql 5.7
创建一个maven项目 使用idea创建一个普通的maven项目就可以了。
添加相关依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 <?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 > HibernateTest</artifactId > <version > 1.0-SNAPSHOT</version > <dependencies > <dependency > <groupId > org.hibernate</groupId > <artifactId > hibernate-core</artifactId > <version > 5.5.0.Final</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.38</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.18.20</version > </dependency > </dependencies > <build > <resources > <resource > <directory > src/main/resources</directory > </resource > <resource > <directory > src/main/java</directory > <includes > <include > **/*.xml</include > </includes > <filtering > false</filtering > </resource > </resources > </build > <properties > <maven.compiler.source > 8</maven.compiler.source > <maven.compiler.target > 8</maven.compiler.target > </properties > </project >
编写hibernate.cfg.xml配置文件 在resource文件夹下创建配置文件,写入以下信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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 ="show_sql" > true</property > <property name ="connection.driver_class" > com.mysql.jdbc.Driver</property > <property name ="connection.url" > jdbc:mysql:///hibernatetest</property > <property name ="connection.username" > root</property > <property name ="connection.password" > Good18882336322</property > <property name ="dialect" > org.hibernate.dialect.MySQL57Dialect</property > <mapping resource ="com/fizzyelf/entity/User.hbm.xml" /> </session-factory > </hibernate-configuration >
创建一个实体类 这里创建一个User类做为演视
1 2 3 4 5 6 7 8 9 10 package com.fizzyelf.entity;import lombok.Data;@Data public class User { private Integer id; private String userName; private String password; }
创建类的映射配置文件 在类的包下在创建一个映射文件User.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping > <class name ="com.fizzyelf.entity.User" table ="user" lazy ="false" > <id name ="id" column ="id" > <generator class ="native" /> </id > <property name ="userName" column ="user_name" /> <property name ="password" column ="password" /> </class > </hibernate-mapping >
在数据库创建一个user表
测试代码 这里的代码我是从官方文档复制过来的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package com.fizzyelf;import com.fizzyelf.entity.User;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.junit.Test;public class HibernateTest { @Test public void Test () { final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() .build(); SessionFactory sessionFactory = null ; try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); User user = new User(); user.setUserName("FizzyElf" ); user.setPassword("123" ); session.save(user); session.getTransaction().commit(); session.close(); } catch (Exception e) { System.out.println(e.getMessage()); StandardServiceRegistryBuilder.destroy( registry ); } } }
运行测试代码
我们可以看见数据库中新增了一条数据