• Springcloud学习笔记46Mybatisplus 使用XML编写动态sql简易方法以及sql的执行顺序;


    1. 入门简单案例

    1.1 在Resources文件夹下创建一个Mapper文件夹

    比如我们需要在User表中使用增删改查,创建PmQuartzConfigMapper.xml,对应MybatisPlus中的PmQuartzConfigMapper接口

    1.2 在application.yml中配置mapper文件夹的路径

    mybatis-plus:
      # xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
      mapper-locations: classpath:mapper/*.xml

    1.3 在PmQuartzConfigMapper.java中创建函数

    public interface PmQuartzConfigMapper extends BaseMapper<PmQuartzConfig> {
    
        // 使函数参数对应xml中的参数wxNickName
        List<PmQuartzConfig> getQuartzInfoByJobGroup(@Param("jobGroup") String jobGroup);
    }

    1.4 在PmQuartzConfigMapper.xml中写sql语句

    <?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="com.ttbank.flep.mapper.PmQuartzConfigMapper">
    
        <select id="getQuartzInfoByJobGroup" resultType="com.ttbank.flep.entity.PmQuartzConfig">
            select * from pm_quartz_config
            where job_group=#{jobGroup}
    
        </select>
    
    </mapper>

    1.5 controller中调用

    package com.ttbank.flep.controller;
    
    
    import com.ttbank.flep.entity.PmQuartzConfig;
    import com.ttbank.flep.mapper.PmQuartzConfigMapper;
    import com.ttbank.flep.service.IPmQuartzConfigService;
    import dto.Result;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * <p>
     *  前端控制器
     * </p>
     *
     * @author lucky
     * @since 2022-04-20
     */
    @RestController
    @RequestMapping("/pm-quartz-config")
    public class PmQuartzConfigController {
        @Autowired
        IPmQuartzConfigService pmQuartzConfigService;
    
        @Autowired
        PmQuartzConfigMapper pmQuartzConfigMapper;
    
    
        @PostMapping("/getQuartzInfoByJobGroup")
        public void getQuartzInfoByJobGroup(){
            List<PmQuartzConfig> quartzConfigList = pmQuartzConfigMapper.getQuartzInfoByJobGroup("test01");
            System.out.println("");
    
        }
    
    }

    postman调用:

    debug断点情况:

    2. MyBatis Mapper XML文件详解

    <?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="com.ttbank.flep.mapper.PmQuartzConfigMapper">
        <select id="getQuartzInfoByJobGroup" resultType="com.ttbank.flep.entity.PmQuartzConfig">
            select * from pm_quartz_config
            where job_group=#{jobGroup}
    
        </select>
    </mapper>

     namespace 就是XXXMapper.xml文件对应的Java接口XXXMapper.java

    2.1 select标签常用字段

    id:填写在XxxMapper.java接口中的方法名

    parameterType:填写参数的类型

    resultType:填写方法中返回值的类型,直接在Idea中右键选择copy reference;

    resultMap属性:与resultMap标签一起使用,填写resultMap标签中定义的id属性

    2.2 resultMap标签

    resultMap标签用于自定义封装结果;

    type(resultMap标签中属性):最终结果还是封装到实体类中,type就是指定封装到哪一个类中
    id:与<select>标签中的resultMap中的属性一直,一定要唯一

    • <id>:该标签是指定主键封装到实体类中的哪一个属性(可以省略)
    • <result>:该标签是其他的列封装到实体类中,一般只需填写实体类中的属性与表中列不同的项即可,property:填写实体类中的属性,column:填写表中的列名;
        <resultMap id="quartzMap" type="com.ttbank.flep.dto.QuartzDTO">
            <id property="id" column="id"/>
            <result property="jobName" column="job_name"/>
            <result property="jobGroup" column="job_group"/>
            <collection property="quartzDetailList" ofType="com.ttbank.flep.entity.PmQuartzDetail">
                <id property="id" column="bId"/>
                <result property="costTime" column="cost_time"/>
            </collection>
        </resultMap>
        <select id="getQuartzCostTime" resultMap="quartzMap">
            SELECT a.job_name, a.job_group,b.id bId,b.cost_time
            FROM pm_quartz_config a
            left join pm_quartz_detail b
            on a.job_group=b.job_group
        </select>

    加一个where 条件进行限制;

    pm_quartz_detail:

    pm_quartz_config

     2.2 sql的执行顺序分析

    sql语句定义的顺序

    (1) SELECT (2)DISTINCT <select_list>
    (3) FROM <left_table>
    (4) <join_type> JOIN <right_table>
    (5)         ON <join_condition>
    (6) WHERE <where_condition>
    (7) GROUP BY <group_by_list>
    (8) WITH {CUBE|ROLLUP}
    (9) HAVING <having_condition>
    (10) ORDER BY <order_by_condition>
    (11) LIMIT <limit_number>

    sql语句执行顺序

    (8) SELECT (9)DISTINCT<select_list>
    (1) FROM <left_table>
    (3) <join_type> JOIN <right_table>
    (2)         ON <join_condition>
    (4) WHERE <where_condition>
    (5) GROUP BY <group_by_list>
    (6) WITH {CUBE|ROLLUP}
    (7) HAVING <having_condition>
    (10) ORDER BY <order_by_list>
    (11) LIMIT <limit_number>

    3.Mybatis中的${}和#{}区别

    https://blog.csdn.net/super_DuoLa/article/details/121971500

    https://blog.csdn.net/li_w_ch/article/details/109754949

    参考文献:

    https://blog.csdn.net/qq_42669399/article/details/107499252

    https://blog.csdn.net/qq_42780864/article/details/88055480

  • 相关阅读:
    迭代器模式-Iterator
    kubernetes之一 k8s基本概念
    多线程中的异常处理
    MySQL高可用集群方案
    RabbitMQ的集群模式
    多线程中如何取消任务
    公钥、私钥、摘要、数字签名、证书
    将Ubuntu系统迁移到SSD固态操作
    Java线程的状态和状态转换
    Ubuntu18.04系统和软件安装记录
  • 原文地址:https://www.cnblogs.com/luckyplj/p/16202273.html
Copyright © 2020-2023  润新知