myBatis知识点

myBatis知识点

Posted by Qc on April 30, 2019

#与$区别

id = "qc"
#{id}:防止sql注入('qc')
${id}:简单的字符串替换(qc)

单一入参

简单参数

#{随意参数}

${id}

需要在对应参数上添加注解

@Param("id")

或使用

${_parameter} 或 ${value}

POJO对象

#{对象属性名}

多个入参

自动封装为Map类型

map.put("param1", id);
map.put("param1", name);
map.put("paramN", ...);

Map遍历(foreach)

单一参数

parameterType="java.util.Map"
<foreach collection="map" index="key" item="val" open="" close="" separator="and">
  键:${key}
  值:#{val}
</foreach>

多个参数

param为传入Map中集合的属性

<foreach collection="param" index="key" item="val">
  键:${key}
  值:#{val}
</foreach>
<foreach collection="param.entrySet()" index="key" item="val">
  键:${key}
  值:#{val}
</foreach>
<foreach collection="param" item="val">
  值:#{val.id}、#{val.name}
</foreach>
<foreach collection="param.keys" item="key">
  键:${key}
  值:#{param[key]}
</foreach>
<foreach collection="param.values" item="val">
  值:#{val}
</foreach>

List遍历(foreach)

单一参数

传入List,collection=”list

传入数组,collection=”array

parameterType="java.util.List"
<foreach collection="list" index="index" item="val">
  #{val}
</foreach>

多个参数

封装为map

parameterType="java.util.HashMap"

param为传入Map中集合的属性

<foreach collection="param" index="index" item="val">
  #{val}
</foreach>

if

<if test="">
</if>

choose

<choose>
  <when test="">
  </when>
  <when test="">
  </when>
  <otherwise>
  </otherwise>
</choose>

trim

用于去除sql语句中多余的关键字,逗号,或者给sql语句前拼接 “where”、“set”以及“values(” 等前缀,或者添加“)”等后缀

属性 描述
prefix 给sql语句拼接的前缀
suffix 给sql语句拼接的后缀
prefixesToOverride 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixesToOverride属性指定,假设该属性指定为”AND”,当sql语句的开头为”AND”,trim标签将会去除该”AND”
suffixesToOverride 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixesToOverride属性指定

<where>等价于

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

<set>等价于

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

where

至少有一个查询条件时,插入where,并自动去除<where>内第一个and或or

<select>
  select * from table
  <where>
    <if test="">
      and x = y
    </if>
    <if test="">
      and y = z
    </if>
  </where>
</select>
select * from table where x = y and y = z

set

用于更新,动态处理逗号

<update>
  update table
  <set>
    <if test="id != null">id=#{id},</if>
    <if test="name != null">name=#{name}</if>
  </set>
</update>

bind

从 OGNL 表达式中创建一个变量并将其绑定到上下文

例如拼接like语句

<bind name="bindeName" value="'%' + eName + '%'"/>

<select>
  select * from table
  <if test="_parameter!=null">
    where id like #{bindeName}
  </if>
</select>

其他

参数验证

_parameter.containsKey('id')

返回布尔类型

代码复用

<sql id="model"></sql>
<select>
  <include refid="model"/>
</select>

分页处理(Java)

传入RowBounds对象

if (page > 0) {
    page--;
}
int offset = page * rows;
int limit = rows;
RowBounds rb = new RowBounds(offset, limit);

打印SQL(Spring)

mybatis.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

定义参数类型

方法一

mybatis.configuration.jdbc-type-for-null: ‘null’

方法二

#{id, jdbcType=VARCHAR}
JDBCType JavaType
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT boolean
BOOLEAN boolean
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB Clob
BLOB Blob
ARRAY Array
DISTINCT mapping of underlying type
STRUCT Struct
REF Ref
DATALINK java.net.URL