1.hibernate.cfg.xml
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.password">yo1ur22s12ord</property>
<property name="connection.url">jdbc:mysql://123.415.182.94:3306/test1?serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- xml-->
<mapping resource="cn/lin/orm/entity/book.nbt.xml"/>
<!-- 注解-->
<mapping class="cn.lin.orm.entity.Book"/>
</session-factory>
</hibernate-configuration>
2,book.hbt.xml
<!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="cn.lin.orm.entity.Book" table="t_book">
<id name="id">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="name"/>
</property>
<property name="price">
<column name="price"/>
</property>
</class>
</hibernate-mapping>
3,注解
package cn.lin.orm.entity;
import javax.persistence.*;
@Entity
@Table(name="t_book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@Column(name="price")
private double price;
@Column(name = "author")
private String author;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
'}';
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
4.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>org.example</groupId>
<artifactId>com.lin</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.17.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
5.测试类
import cn.lin.orm.entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;
public class BookTest {
private SessionFactory factory;
@Before
public void init(){
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}
@Test
public void testSave(){
Session session = factory.openSession();
//开启事务
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setName("python");
book.setAuthor("python");
book.setPrice(100);
session.save(book);
tx.commit();
session.close();
}
@Test
public void get(){
Session session = factory.openSession();
Book book = session.get(Book.class,5);
System.out.println(book);
session.close();
}
@Test
public void delete(){
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setId(3);
session.delete(book);
tx.commit();
session.close();
}
@Test
public void update(){
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setId(5);
book.setName("java1");
book.setPrice(8);
book.setAuthor("lin");
session.update(book);
tx.commit();
session.close();
}
}
错误1
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement错误
解决方案:
在本项目的.cfg.xml配置中加入:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>