前置
你使用
- java
- mybatis/mybatis plus
如果你使用 mybatis plus,也是会向下兼容 mybatis 的
mapper 对应 xml 基础配置
<?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.example.mapper.usermapper"> <!-- sql 映射语句 --> </mapper>
简单查询:
<!-- 简单查询 --> <select id="getuserbyid" resulttype="com.example.model.user"> select * from user where id = #{id} </select>
- id:与 mapper 接口中的方法名一致
- resulttype:返回值类型(java 类的全限定名或别名)
- #{id}:预编译参数,防止 sql 注入
- 如果返回 list,直接使用 resulttype 即可,无需额外配置
简单插入:
<insert id="insertuser" parametertype="com.example.model.user"> insert into user (name, age) values (#{name}, #{age}) </insert>
parametertype:输入参数类型(可选,mybatis 可自动推断)
简单更新:
<update id="updateuser" parametertype="com.example.model.user"> update user set name = #{name}, age = #{age} where id = #{id} </update>
简单删除:
<delete id="deleteuserbyid" parametertype="int"> delete from user where id = #{id} </delete>
mapper 对应 xml 复杂配置
resultmap 配置
当数据库字段名与 java 对象属性名不一致时,需使用 映射。一般都需要配置
<resultmap id="userresultmap" type="com.example.model.user"> <id property="id" column="user_id"/> <!-- 主键映射 --> <result property="name" column="user_name"/> <!-- 字段映射 --> <result property="age" column="user_age"/> </resultmap> <select id="getuserbyid" resultmap="userresultmap"> select user_id, user_name, user_age from user where user_id = #{id} </select>
if 条件
<select id="searchusers" resulttype="com.example.model.user"> select * from user <where> <if test="name != null anconcatd name != ''"> and name like concat('%', #{name}, '%') </if> <if test="age != null"> and age = #{age} </if> </where> </select>
在mybatis中,如果不写这样的条件判断,如果是对表的写操作,如 update,其中的 set 中当name参数为null时,生成的sql语句会直接包含name = null,从而将数据库中的对应字段更新为null。这是mybatis的默认行为,需要开发者显式控制是否要过滤null值
多条件选择 choose
类似于 java 中的 switch-case 或 if-else 逻辑<choose>
的 <when>
是按顺序判断的,一旦某个条件满足,后续条件不再判断
<select id="finduser" resulttype="com.example.model.user"> select * from user <where> <choose> <when test="type == 'name'"> and name = #{value} </when> <when test="type == 'email'"> and email = #{value} </when> <otherwise> and status = 'active' </otherwise> </choose> </where> </select>
<otherwise>
是可选的,但如果省略且所有 <when>
条件都不满足,会导致 sql 语句不完整或无效
遍历集合 foreach
<select id="getusersbyids" resulttype="com.example.model.user"> select * from user where id in <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id} </foreach> </select>
分页查询
<select id="getusersbypage" resulttype="com.example.model.user"> select * from user limit #{offset}, #{limit} </select>
你也可以使用 pagehelper 工具,更常用,那就不需要 limit offset
如果你使用 mybatis plus,也有自己的方式,可以使用 ipage 来分页
mapper 中的相关注解
@param注解
@select("select * from user where name = #{user_name} and age = #{user_age}") user getuserbynameandage(@param("user_name") string username, @param("user_age") int userage);
如果没写 @param,mybatis 默认将未使用 @param 注解的参数封装为 map,键名为 param1、param2、…、paramn(按参数顺序递增),也就是说 sql 中应该写 #{param1}
和 #{param2}
其他
如果你不想使用 resultmap 来映射 mysql column 和 entity 的 field,还有其他办法吗?
方法一,使用 mybatis plus 的 @tablefield 注解来指定 column 和 field 的映射
方法二:xml 中使用别名
<select id="getuserbyid" resulttype="com.example.model.user"> select user_id as id, user_name as name, user_age as age from user </select>
choose when 和 if 区别
if 是独立的判断,而 choose when 更像 ifelse 逻辑,一旦判断一个满足,后续的 when 就不执行了!
到此这篇关于mybatis的mapper对应的xml写法的文章就介绍到这了,更多相关mybatis mapper对应的xml写法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论