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);
}
一、类与接口基础
- 继承关系与框架集成
UserMapper
继承了BaseMapper<User>
,这是 MyBatis - Plus 提供的基础 Mapper 接口。通过继承它,UserMapper
自动获得了一系列常用的 CRUD(创建、读取、更新、删除 )方法,如insert
、deleteById
、selectList
等,无需手动编写这些基础操作的 SQL,极大简化了数据库访问层代码开发。@Mapper
注解是 MyBatis 体系中的标识,Spring Boot 会通过该注解识别这是一个 Mapper 接口,并自动为其创建代理对象,注入到需要使用的地方(如 Service 层 ),实现接口方法到 SQL 执行的映射。
- 实体类关联
BaseMapper<User>
中的泛型User
是项目中的实体类,对应数据库中的user
表(通常 MyBatis - Plus 会根据实体类名默认关联表名,也可通过@TableName
注解自定义 ),Mapper 接口的操作最终会作用于该实体类对应的数据库表。
二、selectPage
方法分析
- 分页查询功能
- 返回值
Page<UserVo>
体现了 MyBatis - Plus 的分页特性。Page
是 MyBatis - Plus 提供的分页对象,包含了分页查询的关键信息,如当前页码、每页条数、总记录数、查询结果列表等。方法执行后,查询结果会封装到这个Page
对象中,方便上层业务(如 Controller )获取分页数据和进行分页展示。 - 方法参数
Page<UserVo> page
用于接收分页参数(比如当前页码、每页显示条数等 ),MyBatis - Plus 会基于该参数自动生成分页 SQL 片段(如 MySQL 中的LIMIT
语句 ),实现物理分页。
- 返回值
- SQL 构建与动态条件
- SQL 结构 :
SQL 语句通过<script>
标签包裹,表明这是一个动态 SQL(可包含条件判断、循环等逻辑 )。整体是一个多表连接查询,涉及USER
表与constantitem
、department
、registlevel
等表的关联,查询结果会映射到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}), '%') or
realnamelike CONCAT(CONCAT('%', #{keyword,jdbcType=VARCHAR}), '%')) </if>
:当keyword
参数不为空时,添加模糊查询条件,匹配用户名(username
)或真实姓名(realname
)中包含该关键词的记录,实现搜索功能。
- 结果映射 :查询的字段(如
u.id
、username
、realname
等 )会通过别名(如ci1.ConstantName usetypeName
中usetypeName
)映射到UserVo
的属性上,MyBatis 会自动将查询结果按照字段与属性的对应关系,封装到UserVo
对象中,方便业务层直接使用。
- SQL 结构 :
- 多表关联逻辑
通过INNER JOIN
连接多个表(constantitem
关联两次、department
、registlevel
),将用户表与用户类型、职称、所属部门、挂号级别等关联表进行连接,能够在一次查询中获取到用户的多维度信息,避免了多次查询,提升了查询效率,也满足了业务中对用户详细信息展示的需求(比如在页面上显示用户所属部门名称、职称名称等 )。
三、updatePwd
方法分析
- 更新功能与条件
- 方法返回值
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
)正确的情况下,才会执行密码更新操作,保证了密码修改的安全性,防止误操作或恶意修改他人密码。
- 方法返回值
- 参数与类型处理
方法参数Integer uid
(用户 ID )、String oldPwd
(原密码 )、String newPwd
(新密码 ),在 SQL 中通过#{}
占位符引用,MyBatis 会自动进行参数替换,并根据jdbcType=VARCHAR
等指定的类型信息,正确处理参数的类型转换和赋值,避免因类型不匹配导致的 SQL 执行错误。