Mybatis Plus 自定义 SQL

一、在 mapper 层自定义查询方法

java 复制代码
@Mapper
public interface UserTableMapper extends BaseMapper<UserTableEntity> {

    /**
     * 自定义查询方法
     * @param username 用户名
     */
    List<UserTableEntity> selectAllByUsername(@Param("username") String username);
}

@param 传参。

二、在 mapper.xml 文件中写下具体查询内容

一般是在 resoucesmapper 目录下自定义 xml 文件,我这边是 src/main/resources/mapper/UserTableMapper.xml

xml 复制代码
<mapper namespace="com.example.library.mapper.UserTableMapper">

  <sql id="Base_Column">
    id, username, password
  </sql>

  <select id="selectAllByUsername" resultType="com.example.library.entity.UserTableEntity">
    select <include refid = "Base_Column"/>
    from sys_user
    where username = #{username}
  </select>
</mapper>

select id 对应方法名,resultType 是实体类,通过 #{} 使用方法的参数。

相关推荐
Victor35629 分钟前
Redis(91)Redis的访问控制列表(ACL)是如何工作的?
后端
努力进修34 分钟前
Rust 语言入门基础教程:从环境搭建到 Cargo 工具链
开发语言·后端·rust
Victor35634 分钟前
Redis(90)如何配置Redis的身份验证?
后端
程序员爱钓鱼2 小时前
Python编程实战 - 函数与模块化编程 - 参数与返回值
后端·python·ipython
程序员爱钓鱼2 小时前
Python编程实战 - 函数与模块化编程 - 局部变量与全局变量
后端·python·ipython
摇滚侠5 小时前
Spring Boot3零基础教程,KafkaTemplate 发送消息,笔记77
java·spring boot·笔记·后端·kafka
计算机学长felix7 小时前
基于SpringBoot的“面向校园的助力跑腿系统”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·后端
紫荆鱼8 小时前
设计模式-迭代器模式(Iterator)
c++·后端·设计模式·迭代器模式
RainSky_9 小时前
LNMP 一键安装包部署 Django 项目
后端·django·1024程序员节
追逐时光者9 小时前
一个开源免费、轻量级的 Avalonia UI 控件库
后端·.net