其他分享
首页 > 其他分享> > persistence-demo之hibernate

persistence-demo之hibernate

作者:互联网

0、前言

目的:旨在记录数据持久层的hibernate框架

项目结构:

1、导入pom文件

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.17.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

2、编写核心配置文件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.url">jdbc:mysql://10.10.50.160:3306/test?characterEncoding=UTF-8</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">abc123456#</property>


        <!-- 选项配置 -->
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>

        <!-- 映射关系 1、映射文件 2注释,两种方式,二选一即可-->
        <mapping resource="mapping/Book.hbm.xml"/>
        <mapping class="cn.itcast.orm.entity.Book"/>

    </session-factory>
</hibernate-configuration>

3、编写实体类

实体类的@Entity等注解也实现了步骤4的orm映射关系

package cn.itcast.orm.entity;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;

import javax.persistence.*;

/**
 * @author jiangl
 * @ClassName Book.java
 * @Description 实体类,图书
 * @createTime 2022年06月03日 11:21
 */

@Entity
@Table(name = "t_book")
public class Book
{
    @Id
    @Column(name = "bid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Integer id;//主键

    @Column(name = "bname")
    private String name;//图书名称

    @Column(name = "author")
    private String author;//作者

    @Column(name = "price")
    private double price;//价格

    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 String getAuthor()
    {
        return author;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    @Override
    public String toString()
    {
        return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}';
    }
}

4、编写orm映射文件Book.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- 实体类和表之间的映射关系配置-->
<hibernate-mapping>
    <class name="cn.itcast.orm.entity.Book" table="t_book">
        <id name="id">
            <column name="bid"></column>
            <!-- 数据库自增 -->
            <generator class="identity"></generator>
        </id>

        <property name="name">
            <column name="bname"></column>
        </property>

        <property name="author">
            <column name="author"></column>
        </property>

        <property name="price">
            <column name="price"></column>
        </property>
    </class>

</hibernate-mapping>

5、编写测试类

需要记得hibernate框架实现的主要步骤,即测试类的步骤1 2 3

import cn.itcast.orm.entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * @author jiangl
 * @ClassName BookTest.java
 * @Description 测试类
 * @createTime 2022年06月03日 12:46
 */
public class BookTest
{
    @Test
    public void testSave()
    {
        //1、创建 Configuration 对象,解析hibernate的核心配置文件hibernate.cfg.xml
        Configuration configure = new Configuration().configure();

        //2、创建 sessionFactory 对象,解析映射文件并生成基本的sql
        SessionFactory sessionFactory = configure.buildSessionFactory();

        //3、得到 对象,该对象具有增删改查的方法
        Session session = sessionFactory.openSession();

        //4、开启事务
        Transaction transaction = session.beginTransaction();

        //5、保存数据
        Book book = new Book();
        book.setName("java从入门到精通");
        book.setAuthor("传智播客");
        book.setPrice(9.9);
        session.save(book);

        //6、提交事务
        transaction.commit();

        //7、释放资源
        session.close();

    }
}

6、测试


保存数据成功。

标签:hibernate,name,author,demo,org,import,public,persistence
来源: https://www.cnblogs.com/jspider/p/16340336.html