Good Java programmer learning path associated with the query share of MyBatis

  Good programmers Java learning routes Share MyBatis associated with the query, database query data we often need more than one table, you need to check out the data of multiple tables together, we studied the connection to query a database, then MyBatis in how will the relationship between multiple tables of data associated with the query it.

Structure of the table

Merchandise and order is typical of many relationship, we will be following the case and order merchandise table table as examples of how to configure the most common-many relationship, the following is the structure of two forms:

Commodity table:


Orders table:


Entity class design

A commodity with a plurality of orders, each order corresponding to a commodity, reflected in the relationship between the entity classes.

Orders entity classes:

/ **  *  Order class * / public class the Order {     Private ID Integer;     Private NUM Integer;     Private Integer goodsId;     Private Time String; / **      *  product line corresponding to the object * / Private Goods Goods;

 






    

     
    

Product entity classes:

/**
 * 商品类
 */
public class Goods {

    private Integer id;
    private String name;
    private Double price;
    private String address;
    /**
     * 订单集合
     */
    private List<Order> orders;

 

Mapper interface design

Here we only introduce related query, so only defines the basic query methods

/ **  *  product interfaces * / public interface GoodsDAO {     // The number query Goods     Goods selectById (int ID); }

 



 

/**
 * 订单接口
 */
public interface OrderDAO{
    //根据订单id查询订单
    Order selectById(int orderId);
    //根据商品id查询订单
    List<Order> selectByGoodsId(int goodsId);
}

 

collection标签

接下来就是重点了,我们在商品类中定义了订单集合属性orders,那么这个集合的数据如何进行查询呢?这就需要我们在mapper文件中使用collection标签。

collection标签用在resultMap标签中,用于配置集合的查询,用法是:

<collection property="集合属性名" column="传入查询方法的列名" select="查询集合所调用的方法"/>

示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--mapper是映射,namespace是设置对应的DAO接口-->
<mapper namespace="com.qianfeng.mybatis01.dao.GoodsDAO">

<!--配置数据库返回结果映射-->
    <resultMap id="goodsMap" type="com.qianfeng.mybatis01.entity.Goods">
        <!--id用于配置主键,property是实体类的属性名,column是表中的字段名-->
        <id property="id" column="goods_id"></id>
        <!--result配置主键外其他列-->
        <result property="name" column="goods_name"></result>
        <result property="price" column="goods_price"></result>
        <result property="address" column="goods_address"></result>
        <!--配置订单集合-->
        <collection property="orders" column="goods_id"
                    select="com.qianfeng.mybatis01.dao.OrderDAO.selectByGoodsId"/>
    </resultMap>
    <select id="selectById" parameterType="int" resultMap="goodsMap">
        select * from tb_goods where goods_id = #{id}
    </select>
</mapper>

collection的意思是:

当商品对象需要orders集合时,就会调用OrderDAOselectByGoodsId按商品id查询所有订单,商品id就是当前商品的goods_id值。

association标签

association标签的用法类似于collection,用于配置一对一的关系,每个订单中有一个商品对象goods,这个对象查询可以用association来进行配置。

用法:

<association property="对象属性名" column="传入查询方法的列名" select="查询对象所调用的方法"/>

示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--mapper是映射,namespace是设置对应的DAO接口-->
<mapper namespace="com.qianfeng.mybatis01.dao.OrderDAO">
    <!--配置数据库返回结果映射-->
    <resultMap id="orderMap" type="com.qianfeng.mybatis01.entity.Order">
        <!--id用于配置主键,property是实体类的属性名,column是表中的字段名-->
        <id property="id" column="order_id"></id>
        <!--result配置主键外其他列-->
        <result property="num" column="order_num"></result>
        <result property="goodsId" column="order_goods_id"></result>
        <result property="time" column="order_time"></result>
        <!--配置商品对象映射-->
        <association property="goods" column="order_goods_id"
                     select="com.qianfeng.mybatis01.dao.GoodsDAO.selectById"/>
    </resultMap>

    <select id="selectById" parameterType="int" resultMap="orderMap">
        select * from tb_order where order_id = #{id}
    </select>

    <select id="selectByGoodsId" parameterType="int" resultMap="orderMap">
        select * from tb_order where order_goods_id = #{goodsId}
    </select>
</mapper>

association的意思是:

当订单对象需要goods对象时,就调用GoodsDAOselectById按商品id查询商品,此商品id就是订单中的外键列order_goods_id

单元测试

@Test
public void testGoodsAndOrders(){
    GoodsDAO goodsDAO = MyBatisUtils.getSession().getMapper(GoodsDAO.class);
    //查询商品
    Goods goods = goodsDAO.selectById(1);
    System.out.println("查询商品:"+goods);
    //获得商品所有的订单
    goods.getOrders().stream().forEach((order)->System.out.println("商品的订单"+order));
    OrderDAO orderDAO = MyBatisUtils.getSession().getMapper(OrderDAO.class);
    //查询订单
    Order order = orderDAO.selectById(4);
    System.out.println("查询订单"+order);
    //获得订单对应的商品
    System.out.println("订单的商品:" + order.getGoods());
}

运行结果:

查询商品:Goods{id=1, name='小米9手机', price=2000.0, address='上海'}

商品的订单Order{id=1, num=2, goodsId=1, time='2019-9-12'}

商品的订单Order{id=4, num=4, goodsId=1, time='2019-9-14'}

查询订单Order{id=4, num=4, goodsId=1, time='2019-9-14'}

订单的商品:Goods{id=1, name='小米9手机', price=2000.0, address='上海'}

 

总结

MyBatis中配置表的关联关系,需要在resultMap中配置collectionassociation标签

collection配置的是一对多关系,property属性是集合的名称,select配置的是查询集合的方法,column配置查询方法的参数对应的列名

association配置的是一对一关系,property属性是对应对象的名称,select配置的是查询集合的方法,column配置查询方法的参数对应的列名

 

 


Guess you like

Origin blog.51cto.com/14479068/2440301
Recommended