欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate(6)——映射类型(上)

程序员文章站 2022-04-03 08:01:16
...

目录

 

1. 基本类型映射;


​​​​​​​

1. 基本类型映射;

Hibernate映射自动对应数据库中表的格式;

Hibernate(6)——映射类型(上) 

先建立一个Book实体类:

package com.java.model;

import java.sql.Blob;
import java.util.Date;

public class Book {
    private int id;
    private String bookName; //图书名称
    private float price; //图书价格
    private boolean specialPrice; //是否是特价
    private Date publishDate; //发布日期
    private String author; //作者
    private String introduction; //简介
    private Blob bookPic; //图书图片

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public float getPrice() {
        return price;
    }

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

    public boolean isSpecialPrice() {
        return specialPrice;
    }

    public void setSpecialPrice(boolean specialPrice) {
        this.specialPrice = specialPrice;
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    public String getAuthor() {
        return author;
    }

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

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public Blob getBookPic() {
        return bookPic;
    }

    public void setBookPic(Blob bookPic) {
        this.bookPic = bookPic;
    }
}

写Book映射文件,Book.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.java.model">

	<class name="Book" table="t_book">
		<id name="id" column="bookId">
			<generator class="native"></generator>
		</id>
		
		<property name="bookName" column="bookName" length="40"></property>
		<property name="price" column="price" type="float"></property>
		<property name="specialPrice" column="specialPrice" type="boolean"></property>
		<property name="publishDate" column="publishDate" type="date"></property>
		<property name="author" column="author" length="20"></property>
		<property name="introduction" column="introduction" length="20"></property>
		<property name="bookPic" column="bookPic" type="blob"></property>

	</class>

</hibernate-mapping>

写hibernate配置文件,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.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>

       
        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
	
        <!-- 控制台显示SQL -->
        <property name="show_sql">true</property>

        <!-- 自动更新表结构 -->
        <property name="hbm2ddl.auto">update</property>

		<!--<mapping resource="com/java/model/Student.hbm.xml"></mapping>
		<mapping resource="com/java/model/Class.hbm.xml"></mapping>-->
        <mapping resource="com/java/model/Book.hbm.xml"></mapping>
    </session-factory>

</hibernate-configuration>

 Book测试类,BookTest:

package com.java.service;

import com.java.model.Book;
import com.java.util.HibernateUtil;
import org.hibernate.LobHelper;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.text.SimpleDateFormat;

public class BookTest {
    public static void main(String[] args) throws Exception{
        SessionFactory sessionFactory= HibernateUtil.getSessionFactory();
        Session session=sessionFactory.openSession(); // 生成一个session
        session.beginTransaction(); // 开启事务

        Book book=new Book();
        book.setBookName("java编程思想");
        book.setPrice(100);
        book.setSpecialPrice(true);
        book.setPublishDate(new SimpleDateFormat("yyyy-MM-dd").parse("2019-10-23"));
        book.setAuthor("埃克尔");
        book.setIntroduction("简介...");

        LobHelper lobHelper=session.getLobHelper();
        InputStream in=new FileInputStream("c://java编程思想.jpg");
        Blob bookPic=lobHelper.createBlob(in, in.available());
        book.setBookPic(bookPic);

        //保存事务
        session.save(book);

        session.getTransaction().commit(); // 提交事务
        session.close(); // 关闭session
    }
}

运行之后看看数据库中的表:

Hibernate(6)——映射类型(上)

以及数据:

Hibernate(6)——映射类型(上)

相关标签: 映射类型