MyBatis学习笔记
来源:互联网 发布:网络缓存级别 影音先锋 编辑:程序博客网 时间:2024/05/27 16:43
一、参数传递
1.基本数据类型的传递:parameterType="int" #{id}
2.对象符合数据传递:parameterType="User" #{id} #{name} #{age}
3.参数完整定义:#{name,javaType=int,jdbcType=NUMERIC}
1.resultType的使用:
resultType="hashmap" //返回简单的HashMap
resultType="com.someapp.model.User" //返回JavaBeans或POJOs
2.给长类名定义别名:<typeAlias type="com.someapp.model.User" alias="User"/> resultType="User"
3.resultMap的使用:
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" /> <!--定义id字段-->
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
resultMap="userResultMap" //引用resultMap,用了resultMap就不要用resultType了
三、动态SQL(参照了OGNL表达式标准)
1.if
<select id="selectBlog1" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE state = 'active'
<if test="title != null">
AND title = #{title}
</if>
</select>
<select id="selectBlog2" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE state = 'active'
<if test="title != null">
AND title LIKE #{title}
</if>
<if test="author != null and author.name != null">
AND title LIKE #{author.name}
</if>
</select>
2.if-else
<select id="selectBlog3" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE state = 'active'
<choose>
<when test="title != null">
AND title LIKE #{title}
</when>
<when test="author != null and author.name != null">
AND title LIKE #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
3.where的使用(避免where后面直接跟动态SQL出现的问题)
<select id="selectBlog4" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE
<!--如果由被包含的标记返回任意内容,就仅仅插入“WHERE” 。而且,如
果以“AND”或“OR”开头的内容,那么就会跳过 WHERE 不插入。-->
<where>
<if test="state != null">
AND state = #{state}
</if>
<if test="title != null">
AND title LIKE #{title}
</if>
<if test="author != null and author.name != null">
AND title LIKE #{author.name}
</if>
</where>
</select>
3.set的使用
<!--在写更新语句set标签和这里where有相同的用法-->
<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
4.trim(比起where和set更加灵活的适配不同情况)
<!---通过trim自定义和where等价的形式->
<trim prefix="WHERE" prefixOverrides="AND|OR ">
</trim>
<!---通过trim自定义和set等价的形式->
<trim prefix="SET" prefixOverrides="AND|OR ">
</trim>
5.forEach
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT * FROM post p WHERE id IN
<!--collection表示集合,item表示集合中的一个元素,index表示元素对应的下标-->
<!--open表示在最开端添加的字符,separator表示每个元素之间的分隔符,close表示在末尾追加的字符-->
<foreach item="id" index="i" collection="idList" open="(" separator="," close=")">
#{id}
</foreach>
</select>
1.基本数据类型的传递:parameterType="int" #{id}
2.对象符合数据传递:parameterType="User" #{id} #{name} #{age}
3.参数完整定义:#{name,javaType=int,jdbcType=NUMERIC}
4.#{name}会创建预处理语句;${name}不会预处理,直接将值嵌入(出于安全考虑,应谨慎使用)
1.resultType的使用:
resultType="hashmap" //返回简单的HashMap
resultType="com.someapp.model.User" //返回JavaBeans或POJOs
2.给长类名定义别名:<typeAlias type="com.someapp.model.User" alias="User"/> resultType="User"
3.resultMap的使用:
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" /> <!--定义id字段-->
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
resultMap="userResultMap" //引用resultMap,用了resultMap就不要用resultType了
三、动态SQL(参照了OGNL表达式标准)
1.if
<select id="selectBlog1" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE state = 'active'
<if test="title != null">
AND title = #{title}
</if>
</select>
<select id="selectBlog2" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE state = 'active'
<if test="title != null">
AND title LIKE #{title}
</if>
<if test="author != null and author.name != null">
AND title LIKE #{author.name}
</if>
</select>
2.if-else
<select id="selectBlog3" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE state = 'active'
<choose>
<when test="title != null">
AND title LIKE #{title}
</when>
<when test="author != null and author.name != null">
AND title LIKE #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
3.where的使用(避免where后面直接跟动态SQL出现的问题)
<select id="selectBlog4" parameterType="Blog" resultType="Blog">
SELECT * FROM blog WHERE
<!--如果由被包含的标记返回任意内容,就仅仅插入“WHERE” 。而且,如
果以“AND”或“OR”开头的内容,那么就会跳过 WHERE 不插入。-->
<where>
<if test="state != null">
AND state = #{state}
</if>
<if test="title != null">
AND title LIKE #{title}
</if>
<if test="author != null and author.name != null">
AND title LIKE #{author.name}
</if>
</where>
</select>
3.set的使用
<!--在写更新语句set标签和这里where有相同的用法-->
<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
4.trim(比起where和set更加灵活的适配不同情况)
<!---通过trim自定义和where等价的形式->
<trim prefix="WHERE" prefixOverrides="AND|OR ">
</trim>
<!---通过trim自定义和set等价的形式->
<trim prefix="SET" prefixOverrides="AND|OR ">
</trim>
5.forEach
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT * FROM post p WHERE id IN
<!--collection表示集合,item表示集合中的一个元素,index表示元素对应的下标-->
<!--open表示在最开端添加的字符,separator表示每个元素之间的分隔符,close表示在末尾追加的字符-->
<foreach item="id" index="i" collection="idList" open="(" separator="," close=")">
#{id}
</foreach>
</select>
0 0
- mybatis 学习笔记
- MyBatis学习笔记
- mybatis学习笔记---入门
- MyBatis 学习笔记
- Mybatis学习笔记
- 【MyBatis学习笔记】系列
- Mybatis学习笔记
- mybatis学习笔记
- mybatis学习笔记
- MyBatis 学习笔记
- mybatis学习笔记
- mybatis学习笔记
- MyBatis学习笔记
- mybatis学习笔记
- Mybatis框架学习笔记
- Mybatis学习笔记
- MyBatis学习笔记
- MyBatis 学习笔记
- 这几条UI原则可以让你的设计提高(中)
- 头尾式动画
- 几种计算机视觉中常用的聚类算法(K-means, Agglomerative clustering, Mean shift, Spectral clustering)
- Scala 中的foreach和map方法比较
- Java持久层的三种查询方式
- MyBatis学习笔记
- 操作系统与网络实现 之十八(丙)
- PAT_乙级 1009 说反话
- C++单例模式如何进行释放控制
- Android Studio快捷键(Window,Mac)
- 2016.11.1第二题 音量调节 codevs 2278
- 62 & 63. Unique Paths I & II
- ubuntu如何在线安装java7?
- 特殊符号 UNICODE编码