IDEA中数据库和java之间的配置文件*.hbm.xml(多对一,多对多,。。。))

直接放代码:代码中均有注释:

Book.java代码如下:

package com.whpu.k16035.entity;

import java.util.Date;
public class Book {
    private Integer bookId;
    private String bookName;
    private String bookAuthor;
    private Date bookSaleTime;
    private Double bookPrice;
//    private Integer bookTypeId;
     //书相对于书类型是什么
    //多对一的关系
    //关联条件是外键
    //在多的这方引入一的类型
    private  BookType bookType;

    public BookType getBookType() {
        return bookType;
    }

    public void setBookType(BookType bookType) {
        this.bookType = bookType;
    }

    public Book(){

    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

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

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public Date getBookSaleTime() {
        return bookSaleTime;
    }

    public void setBookSaleTime(Date bookSaleTime) {
        this.bookSaleTime = bookSaleTime;
    }

    public Double getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(Double bookPrice) {
        this.bookPrice = bookPrice;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", bookAuthor='" + bookAuthor + '\'' +
                ", bookSaleTime=" + bookSaleTime +
                ", bookPrice=" + bookPrice +
                '}';
    }
}


book的配置文件: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">
<!--
    package :  实体类的包名
-->
<hibernate-mapping package="com.whpu.k16035.entity">
    <class name="Book" table="book">
        <id name="bookId" column="book_id" type="java.lang.Integer">
            <!--主键生成策略
               native: 数据库自己管理
               sequence:使用oracle数据库时用到
               UUID: 字符串随机数
            -->
            <generator class="native"/>
        </id>
        <property name="bookName" column="book_name" type="java.lang.String" length="64"></property>
        <property name="bookAuthor" column="book_author" ></property>
        <property name="bookPrice" column="book_price"></property>
        <property name="bookSaleTime" column="book_saleTime"></property>
<!--        <property name="bookTypeId" column="book_type" type="java.lang.Integer"></property>-->
        <!--多对一的配置-->
        <!--·
             name:    属性名称   bookType
             class:   属性数据类型  com.whpu.entity.BookType
             column:  表中外键的字段名 book_type
        -->
        <many-to-one name="bookType" class="com.whpu.k16035.entity.BookType" column="book_type" />
    </class>
</hibernate-mapping>

BookType.java的代码:

public class BookType {
    private  Integer typeId;
    private  String typeName;

    //关联book表的关系
    //什么关系:一对多
    private Set<Book> bookList = new HashSet<Book>();
}

bookType的配置文件:bookType.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">
<!--
    package :  实体类的包名
-->
<hibernate-mapping  package="com.whpu.k16035.entity" >
    <!--
        name:实体类的名称
        table:表的名
    -->
    <class name="BookType" table="booktype">
        <!--
            id:配置表的主键
                name:实体类中的属性
                column:表中的主键名称
        -->
        <id name="typeId" column="type_id"  type="java.lang.Integer" length="11">
            <!--主键生成策略
                native:数据库自己管理
            -->
            <generator class="native"/>
        </id>
        <!--
            property:配置其他属性
                name:实体类属性的名称
                column:表中字段的名称
                type:数据类型
                length:数据类型的长度
        -->
        <property name="typeName" column="type_name" type="java.lang.String" length="64"></property>

        <!--一对多的配置-->
        <!--
              set:配置一对多
              name:属性的名称
        -->
        <set name="bookList">
            <!--
                 '多'的外键的名称
            -->
            <key><column name="book_type"></column></key>
            <!--
                class:集合的泛型
            -->
            <one-to-many class="com.whpu.k16035.entity.Book"/>
        </set>

    </class>
</hibernate-mapping>

下面用学生和老师的例子展示多对多的实现:

Stu.java:

public class Stu {
    private Integer sId;
    private String sName;

    //  多对多
    private Set<Tea> teaSet =new HashSet<Tea>();

    public Set<Tea> getTeaSet() {
        return teaSet;
    }

    public void setTeaSet(Set<Tea> teaSet) {
        this.teaSet = teaSet;
    }
   
}

Stu的配置文件:Stu.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  package="com.whpu.k16035.entity" >

    <class name="Stu" table="stu">
        <id name="sId" column="sid">
            <generator class="native"/>
        </id>
        <property name="sName" column="sname"></property>

        <!--多对多
            name:属性的名称
            table:中间表名
        -->
        <set name="teaSet" table="tea_stu">
            <!--
                column : name:中间表与本表关联的外键的名称
            -->
            <key><column name="s_id"></column></key>
            <!--
                many-to-many:多对多
                column:中间表的字段与另一张表关联的外键
                class:另一张表对应的实体类的全类限定名
            -->
            <many-to-many column="t_id" class="com.whpu.k16035.entity.Tea" ></many-to-many>
        </set>
    </class>
</hibernate-mapping>

Tea.java如下:

public class Tea {
    private Integer tId;
    private String tName;

    //多对多
    private Set<Stu> setStu= new HashSet<Stu>();

    public Set<Stu> getSetStu() {
        return setStu;
    }

    public void setSetStu(Set<Stu> setStu) {
        this.setStu = setStu;
    }
    }

Tea的配置文件:Tea.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  package="com.whpu.k16035.entity" >
    <class name="Tea" table="tea">
        <id name="tId" column="t_id">
            <generator class="native"/>
        </id>
        <property name="tName" column="t_name"></property>

        <!--//多对多-->
        <set name="setStu" table="tea_stu">
            <key><column name="t_id"></column></key>
            <many-to-many column="s_id" class="Stu"></many-to-many>
        </set>

    </class>
</hibernate-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值