【Hibernate】Re01 快速入门
官网地址:
http://hibernate.org/orm/releases/5.4/
地址:
https://bintray.com/hibernate/artifacts/hibernate-orm
只需要一个坐标就搞好了
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-agroal</artifactId> <version>5.4.21.Final</version> <type>pom</type> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
编写新闻实体类:
package cn.zeal4j.domain; import java.util.Date; /** * @author Administrator * @file Hibernate * @create 2020 09 23 16:06 */ public class News { private Integer id; private String title; private String author; private Date date; public News() { } public News(Integer id, String title, String author, Date date) { this.id = id; this.title = title; this.author = author; this.date = date; } @Override public String toString() { return "News{" + "id=" + id + ", title='" + title + '\'' + ", author='" + author + '\'' + ", 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; } }
编写Hibernate核心配置XML【hibernate.cfg.xml】
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 基本链接参数 --> <property name="connection.url">jdbc:mysql://IP-Address:Port/hibernate?serverTimezone=Asia/Shanghai</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.username">Username</property> <property name="connection.password">Password</property> <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property> <!-- 数据库版本方言 --> <property name="show_sql">true</property> <!-- 是否让控制台输出SQL语句 --> <property name="format_sql">true</property> <!-- 控制台输出的SQL格式化 --> <property name="hbm2ddl.auto">update</property> <!-- 数据库表的生成策略 --> <mapping resource="hibernate/mapping/News.hbm.xml" /> <!-- 实体类映射XML路径 --> </session-factory> </hibernate-configuration>
编写映射配置:
<?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 package="cn.zeal4j.domain" > <!-- 实体类包映射 --> <class name="News" table="news" > <!-- 实体类配置 --> <id name="id" column="id" type="java.lang.Integer"> <!-- 主键与维护策略 --> <generator class="increment" /> <!-- 发现官方提供的策略很严格,必须increment 其他的策略都会报错说 主键没有默认值 --> <!--<generator class="assigned" />--> <!--<generator class="native" />--> </id> <property name="title" column="title" type="java.lang.String" /> <!-- 类属性 表字段 java类型 --> <property name="author" column="author" type="java.lang.String" /> <property name="date" column="date" type="java.util.Date" /> <!-- 默认的util包的Date即可,不要使用sql包的 --> </class> </hibernate-mapping>
测试运行:
package cn.zeal4j; import cn.zeal4j.domain.News; 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.junit.Test; import java.io.Serializable; import java.util.Date; /** * @author Administrator * @file Hibernate * @create 2020 09 23 16:16 */ public class HibernateTest { @Test public void quickStart() { Transaction transaction = null; Session session = null; SessionFactory sessionFactory = null; try { // 但在5.1.0版本汇总,hibernate则采用如下新方式获取: // 1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final // 在configure("cfg/hibernate.cfg.xml")方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate/hibernate.cfg.xml").build(); // 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂 sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); /* - - - - - - - 上面是配置准备,下面开始我们的数据库操作 - - - - - - - */ session = sessionFactory.openSession(); //从会话工厂获取一个session transaction = session.beginTransaction(); News news = new News(null, "新闻标题:这是一段演示文本...", "作者:Zeal4J", new Date()); Serializable save = session.save(news); System.out.println("操作结果:" + save); transaction.commit(); } catch (Exception exception) { exception.printStackTrace(); transaction.rollback(); } finally { session.close(); sessionFactory.close(); } } }
输出的SQL语句:
Hibernate: select max(id) from news 操作结果:3 Hibernate: insert into news (title, author, date, id) values (?, ?, ?, ?)