MyBatis动态SQL多表操作

简介: 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>


标签可以自动帮我们去掉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=1</otherwise>
    </choose>
</select>
  1. foreach标签
<delete id="deleteById">
    delete frpm mybatis where id in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>;
</delete>


多表操作


一对一


一个用户有一张订单

50bc23bb7f08421fba8f7110b802e878.png


<association property="user" javaType="user">
  <id column="uid" property="id"></id>
  <result column="username" property="username"></result>
  <result column="password" property="password"></result>
</association>

通过<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>

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


一对多


一个用户有多张订单

b8411ca782a3463d9eefe70adc4ef278.png

<collection property="orderList" ofType="order">
    <!--封装order的数据-->
    <id column="oid" property="id"></id>
    <result column="ordertime" property="ordertime"></result>
    <result column="total" property="total"></result>
</collection>
  • property:集合名称,User实体中的orderlist属性
  • ofType:当前集合中的数据类型,就是order实体
  • 原有的User实体中加上一个表示“用户有哪些订单的属性” private List<Order> orderList;


<!--一对多的SQL-->
<select id="findAll" resultMap="userMap">
   SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
</select>


多对多


多用户多角色


b0ef28cc02d84db9b5edcff4ef1161bc.png



多对多的建表原则是引入一张中间表,用于维护外键,就是一张表通过中间表找到另一张表


和一对多的模型类似,先在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>

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

<id column="userId" property="id"></id>
<id column="roleId" property="id"></id>


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>


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

相关文章
|
5月前
|
SQL Java 数据库连接
MyBatis动态SQL多表操作
MyBatis动态SQL多表操作
|
3月前
|
SQL Java 数据库连接
MyBatis的多表操作
MyBatis的多表操作
19 0
|
7月前
|
XML Java 数据库连接
Mybatis的多表查询操作 1
Mybatis的多表查询操作
43 1
|
7月前
|
SQL XML Java
Mybatis的多表查询操作 2
Mybatis的多表查询操作
51 1
|
8月前
|
SQL Java 数据库连接
MyBatis多表操作
MyBatis多表操作
64 0
|
8月前
|
SQL Java 数据库连接
【MyBatis】查询语句汇总
【MyBatis】查询语句汇总
|
8月前
|
SQL Java 数据库连接
mybatis多表关联查询
mybatis多表关联查询
74 0
Mybatis:动态SQL分组查询
Mybatis:动态SQL分组查询
884 0
Mybatis:动态SQL分组查询
|
SQL Java 关系型数据库
MyBatis【多表查询与动态SQL使用】
MyBatis【多表查询与动态SQL使用】
MyBatis【多表查询与动态SQL使用】

热门文章

最新文章