hibernate

本文展示了如何使用Hibernate ORM框架配置MySQL数据库连接,并通过XML映射文件及注解实现数据的增删改查操作。配置包括hibernate.cfg.xml文件,其中包含了数据库连接信息和Hibernate配置,如方言设置、自动更新等。此外,还提供了Book实体类的XML映射文件和注解定义,以及对应的Maven依赖。测试类中实现了保存、获取、删除和更新Book实体的方法。
摘要由CSDN通过智能技术生成

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值