Mybatis

目录

动态sql

多表联查

类的设计

表的设计

MyBatis的关联查询的实现

查询用户,及用户下的所有订单信息


动态sql

高级查询带分页:

查询条件: 动态组合, 代码中, 逻辑代码去判断,生成一个SQL

<if>

<if test="条件"></if>

注意:

  1. 条件是一个String类型, 需要判断 null与"", 其他类型只判断null

  2. 条件中使用 <,需要转义: &lt;

如果是 if --- else if ---else if --- else

<choose>
<when test=""></when>
<when test=""></when>
<otherwise></otherwise>
</choose>

<where>

是where关键字, 可以自动处理第一个and,or, 把where的第一个and,or去掉

替换: where 1=1

<set>

替换关键字: set, 修改中使用

可以去掉最后一个逗号

<foreach>

循环标签:

根据一组id批量删除

<sql>

sql片段, 把重复SQL, 提取出来, 放到sql标签中, 在其他statement中引用sql片段, 做到重复sql不重复写

在实际开发中, 查询不需要直接写 * , 效率最低

查询的列,根据前端需要那些字段,就查询那些

多表联查

分类:

 

两个对象之间的关联关系不是一成不变的, 考虑的当前项目下对象之间的关系

类的设计

维护两个对象之间的关系: 关联属性

  • 单个的关联属性

  • 集合关联属性

一对一: 在对应类中添加单个对方类的对象作为关联属性

 

一对多: 在一的一方有一个集合关联属性, 多的一方, 单个关联属性

 

多对多关联: 双方都是一个集合关联属性

 

表的设计

一对一: 外键可以加在任意一方, 这个外键需要添加一个唯一约束

 

一对多: 一个一对一与一个对多组成

外键是加在多一方

 

 

多对多: 需要一个中间表来维护两张表的关系

 

MyBatis的关联查询的实现

  1. 查询订单信息,以及关联的用户信息

    第一种写法: 表连接的sql语句

  1. 在OrderMapper接口中定义一个方法

   public Order findOrderAndUserById(Integer oId);
  1. 在OrderMapper.xml文件中编写sql语句

  SELECT o.id,o.userid,o.state,o.createtime,
  u.id,u.name,u.sex,u.address,u.birthday
  FROM  tb_order o
  JOIN tb_user u
  ON o.userid = u.id
  WHERE o.id = 1
  1. 在sql映射文件中编写resultMap, 进行手动映射

   <resultMap id="orderMap" type="Order">
          <!--order的映射
              id: 主键映射
          -->
          <id column="id" property="id"/>
          <!--
            result: 非主键
            简单数据类型
          -->
          <result column="userid" property="userId"/>
          <result column="state" property="state"/>
          <result column="createtime" property="createTime"/>
          <!--关联属性: 单个  association
             property: 关联属性名
             javaType: 关联属性的类类型
          -->
          <association property="user" javaType="User" >
              <id column="uid" property="id"/>
              <result column="name" property="username"/>
              <result column="sex" property="sex"/>
              <result column="birthday" property="birthday"/>
              <result column="address" property="address"/>
          </association>
      </resultMap>

如果多表联查, 查询的表中相同的列名, 需要使用别名区分

发送的sql语句: 多表联查

第二种写法: 多表联查的sql,拆分为多条sql

 <resultMap id="orderMap2" type="Order">
        <id column="id" property="id"/>
        <result column="userid" property="userId"/>
        <result column="state" property="state"/>
        <result column="createtime" property="createTime"/>
        <!--关联属性: user
            select: 查询关联属性, 调用那个statementId的
                    如果是其他sql映射文件的statement: namespace.statementId
                    如果没有写namespace,表示从当前的sql映射文件查询statementId
            column:
        -->
        <association column="userid" property="user" javaType="User" select="com.fs.mapper.UserMapper.findById"/>
    </resultMap>
    <select id="findOrderAndUserById1" parameterType="int" resultMap="orderMap2">
        select o.id,o.userid,o.state,o.createtime from tb_order o
         WHERE o.id = #{id}
    </select>

查询用户,及用户下的所有订单信息

User 对Order是一对多

pojo的实现:

在User类: 添加一个集合属性: List<Order>

 

在UserMapper接口中, 添加一个方法

//根据id查询用户,以及该用户下的所有的订单
public User queryUserAndOrdersById(Integer uid);

在UserMapper.xml文件中编写sql以及映射:resultMap(做映射)

第一种写法: 表连接查询

 <resultMap id="userOrderMap" type="User" >
        <id column="id" property="id"/>
        <result column="name" property="username"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
       <!--关联属性 orders: 集合属性 collection
         property: 关联属性名
         javaType: 关联属性的数据类型 不需要写
         ofType: 关联的集合属性的元素类型
       -->
        <collection property="orders" ofType="Order" >
            <id column="oid" property="id"/>
            <result column="userid" property="userId"/>
            <result column="state" property="state"/>
            <result column="createtime" property="createTime"/>
        </collection>
   </resultMap>
    <select id="queryUserAndOrdersById" parameterType="int" resultMap="userOrderMap">
        SELECT o.id oid,o.userid,o.state,o.createtime,
            u.id,u.name,u.sex,u.address,u.birthday
            FROM tb_user u
            JOIN tb_order o
            ON o.userid = u.id
        WHERE u.id = #{uid}
    </select>

第二种写法: 拆分为多条sql语句

 <resultMap id="userOrderMap2" type="User">
        <id column="id" property="id"/>
        <result column="name" property="username"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
        <!--关联属性-->
        <collection property="orders" ofType="Order" select="com.fs.mapper.OrderMapper.findByUserId"  column="id"/>
    </resultMap>
    <select id="queryUserAndOrdersById2" parameterType="int" resultMap="userOrderMap2">
        SELECT u.id,u.name,u.sex,u.address,u.birthday
            FROM tb_user u
        WHERE u.id = #{uid}
    </select>
User(id=4, username=wangwu, password=null, sex=男, birthday=Sun Nov 12 00:00:00 CST 2000, address=上海, 
     orders=[Order(id=1, userId=4, createTime=Thu May 12 15:44:00 CST 2022, state=已完成, user=null, 
                   orderDetails=[
                       OrderDetail(id=1, productId=1, ordereId=1, num=1, price=5999.0, 
                                   product=Product(id=1, name=华为p40, price=5999.0, description=5G手机)), 
                       OrderDetail(id=2, productId=2, ordereId=1, num=1, price=1999.0, 
                                   product=Product(id=2, name=红米8, price=1999.0, description=性价比高))]), 
             Order(id=3, userId=4, createTime=Mon Aug 15 15:44:45 CST 2022, state=已付款, user=null, orderDetails=[OrderDetail(id=4, productId=3, ordereId=3, num=2, price=6999.0, product=Product(id=3, name=联想电脑, price=6999.0, description=I7的CPU))]), Order(id=5, userId=4, createTime=Sun Aug 14 15:45:25 CST 2022, state=已发货, user=null, orderDetails=[OrderDetail(id=6, productId=2, ordereId=5, num=1, price=1999.0, product=Product(id=2, name=红米8, price=1999.0, description=性价比高))])])
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kblzxj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值