pom.xml
<?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>com</groupId> <artifactId>hibernate</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.2.Final</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!--<scope>test</scope>--> </dependency> </dependencies> <build> <plugins> <!-- 指定jdk --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <resources> <!-- 把xml文件也打包到相应位置 --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <excludes> <exclude>**/*.properties</exclude> </excludes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build> </project>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置连接数据库的基本信息 --> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://xxx.xxx.xxx.xxx:3306/hibernate?useSSL=false&characterEncoding=utf-8</property> <!-- 配置 hibernate 的基本信息 --> <!-- hibernate 所使用的数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property> <!-- 执行操作时是否在控制台打印 SQL --> <property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml 文件 --> <mapping resource="com/hibernate/a/News.hbm.xml"/> </session-factory> </hibernate-configuration>
实体类
News
package com.hibernate.a; import java.util.Date; public class News { private Integer id; private String title; private String author; private Date date; // 用于实例化 public News() {} public News(String title, String author, Date date) { super(); this.title = title; this.author = author; this.date = date; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; } }
News.hbm.xml
<?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 package="com.hibernate.a"> <class name="News" table="NEWS"> <id name="id" type="java.lang.Integer" column="ID"> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <generator class="native" /> </id> <property name="title" type="java.lang.String" column="TITLE" /> <property name="author" type="java.lang.String" column="AUTHOR"/> <property name="date" type="date" column="DATE"/> </class> </hibernate-mapping>
测试
package com.hibernate.a; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import java.sql.Date; public class HibernateTest { @Test public void test() { // 创建 Configuration 对象: 对应 hibernate 的基本配置信息和对象关系映射信息 Configuration config = new Configuration().configure(); // 创建 ServiceRegistry 对象: hibernate 的任何配置和服务都需要在该对象中注册后才能有效 // ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config .getProperties()).build(); // 创建 SessionFactory 对象 // SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); SessionFactory sessionFactory = config.buildSessionFactory(); // 创建 Session 对象 Session session = sessionFactory.openSession(); // 开启事务 Transaction transaction = session.beginTransaction(); // 执行保存操作 News news = new News("Java12345", "ATGUIGU", new Date(new java.util.Date().getTime())); session.save(news); News news1 = session.get(News.class, 1); System.out.println(news1); // 提交事务 transaction.commit(); // 关闭 Session session.close(); // 关闭 SessionFactory 对象 sessionFactory.close(); } }
项目结构