码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis学习笔记,问题总结

时间:2016-08-22 23:39:34      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:mybatis

1.parameterType和resultType

在映射文件中通过parameterType指定输入参数的类型,通过resultType指定输出结果的类型。

2.#{}和${}

#{}表示一个占位符号,#{}接收输入参数,类型可以是简单类型,pojohashmap

如果接收简单类型,#{}中可以写成value或其它名称。

#{}接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性...的方式获取对象属性值。

${}表示一个拼接符号,会引用sql注入,所以不建议使用${}

${}接收输入参数,类型可以是简单类型,pojohashmap

如果接收简单类型,${}中只能写成value

${}接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性...的方式获取对象属性值。

3.当传入的参数为一个pojo对象,来增加记录时

parameterType:指定输入 参数类型是pojo(包括 用户信息)

#{}中指定pojo的属性名,接收到pojo对象的属性值

id:标识 映射文件中的 sql,将sql语句封装到mappedStatement对象中,所以将id称为statement的id

<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>

4.当传入的参数为简单类型,来查询一条记录时

如果输入 参数是简单类型,#{}中的参数名可以任意,可以value或其它名称

resultType:指定sql输出结果 的所映射的java对象类型,select指定resultType表示将单条记录映射成的java对象。

<select id="findUserById" parameterType="int" resultType="user">
SELECT * FROM USER WHERE id=#{value}
</select>

5.根据用户名称模糊查询用户信息,返回一条或多条记录时

resultType:指定的仍是单条记录所映射的java对象

<select id="findUserByName" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User">
SELECT * FROM USER WHERE username LIKE ‘%${value}%‘
</select>

6.自增主键的表中添加用户,并将主键值返回到user对象中

<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<!-- 
将插入数据的主键返回到user对象中
SELECT LAST_INSERT_ID():得到刚insert进去记录的主键值,只适用与自增主键
keyProperty:将查询到主键值设置到parameterType指定的对象的哪个属性
order:SELECT LAST_INSERT_ID()相对于insert语句的执行顺序
resultType:指定SELECT LAST_INSERT_ID()的结果类型
 -->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>

7.使用mysql的uuid()生成主键的表中添加用户,并将主键值返回到user对象中

<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<!-- 
使用mysql的uuid()生成主键
执行过程:
首先通过uuid()得到主键,将主键设置到user对象的id属性中
其次在insert执行时,从user对象中取出id属性值
 -->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
SELECT uuid()
</selectKey>
insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address})
</insert>


8.sql片段的定义与使用

 sql片段的定义,id时sql片段的唯一标识

<sql id="query_user_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=‘‘">
and user.sex = #{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=‘‘">
and user.username LIKE ‘%${userCustom.username}%‘
</if>
<if test="ids!=null">
<!-- 使用 foreach遍历传入ids
collection:指定输入 对象中集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:结束遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
 -->
 <!-- 使用实现下边的sql拼接:
  AND (id=1 OR id=10 OR id=16) 
  -->
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
<!-- 每个遍历需要拼接的串 -->
id=#{user_id}
</foreach>
<!-- 实现  “ and id IN(1,10,16)”拼接 -->
<!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
每个遍历需要拼接的串
#{user_id}
</foreach> -->
</if>
</if>
</sql>

  sql片段的使用

<!-- 用户信息综合查询
#{userCustom.sex}:取出pojo包装对象中性别值
${userCustom.username}:取出pojo包装对象中用户名称
 -->
<select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo" 
resultType="cn.itcast.mybatis.po.UserCustom">
SELECT * FROM USER
<!-- 
where可以自动去掉条件中的第一个and
 -->
<where>
<!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 在这里还要引用其它的sql片段  -->
</where>
</select>

9.resultMap的使用

  定义resultMap

<!--
将SELECT id id_,username username_ FROM USER 和User类中的属性作一个映射关系
type:resultMap最终映射的java对象类型,可以使用别名
id:对resultMap的唯一标识
 -->
 <resultMap type="user" id="userResultMap">
 <!-- id表示查询结果集中唯一标识 
 column:查询出来的列名
 property:type指定的pojo类型中的属性名
 最终resultMap对column和property作一个映射关系 (对应关系)
 -->
 <id column="id_" property="id"/>
 <!-- 
 result:对普通名映射定义
 column:查询出来的列名
 property:type指定的pojo类型中的属性名
 最终resultMap对column和property作一个映射关系 (对应关系)
  -->
 <result column="username_" property="username"/>
 
 </resultMap>

  使用resultMap进行输出映射

<!-- resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前边需要加namespace
-->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
SELECT id id_,username username_ FROM USER WHERE id=#{value}
</select>



mybatis学习笔记,问题总结

标签:mybatis

原文地址:http://10986334.blog.51cto.com/10976334/1841245

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!