重点内容###动态SQL拼接与参数传递
parameterType:参数的数据类型,即定义输入到sql中的映射类型。
resultType:执行sql语句结果的数据类型,如果是pojo则应该给出全路径。
resultMap:结果映射,值为resultMap标签的id值。
#{id}:表示使用PreparedStatement设置占位符号并将输入变量id传到sql中。说白点,#{}作用就是占位符,相当于JDBC中的?。(可以这样理解:当碰到#{}时,用?代替,然后再通过{}里面的变量名找到对应的值填充,实质上就是原始的jdbc操作)
${id}:与字符串拼接无异,用来控制不需要预编译的地方
还有一点:Mybatis标签使用了OGNL表达式,它与#{}是不一样的机制,不要搞混
resultType和resultMap的区别:两种都是表示结果集与java对象的一直关系,好让Mybatis自动处理结果集,将结果集放到java对象中,当使用resultType时,比如resultType=“com.yhc.bean.Message”,我们就不用配置resultMap了,这时查询的结果集如何与pojo对象对应呢,列名对应属性名,不区分大小写,resultType也有其他应用方式,比如说可以放Map,resultType=“java.util.Map”,这样的话key为列名,value为查询的值,这样写大小写敏感
if标签
<select id="queryMessageList" parameterType="com.yhc.bean.Message" resultMap="MessageResult">
select id, command, description, content from message where 1=1
<if test="command != null and !"".equals(command.trim())">
and command = #{command}
</if>
<if test="description != null and !"".equals(description.trim())">
and description like '%' #{description} '%'
</if>
</select>
foreach标签
<delete id="deleteBatch" parameterType="java.util.List">
delete from message where id in (
<foreach collection="list" item="item" separator=",">
#{item}
</foreach>
)
</delete>
where标签
作用:自动实现SQL语句的where的功能
sql标签
<sql id="columns">id, command, description, content</sql>
//然后我们就可以引用它了
select <include refid="columns"/> from message
set标签
<update id="">
update message
<set>
<if test="command != null and !"".equals(command.trim())">
command = #{command}
</if>
<if test="description != null and !"".equals(description.trim())">
description = #{description}
</if>
</set>
where id = #{id}
</update>
trim标签
作用:组合功能
<trim prefix="where" suffix="" prefixOverrides="" suffixOverrides="">
</trim>
choose标签
作用:相当于if…else if …else if…else或者switch case
<choose>
<when test="">
</when>
<when test="">
</when>
<when test="">
</when>
<otherwise></otherwise>
</choose>