Springcloud学习笔记46--Mybatis-plus 使用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 {

    // 使函数参数对应xml中的参数wxNickName
    List 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:与