一、控制台打印SQL日志
SpringBoot配置Mybatis打印SQL日志,在application.yml文件增加如下配置
mybatis
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
二、插入语句返回自增主键
- 使用 JDBC 方式返回自增主键
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
insert into sys_user( user_ name, user_password) values( #{userName}, #{userPassword})
</insert>
// 可以看到主要配置两个属性useGeneratedKeys=“true” 和 keyProperty=“id”,一般只有Mysql数据库支持主键自增
- 使用 selectKey 返回主键的值,支持自增和非自增
<insert idsert3>
insert into sys_user(user_name, user_password) values( #{userName} , #{userPassword})
<selectKey keyColumn="id" resultType="long" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
// 多了上面<selectKey>标签,order 属性的设置和使用的数据库有关,MySQL数据库中, order 属性设置的值是 AFTER,因为当前记录的主键值在 insert 语句执行成功后才能获取 而在 Oracle 数据库中, order 的值要设置为 BEFORE,这是因为 Oracle 中需要先从序列获取值,然后将值作为主键插入到数据库中
三、接口多个参数的用法
List<SysRole> selectRolesByUserAndRole( @Param (”user”) SysUser user,
@Param (”role ”) SysRole role);
1. 接口参数为JavaBean时,在SQL获取参数时,通过点取值的方式,如#{user.id}和#{role enabled}
2. SQL参数取值,其实是从Map中取参数,如果有加@Param注解,则会将注解的值作为key值,如果没有的话,就默认以参数名作为key值
四、如何编写模糊查询Like语句
- 在java代码中加 sql 通配符
String wildcardname = “%smi%”;
List<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like #{value}
</select>
- 在 sql 语句中拼接通配符,会引起 sql 注入
String wildcardname = “smi”;
List<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like "%"#{value}"%"
</select>
五、Mybatis动态SQL
- if用法
- 在where条件中使用if
<select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
select id, user_name from sys_user where 1 = 1
<if test="userName != null and userName != ''">
and user_name = #{userName}
</if>
</select>
- 在update中使用if
<update id="updateByidSelective">
update sys_user
set
<if test="userName != null and userName != ''">
user_name = #{userName}, //一般把要判断的条件放在前面,因为有逗号
</if>
id = #{id}
where id = #{id}
</update>
- 在insert中使用if
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
insert into sys_user(
user_name,
<if test="userEmail != null and userEmail != ''">
user_email,
</if>
user_password)
values(
#{userName},
<if test="userEmail != null and userEmail != ''">
#{userEmail}
</if>
)
</insert>
- choose用法
<select id="selectByIdOrUserName" resultType="tk.mybatis.simple.model.SysUser">
select id,user_name from sys_user
where 1 = 1
<choose>
<when test="id != null">
and id = #{id}
</when>
<otherwise>
and 1 = 2
</otherwise>
</choose>
</select>
- set、trim用法 set标签作用:如果该标签包含的元素中有返回值,就插入 set;如果 set 后面的字符串是以逗号结尾的,就将这个逗号剔除。
<update id="updateById">
update sys_user
<set>
<if test="userName != null">
user_name = #{userName},
</if>
id = #{id}
</set>
where id = #{id}
</update>
trim标签有如下属性:
<trim prefix="SET" suffixOverrides=",">
...
</trim>
- prefix:当 trim 元素内包含内容时,会给内容增加 prefix 指定的前缀。
- prefixOverrides:当 trim 元素内包含内容时,会把内容中匹配的前缀字符串去掉。
- suffix:当 trim 元素内包含内容时,会给内容增加 suffix 指定的后缀。
- suffixOverrides:当 trim 元素内包含内容时,会把内容中匹配的后缀字符串去掉。
- foreach用法 foreach 包含以下属性
- collection:必填,值为要选代循环的属性名。这个属性值的情况有很多。
- item:变量名,值为从法代对象中取出的每一个值。
- index:索引的属性名,在集合数组情况下值为当前索引值 当选代循环的对象是 Map 类型时,这个值为 Map的 key (键值)。
- open:整个循环内容开头的字符串。
- close:整个循环内容结尾的字符串。
- separator:每次循环的分隔符。
- foreach实现in集合
<select id="selectByList" resultType="tk.mybatis.simple.model.SysUser">
select id,user_name from sys_user
where id in
<foreach collection="list" open="(" close=")" separator="," item="id" index="i"
#{id}
</foreach>
</select>
- foreach实现批量插入
<insert id="insertList">
insert into sys_user(
user_name, user_password, user_email)
values
<foreach collection="list" item="user" separator=",">
( #{user.userName}, #{user.userPassword}, #{user.userEmail} )
</foreach>
</insert>
- foreach实现动态update
//接口
int updateByMap(Map<String, Object> map);
<update id="updateByMap">
update sys_user
set
<foreach collection="_parameter" item="val" index="key" separator=",">
${key} = #{val}
</foreach>
where id = #{id}
</update>
//这里的 key 做为列名,对应的val值为该列的值,注意这里没有通过@param注解指定参数名,因为Mybatis在内部的上下文中使用了默认值 _parameter 作为该参数的key,所以在XML中也使用 _parameter。
- bind用法 bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中。
例如这个模糊查询
<if test="userName != null and userName != ''">
<bind name = "userNameLike" value = "'%' + userName + '%'"/>
and user_name like #{userNameLike}
</if>
六、OGNL用法
- el or e2
- el and e2
- el == e2 el eq e2
- el ! = e2 el neq e2
- 小于: el lt e2
- 小于等于: el lte e2,gt (大于)、 gte (大于等于)
- el + e2 ,e2 1/e2 ,e1 - e2, e1 % e2
- !e 或 not ,非,取反
- e.method(args), 调用对象方法
- e.property,对象属性值
- e1[index] ,按索引取值( List 、数组和 Map)
- @class@method(args):调用类的静态方法
- @class@field :调用类的静态字段值