MyBatis-plus中自定义SQL语句详解

前言:

  1. 能够使mybatis-plus像mybatis一样在xml中写SQL
  2. 前提是原本可以在项目中正常使用mybatis-plus
  3. 只需要三步

目录结构:

《MyBatis-plus中自定义SQL语句详解》 模块结构.jpeg

《MyBatis-plus中自定义SQL语句详解》 mapper文件下结构.jpeg

一. xml文件

在同目录下面复制一份xxxMapper.xm文件,修改名称为xxxExtMapper.xm

<?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.test.mapper.WxUserExtMapper">
<!--注意上面的namespace一定要修改-->

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.test.entity.WxUser">
        <id column="id" property="id" />
        <result column="created_at" property="createdAt" />
        <result column="updated_at" property="updatedAt" />
        <result column="openid" property="openid" />
        <result column="nickname" property="nickname" />
        <result column="sex" property="sex" />
        <result column="city" property="city" />
        <result column="province" property="province" />
        <result column="country" property="country" />
        <result column="headimgurl" property="headimgurl" />
        <result column="subscribe_time" property="subscribeTime" />
        <result column="telephone" property="telephone" />
        <result column="unionid" property="unionid" />
        <result column="remark" property="remark" />
        <result column="groupid" property="groupid" />
        <result column="source" property="source" />
        <result column="is_admin" property="isAdmin" />
    </resultMap>

    <!--以下是新增的方法-->
    <select id="selectMethod" resultType="com.test.entity.WxUser">
        select * from wx_user;
    </select>

    <select id="oneUser" parameterType="java.lang.Integer" resultType="com.test.entity.WxUser">
        select * from wx_user WHERE id = #{id};
    </select>
</mapper>

注意:1. namespace一定要修改成xxxExtMapper.java所对应的全名称
二. mapper文件

在同目录下面复制一份xxxMapper.java文件,修改名称为xxxExtMapper.java

package com.test.mapper;

import com.test.entity.WxUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface WxUserExtMapper {
   
    List<WxUser> selectMethod();

    WxUser oneUser(@Param("id") Integer id);
}
三. 引入xml文件路径

打开所在模块的下的pom文件,增加以下代码

<build>
    <resources>
        <!--引入静态文件-->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <!--引入mapper对应的xml文件-->
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>
  其他: 1. 在@Autowired  private WxUserExtMapper wxUserExtMapper; 的时候
           wxUserExtMapper会标红,不用理会
        2. 没有做第三步,在调用时会报异常(看了其他博主的解决方法也是):
           通用异常org.apache.ibatis.binding.BindingException: 
           Invalid bound statement (not found): com.test.mapper.WxUserExtMapper.oneUser
        3. 若将ExtMapper.xml文件放入静态文件目下的话,就需要更改mybatis.mapper-locations
           的路径(为xml文件的存放路径),所以在自动生成类的时候就需要调整好各个类的结构,
           以便增加自定义SQL的时候,项目结构不会看起来很乱
    原文作者:samgroves
    原文地址: https://www.jianshu.com/p/4c915d908ac7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