Mybatis编码随记 1. #{} 和 ${} 区别 #{}: 解析为SQL时,会将形参变量的值取出,并自动给其添加引号。该方式预编译后在把内容填入,预防SQL注入 ${}: 解析为SQL时,将形参变量的值直接取出,直接拼接显示在SQL中,该方式是拼接后再进行sql编译,存在安全隐患在where a=’${}’内填充 ‘ OR ‘1’=’1’ – 获得的效果是:where a=’’ or ‘1’=’1’ –’ (–注释了where后面的引号及其他内容,而or保证了where为true) 2. Mybatis映射器 insert主键自增 1 2 3 4 5 6 <insert id ="唯一标识" useGeneratedKeys ="true" keyProperty ="id" > insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})</insert >
resultMap 联表查询的结果一般使用resultType=”map”映射或者定义resultMap来映射 resultMap有嵌套/分步查询与单步查询,因避免使用分步查询以防止出现”N+1查询问题”1 2 3 4 5 6 7 8 9 <resultMap id ="该元素的唯一标识" type ="POJO类" > <id property ="POJO属性" column ="DB的列名/别名" jdbcType ="" javaType ="除HashMap均可省略" /> <result /> <association property ="studentcard" resultMap ="可选择引用map,或标签内映射id/result标签" > </association > <collection property ="" ofType ="指定集合元素的POJO类" > <id /> <result /> </collection > </resultMap >
3. 动态SQL if标签:条件判断 1 2 3 <if test ="判断条件为真执行" > SQL语句</if >
choose、when和otherwise标签(switch-case-default) 1 2 3 4 5 6 7 8 9 10 11 <choose > <when test ="判断条件1" > SQL语句1 </when > <when test ="判断条件2" > SQL语句2 </when > <otherwise > SQL语句3 </otherwise > </choose >
trim,where,set标签 :字符串拼接 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <trim prefix ="添加前缀" suffix ="添加后缀" prefixOverrides ="忽略前缀字符" suffixOverrides ="忽略后缀字符" > SQL语句</trim > <trim prefix ="WHERE" prefixOverrides ="AND |OR " > ...</trim > <trim prefix ="SET" suffixOverrides ="," > ...</trim >
foreach标签 对集合进行遍历(尤其是在构建 IN 条件语句的时候)
1 2 3 4 5 6 7 SELECT * FROM POST P WHERE ID in<foreach item ="item" index ="index" collection ="list|array|map|set的集合之一" open ="(" separator ="," close =")" > 参数值</foreach >