• MyBatis动态SQL多表操作


    动态SQL

    1. if-where标签
    <select id="selByCondition" resultMap="rm">
        select *
        from mybatis
        <where>
        <if test="status !=null">
           and STATUS=#{STATUS}
        if>
        <if test="companyName !=null and companyName !=''">
        and company_name like #{companyName}
        if>
        <if test="bracdName !=null and bracdName !=''">
        and bracd_name like #{bracdName}
        if>
        where>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    标签可以自动帮我们去掉and

    1. choose-when-ortherwise标签
    <select id="selByCondition2" resultMap="rm">
        select *
        from mybatis where
        <choose>
            <when test="status !=null">
                STATUS=#{STATUS}
            when>
            <when test="companyName !=null and companyName !=''">
                company_name like #{companyName}
            when>
            <when test="bracdName !=null and bracdName !=''">
                bracd_name like #{bracdName}
            when>
            <otherwise>1=1otherwise>
        choose>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. foreach标签
    <delete id="deleteById">
        delete frpm mybatis where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        foreach>;
    delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    多表操作

    一对一

    一个用户有一张订单
    在这里插入图片描述

    <association property="user" javaType="user">
    	<id column="uid" property="id">id>
    	<result column="username" property="username">result>
    	<result column="password" property="password">result>
    association>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过<association>把两张表对应的实体类连接起来,只不过是主键ID要用单独的标签

    • property: 当前实体(order)中的属性名称(private User user)
    • javaType: 当前实体(order)中的属性的类型(User)
    <select id="findAll" resultMap="orderMap">
       SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
    select>
    
    • 1
    • 2
    • 3

    SQL环节和原来没什么区别,同样也是通过resultMap把字段和属性映射封装

    一对多

    一个用户有多张订单
    在这里插入图片描述

    <collection property="orderList" ofType="order">
        
        <id column="oid" property="id">id>
        <result column="ordertime" property="ordertime">result>
        <result column="total" property="total">result>
    collection>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • property:集合名称,User实体中的orderlist属性
    • ofType:当前集合中的数据类型,就是order实体
    • 原有的User实体中加上一个表示“用户有哪些订单的属性” private List orderList;
    
    <select id="findAll" resultMap="userMap">
       SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
    select>
    
    • 1
    • 2
    • 3
    • 4

    多对多

    多用户多角色
    在这里插入图片描述
    多对多的建表原则是引入一张中间表,用于维护外键,就是一张表通过中间表找到另一张表

    和一对多的模型类似,先在User实体类中增添一个“用户具备哪些角色”的属性private ListroleList;其次配置Mapper文件:

    <collection property="roleList" ofType="role">
       <id column="roleId" property="id">id>
       <result column="roleName" property="roleName">result>
       <result column="roleDesc" property="roleDesc">result>
    collection>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    多表的连接是靠中间表,这点在Mapper文件中通过映射实现,具体是把两张外表的id(userId和roleId)在id标签中配置成同一个属性,就像这样:

    <id column="userId" property="id">id>
    <id column="roleId" property="id">id>
    
    • 1
    • 2

    SQL环节就得用多对多的套路了

    <select id="findUserAndRoleAll" resultMap="userRoleMap">
        SELECT * FROM USER u,user-role ur,role r WHERE u.id=ur.userId AND ur.roleId=r.id
    select>
    
    • 1
    • 2
    • 3

    多表操作时MyBatis确实减少了很多硬编码,每一次新的SQL只需要在标签里改几个属性就可以,只要理清字段与属性的映射关系,在MyBatis中进行多表操作就是一个“对号入座”。

  • 相关阅读:
    一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
    【Java】1608. 特殊数组的特征值---使用桶排序
    IDEA(2023)修改默认缓存目录
    初识AOS --------AOS学习笔记系列
    MySQL - mysql服务基本操作以及基本SQL语句与函数
    DI依赖注入和第三方bean管理以及核心容器
    11.摆花
    【新知实验室】腾讯云TRTC初体验
    提高企业研发效率核心关键在打造组合应用建设
    JVM 的可达性分析法和四种引用
  • 原文地址:https://blog.csdn.net/ximaiyao1984/article/details/128159588