MyBatis Plus 中 xml 文件的一个例子

java 复制代码
package com.neuedu.hisweb.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.hisweb.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neuedu.hisweb.entity.vo.DepartmentVo;
import com.neuedu.hisweb.entity.vo.UserVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lynn
 * @since 2023-07-17
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("""
            <script>
                select u.id,username,realname,usetype,ci1.ConstantName usetypeName,
                	doctitleId,ci.ConstantName AS docTitle,
                	isscheduling,deptid,d.DeptName dept,
                	registLeID,r.RegistName registLe
                from
                	USER u
                	INNER JOIN constantitem ci1 ON u.usetype = ci1.ID
                    INNER JOIN constantitem ci ON u.DocTitleID = ci.ID
                    INNER JOIN department d ON u.DeptID = d.ID
                    INNER JOIN registlevel r ON u.RegistLeID = r.ID
                    <where>
                        and u.DelMark=1
                        <if test='userType != null and userType!=""' >
                            and `usetype`=#{userType}
                        </if>
                        <if test='dept != null and dept!=""' >
                            and u.deptid=#{dept}
                        </if>
                        <if test='docType != null and docType!=""' >
                            and u.doctitleId=#{docType}
                        </if>
                        <if test='keyword != null and keyword!=""'>
                            and `username` like CONCAT(CONCAT('%', #{keyword,jdbcType=VARCHAR}), '%')
                            or `realname` like CONCAT(CONCAT('%', #{keyword,jdbcType=VARCHAR}), '%')
                        </if>
                    </where>
            </script>
            """)
    Page<UserVo> selectPage(Page<UserVo> page, String keyword, String userType, String dept, String docType);

    @Update("update user set password=#{newPwd,jdbcType=VARCHAR} where id=#{id} and  password=#{oldPwd,jdbcType=VARCHAR}")
    boolean updatePwd(Integer uid,String oldPwd,String newPwd);

}

一、类与接口基础

  1. 继承关系与框架集成
    • UserMapper 继承了 BaseMapper<User>,这是 MyBatis - Plus 提供的基础 Mapper 接口。通过继承它,UserMapper 自动获得了一系列常用的 CRUD(创建、读取、更新、删除 )方法,如 insertdeleteByIdselectList 等,无需手动编写这些基础操作的 SQL,极大简化了数据库访问层代码开发。
    • @Mapper 注解是 MyBatis 体系中的标识,Spring Boot 会通过该注解识别这是一个 Mapper 接口,并自动为其创建代理对象,注入到需要使用的地方(如 Service 层 ),实现接口方法到 SQL 执行的映射。
  2. 实体类关联
    BaseMapper<User> 中的泛型 User 是项目中的实体类,对应数据库中的 user 表(通常 MyBatis - Plus 会根据实体类名默认关联表名,也可通过 @TableName 注解自定义 ),Mapper 接口的操作最终会作用于该实体类对应的数据库表。

