Mybatis第三天(入参注意,连表查询,动态SQL)
入参注意
1.以多个参数入参(需要在每个参数前面添加@Param注解)
语法:public 返回值类型 方法名(@Param("形参1")参数类型1 实参1,@Param("形参2")参数类型2 实参2);
注意:此处的name和role相当于是起的别名,类似形参,查询语句的参数也需要写name和role
public List<User> getUserList(@Param("name")String userName,@Param("role")int userRole);
使用(参数用形参代替):
<select id="getUserList" resultType="user">
select * from smbms_user where userName like concat('%',#{name},'%') and userRole=#{role}
</select>
2.以对象入参
public List<User> getUserList(User user);
使用(如果入参的是一个对象,那么拿它的值,就直接写对应的这个值在实体类中的变量名即可):
<select id="getUserList" resultType="user" parameterType="user">
注意:如果入参的是一个对象,那么拿它的值,就直接写对应的这个值在实体类中的变量名即可,不需要get方法
select * from smbms_user where userName like concat('%',#{userName},'%') and userRole=#{userRole}
</select>
3.以map集合入参
public List<User> getUserList(Map<String, String>map);
使用(注意:如果是以map集合入参,那么拿值的时候,必须和map集中中的key值名字一样):
<select id="getUserList" resultType="user" parameterType="map">
注意:如果是以map集合入参,那么拿值的时候,必须和map集中中的key值名字一样
select * from smbms_user where userName like concat('%',#{uName},'%') and
userRole=#{uRole}
</select>
测试类拿值:需要与XXXMapper.xml中设定的key值一致
复杂查询(连表查询)
- 需要查询多个表并且使用到别的表中的某一个字段
在实体类中添加一个字段对应另外一个表中的某个数据 - 需要使用别的表中的多个字段
在实体类中添加某个实体类对象(某个实体类中添加另外一个实体类对象)association
<resultMap type="user" id="userList">
<!-- result:设置映射字段的标签 column属性:对应的是数据库中字段的名字(也可以是查询语句中设置的字段别名) property属性:对应的是要映射到的实体类中的属性名称 -->
<id column="id" property="id" />
<result property="code" column="userCode" />
<!-- 多表查询时,role表里的roleName映射到user实体类中的userRoleName字段 -->
<result property="userRoleName" column="roleName" />
<!-- 多表查询中,结果集包含两个数据表,此时要映射到user对象中,而user实体类中又添加了role实体类对象 javaType属性:指的是映射到role这个属性的数据类型 -->
<!-- <association property="role" javaType="Role"> <id column="id" property="id"
/> <result property="roleCode" column="roleCode" /> <result property="roleName"
column="roleName" /> </association> -->
<!-- 映射对象还可以使用association转resultMap的方式 -->
<association property="role" javaType="Role"
resultMap="roleList" />
</resultMap>
- 需要使用到别的表中的多个字段,并且是多条数据
XXXMapper.xml中使用collection标签进行映射
<resultMap type="user" id="userAddress">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="phone" column="phone" />
<!-- 映射address地址:address地址是一个集合 -->
<collection property="place" ofType="Address">
<!-- <id property="id" column="id" /> -->
<id property="contact" column="contact" />
<id property="addressDesc" column="addressDesc" />
</collection>
</resultMap>
动态sql
根据参数的传入(特定条件)来实现动态的拼接sql语句,实际上就是支持写if判断以及智能的忽略或者拼接and/or这样的连接语句
<select id="getUserList" resultMap="userList">
select * from smbms_user where
<if test="name!=null and name !=''">
userName like CONCAT('%',#{name},'%')
</if>
<!-- 判断传入的角色是否有选择 -->
<if test="userRole !=0">
and userRole=#{userRole}
</if>
</select>
if+where组合
<select id="getUserList" resultMap="userList">
select * from smbms_user
<!-- 会自动忽略and和or -->
<where>
<if test="name!=null and name !=''">
and userName like CONCAT('%',#{name},'%')
</if>
<!-- 判断传入的角色是否有选择 -->
<if test="userRole !=0">
and userRole=#{userRole}
</if>
</where>
</select>
if+trim组合
suffix="":前缀
prefix="":后缀
suffixOverrides="":前缀替换
prefixOverrides="":后缀替换
<trim prefix="where" prefixOverrides="and | or">
<if test="name!=null and name !=''">
and userName like CONCAT('%',#{name},'%')
</if>
<!-- 判断传入的角色是否有选择 -->
<if test="userRole !=0">
and userRole=#{userRole}
</if>
</trim>
if+set组合:修改用户信息
<set>
<if test="code!=null">userCode=#{code},</if>
<if test="userName!=null">userName=#{userName},</if>
<if test="userPassword!=null">userPassword=#{userPassword},</if>
<if test="gender!=null">gender=#{gender},</if>
<if test="birthday">birthday=#{birthday},</if>
<if test="phone!=null">phone=#{phone},</if>
<if test="address!=null">address=#{address},</if>
<if test="userRole!=null">userRole=#{userRole},</if>
<if test="createdBy!=null">createdBy=#{createdBy},</if>
<if test="creationDate!=null">creationDate=#{creationDate},</if>
<if test="modifyBy!=null">modifyBy=#{modifyBy},</if>
<if test="modifyDate!=null">modifyDate=#{modifyDate}</if>
</set>
foreach
foreach:遍历
collection:要遍历的参数类型
item:要遍历的参数名
open:要在遍历之前拼接的内容
separator:遍历一个值后,在值后面拼接的内容
close:遍历结束之后拼接的内容
<select id="getUserListByUserRole" resultMap="userList">
select * from smbms_user where userRole in
<foreach collection="array" item="roleIds" open="(" separator="," close=")">
#{roleIds}
</foreach>
</select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?