SQL 映射文件只有很少的的几个(按照被定义的顺序列出)
官网地址:https://mybatis.net.cn/sqlmap-xml.html
<select id="findById" parametreType="int" resultType="blog">
select * from blog where `id` = #{id}
select>
可选标签。参数类的完全限定名或别名,如果是传参是对象,这是对象的绝对路径,例如:parameterType=“com.hjt.userInfo.entity.SysMenu”。如果是基本数据类型,就直接写。例如:parameterType=“Long”,parameterType=“java.util.List”
非必选标签。注意这里的非选是因为resultType和resultMap不能并存,两者能且只能选择一个。主要是用来定义一个返回结果集对象的全限定名或者别名。
非必选标签。注意这里的非选是因为resultType和resultMap不能并存,两者能且只能选择一个。
resultMap类型的结果集映射,也就是返回值类型(比如接收类型是一个对象:resultMap=“SysMenuResult”)
<resultMap type="com.hjt.userInfo.entity.SysDept" id="SysDeptResult">
<id property="deptId" column="dept_id" />
<result property="parentId" column="parent_id" />
<result property="ancestors" column="ancestors" />
<result property="deptName" column="dept_name" />
<result property="orderNum" column="order_num" />
<result property="leader" column="leader" />
<result property="phone" column="phone" />
<result property="email" column="email" />
<result property="status" column="status" />
<result property="delFlag" column="del_flag" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
resultMap>
也可以返回值是基本数据类型 eg: resultType=“Integer”
<insert id="insert" parameterType="blog">
insert into blog (`id`, `name`, `title`, `content`)
values (#{id}, #{name}, #{title}, #{content})
insert>
<update id="update" parameterType="blog">
update blog
set `name` = #{name}, `title` = #{title}, `content` = #{title}
where id = #{id}
update>
<delete id="delete" parameterType="int">
delete from blog where id = #{id}
delete>
| collection | 表示迭代集合的名称 |
|---|---|
| item | 表示本次迭代获取的元素,若collection为List、Set或者数组,则表示其中的元素;若collection为map,则代表key-value的value,该参数为必选 |
| open | 表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项 |
| close | 表示该语句以什么结束,最常用的是右括弧’)’,注意:mybatis会将该字符拼接到整体的sql语句之后,该参数为可选项 |
| separator | mybatis会在每次迭代后给sql语句append上separator属性指定的字符,该参数为可选项 |
| index | 在list、Set和数组中,index表示当前迭代的位置,在map中,index代指是元素的key,该参数是可选项。 |
<select id="testSelectAllSysDeptByDeptId" resultType="com.hjt.userInfo.entity.SysDept">
<include refid="selectDeptVo">include>
where d.dept_id in
<foreach collection="deptIds" item="item"
separator="," open="(" close=")">
#{item}
foreach>
select>
/***
* 根据deptId查询全部
* @param deptIds split(',')
* @return
*/
List<SysDept> testSelectAllSysDeptByDeptId(@Param("deptIds") List<Long> deptIds);
实际执行的样子:
==> Preparing: select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time from sys_dept d where d.dept_id in ( ? , ? , ? , ? )
先看第一个foreach:
1、是在括号里有多个参数,所以要open="(“开始,close=”)“结束,separator=”,"作为中间的分隔
2.@Param(“deptIds”) List deptIds中的 deptIds要和 DEBUG [main] - ==> Preparing: INSERT INTO sys_user(user_name, user_password, user_email, user_info, head_img, create_time) VALUES (?,?,?,?,?,?) , (?,?,?,?,?,?) 实际执行语句 ==> Preparing: update sys_dept set dept_name = case dept_id when ? then ? when ? then ? end where dept_id in ( ? , ? ) 事先写好sql语句 eg: mybatis的trim标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。 属性 描述 首先来看一个问题 如果这些条件没有一个能匹配上会发生什么?最终这条 SQL 会变成这样: 可以用where标签或者trim标签来解决这个问题 也可以用下面的where来解决/**
批量插入用户信息
*
@param userList
@return
*/
int insertList(List<SysUser> userList);
8.2 forearch批量插入
<insert id="insertList" useGeneratedKeys="true" keyProperty="id">
INSERT INTO sys_user(user_name, user_password, user_email, user_info, head_img, create_time)
VALUES
<foreach collection="list" item="user" separator=",">
(#{user.userName},#{user.userPassword},#{user.userEmail},#{user.userInfo},#{user.headImg,jdbcType=BLOB},#{user.createTime,jdbcType=TIMESTAMP})
foreach>
insert>
8.3 forearch批量修改
<update id="updateDeptNameById" parameterType="java.util.List">
update sys_dept set dept_name =
<foreach collection="sysDeptDtos" item="item" open="case dept_id" close="end"
separator=" ">
when #{item.deptId} then #{item.deptName}
foreach>
where dept_id in
<foreach collection="sysDeptDtos" item="item" index="index"
separator="," open="(" close=")">
#{item.deptId}
foreach>
update>
/***
* 批量修改
* @param sysDeptDtos
*/
void updateDeptNameById(@Param("sysDeptDtos")List<SysDeptDto> sysDeptDtos);
> Parameters: 366(Long), 测试3(String), 367(Long), 测试4(String), 366(Long), 367(Long)
< Updates: 29.include
<sql id="selectMenuVo">
select menu_id, menu_name, parent_id, order_num, path, name, component, iframe_url, is_frame, is_cache, menu_type, visible, status, ifnull(perms,'') as perms, icon, create_time
from sys_menu
sql>
<select id="checkMenuNameUnique" parameterType="com.hjt.userInfo.entity.SysMenu" resultMap="SysMenuResult">
<include refid="selectMenuVo"/>
where menu_name=#{menuName} and parent_id = #{parentId} limit 1
select>
10.if
<update id="updateDept" parameterType="com.hjt.userInfo.entity.SysDept">
update sys_dept
<set>
<if test="parentId != null and parentId != 0">parent_id = #{parentId},if>
<if test="deptName != null and deptName != ''">dept_name = #{deptName},if>
<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},if>
<if test="orderNum != null and orderNum != ''">order_num = #{orderNum},if>
<if test="leader != null">leader = #{leader},if>
<if test="phone != null">phone = #{phone},if>
<if test="email != null">email = #{email},if>
<if test="status != null and status != ''">status = #{status},if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},if>
update_time = sysdate()
set>
where dept_id = #{deptId}
update>
11.trim
prefix 给sql语句拼接的前缀
suffix 给sql语句拼接的后缀
prefixOverrides 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND"
suffixOverrides 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE
<if test="state != null">
state = #{state}
if>
<if test="title != null">
AND title like #{title}
if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
if>
select>
SELECT * FROM BLOG WHERE
12.where
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
if>
<if test="title != null">
AND title like #{title}
if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
if>
where>
select>