二、selectPage 方法分析

  1. 分页查询功能
    • 返回值 Page<UserVo> 体现了 MyBatis - Plus 的分页特性。Page 是 MyBatis - Plus 提供的分页对象,包含了分页查询的关键信息,如当前页码、每页条数、总记录数、查询结果列表等。方法执行后,查询结果会封装到这个 Page 对象中,方便上层业务(如 Controller )获取分页数据和进行分页展示。
    • 方法参数 Page<UserVo> page 用于接收分页参数(比如当前页码、每页显示条数等 ),MyBatis - Plus 会基于该参数自动生成分页 SQL 片段(如 MySQL 中的 LIMIT 语句 ),实现物理分页。
  2. SQL 构建与动态条件
    • SQL 结构
      SQL 语句通过 <script> 标签包裹,表明这是一个动态 SQL(可包含条件判断、循环等逻辑 )。整体是一个多表连接查询,涉及 USER 表与 constantitemdepartmentregistlevel 等表的关联,查询结果会映射到 UserVo 实体类(VO 一般用于封装视图层需要的数据,可能包含多个表的关联字段 )。
    • 动态条件(<where> 标签内)
      • and u.DelMark=1:这是一个固定条件,用于筛选未被标记删除(假设 DelMark 字段为 1 表示有效数据 )的用户记录。
      • <if test='userType != null and userType!=""' > and usetype=#{userType} </if>:当传入的 userType 参数不为空时,添加条件筛选用户类型(usetype 字段 )等于 userType 的记录,实现按用户类型动态过滤。
      • <if test='dept != null and dept!=""' > and u.deptid=#{dept} </if>:类似上面,根据传入的 dept 参数,筛选部门 ID(deptid 字段 )匹配的用户,用于按部门过滤。
      • <if test='docType != null and docType!=""' > and u.doctitleId=#{docType} </if>:依据 docType 参数,筛选职称 ID(doctitleId 字段 )对应的用户,实现按职称过滤。
      • <if test='keyword != null and keyword!=""'> and (usernamelike CONCAT(CONCAT('%', #{keyword,jdbcType=VARCHAR}), '%') orrealname like CONCAT(CONCAT('%', #{keyword,jdbcType=VARCHAR}), '%')) </if>:当 keyword 参数不为空时,添加模糊查询条件,匹配用户名(username )或真实姓名(realname )中包含该关键词的记录,实现搜索功能。
    • 结果映射 :查询的字段(如 u.idusernamerealname 等 )会通过别名(如 ci1.ConstantName usetypeNameusetypeName )映射到 UserVo 的属性上,MyBatis 会自动将查询结果按照字段与属性的对应关系,封装到 UserVo 对象中,方便业务层直接使用。
  3. 多表关联逻辑
    通过 INNER JOIN 连接多个表(constantitem 关联两次、departmentregistlevel ),将用户表与用户类型、职称、所属部门、挂号级别等关联表进行连接,能够在一次查询中获取到用户的多维度信息,避免了多次查询,提升了查询效率,也满足了业务中对用户详细信息展示的需求(比如在页面上显示用户所属部门名称、职称名称等 )。

三、updatePwd 方法分析

  1. 更新功能与条件
    • 方法返回值 boolean 用于标识密码更新操作是否成功执行。MyBatis 会根据 SQL 执行结果(影响的记录行数 )来判断,若影响行数大于 0 则返回 true,否则返回 false,方便上层业务判断密码修改是否成功。
    • SQL 语句 update user set password=#{newPwd,jdbcType=VARCHAR} where id=#{id} and password=#{oldPwd,jdbcType=VARCHAR} 实现了密码更新逻辑:
      • set password=#{newPwd,jdbcType=VARCHAR}:将 user 表中对应记录的 password 字段更新为传入的新密码(newPwd )。
      • where id=#{id} and password=#{oldPwd,jdbcType=VARCHAR}:这是更新的条件,确保只有用户 ID(id )匹配,且原密码(oldPwd )正确的情况下,才会执行密码更新操作,保证了密码修改的安全性,防止误操作或恶意修改他人密码。
  2. 参数与类型处理
    方法参数 Integer uid(用户 ID )、String oldPwd(原密码 )、String newPwd(新密码 ),在 SQL 中通过 #{} 占位符引用,MyBatis 会自动进行参数替换,并根据 jdbcType=VARCHAR 等指定的类型信息,正确处理参数的类型转换和赋值,避免因类型不匹配导致的 SQL 执行错误。
相关推荐
一只小小汤圆2 小时前
如何xml序列化 和反序列化类中包含的类
xml·开发语言·c#
yang_xiao_wu_6 小时前
springboot+mybatis面试题
spring boot·后端·mybatis
曹勖之7 小时前
ROS2 工作空间中, CMakeLists.txt, setup.py和 package.xml的作用分别是?
xml·linux·服务器·ros2
南极Ou1 天前
Mybatis逆向工程详解(附源码文件)动态创建实体类、条件扩展类、Mapper接口、Mapper.xml映射文件
xml·java·mybatis
生产队队长1 天前
项目练习:使用mybatis的foreach标签,实现union all的拼接语句
mybatis
加什么瓦1 天前
Mybatis
java·开发语言·mybatis
机 _ 长1 天前
一键批量修改XML标签名称:告别手工修改,高效管理标注数据
xml
珹洺2 天前
MyBatis实战指南(七)MyBatis缓存机制
java·数据库·sql·安全·缓存·oracle·mybatis
結城2 天前
Spring Security如何拿到登录用户的信息
java·spring·mybatis