欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis入门(二)—— 输入映射和输出映射、动态sql、关联查询

程序员文章站 2022-09-30 20:14:11
本文主要介绍MyBatis映射mapper.xml中,输入映射和输出映射、动态sql、关联查询等的写法。 ......

 一、输入映射和输出映射

1. parameterType(输入类型)

1.1 传递简单类型

1 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
2   SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
3 </select>

1.2 传递pojo对象

1 <insert id="insertUser" parameterType="com.cenobitor.pojo.User">
2      INSERT INTO USER (`username`,`birthday`,`sex`,`address`)
3      VALUES (#{username},#{birthday},#{sex},#{address})
4 </insert>

Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。

1.3 传递pojo包装对象

1 <!-- 1、resultType:如果要返回数据集合,只需设定为每一个元素的数据类型
2     2、 包装的pojo取值通过 "."来获取,如取包装的pojo中user属性对象里的username属性的表达式为:user.username
3 -->
4 <select id="getUserByQueryVo" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
5      SELECT * FROM USER WHERE username LIKE '%${user.username}%'
6 </select>

2. resultType(输出类型)

2.1 输出简单类型

1 <!-- 查询用户总记录数,演示返回简单类型 -->
2 <select id="getUserCount" resultType="int">
3   SELECT COUNT(1) FROM USER
4 </select>

2.2 输出pojo对象:

1 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
2    SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
3 </select>

2.3输出pojo列表

1 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
2    SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE sex = #{sex}
3 </select>

2.4 输出resultMap

字段与pojo属性不一致时引出的resultMap

 1 <!-- resultMap入门
 2   type:映射成的pojo类型
 3   id:resultMap唯一标识
 4 -->
 5 <resultMap type="order" id="orderMap">
 6 
 7   <!-- id标签用于绑定主键 -->
 8   <id property="id" column="id"/>
 9 
10   <!-- 使用result绑定普通字段 -->
11   <result property="number" column="number"/>
12   <result property="createtime" column="createtime"/>
13   <result property="note" column="note"/>
14 </resultMap>
15 
16 <!-- 使用resultMap -->
17 <select id="getOrderListResultMap" resultMap="orderMap">
18   SELECT * FROM `order`
19 </select>
二、动态sql

2.1  If

由多查询条件拼装引出if标签。

 1 <!-- 演示动态sql-if标签的使用情景 -->
 2 <select id="getUserByWhere" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
 3   SELECT * FROM USER where 1 = 1
 4   <!-- if标签的使用 -->
 5   <if test="id != null">
 6     and id = #{id}
 7   </if>
 8   <if test="username != null and username != ''">
 9     and username LIKE '%${username}%'
10   </if>
11 </select>

2.2 Where

 1 <!-- 演示动态sql-where标签的使用情景 -->
 2 <select id="getUserByWhere2" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
 3   <!-- include:引入sql片段,refid引入片段id -->
 4   SELECT * FROM USER
 5   <!-- where会自动加上where同处理多余的and -->
 6   <where>
 7     <!-- if标签的使用 -->
 8     <if test="id != null">
 9       and id = #{id}
10     </if>
11     <if test="username != null and username != ''">
12       and username LIKE '%${username}%'
13     </if>
14   </where>
15 </select>

 2.3 Foreach

 1 <!-- 演示动态sql-foreach标签的使用情景 -->
 2 <select id="getUserByIds" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
 3   SELECT * FROM USER
 4   <!-- where会自动加上where同处理多余的and -->
 5   <where>
 6     <!-- id IN(1,10,25,30,34) -->
 7     <!-- foreach循环标签 
 8         collection:要遍历的集合,来源入参 
 9         open:循环开始前的sql 
10         separator:分隔符 
11         close:循环结束拼接的sql
12     -->
13     <foreach item="uid" collection="ids" open="id IN(" separator="," close=")">
14       #{uid}
15     </foreach>
16   </where>
17 </select>

2.4 Sql片段

演示通过select * 不好引出查询字段名,抽取共用sql片段。

① 定义

1 <!-- sql片段 定义,id:片段唯一标识 -->
2 <sql id="user_column">
3   `id`, `username`, `birthday`, `sex`, `address`, `uuid2`
4 </sql>

② 使用

1 SELECT
2 <!-- sql片段的使用:include:引入sql片段,refid引入片段id -->
3 <include refid="user_column" />
4 FROM USER
 三、关联查询

3.1 一对一关联

① 方法一,使用resultType

1 <!-- 一对一关联查询,使用resultType -->
2 <select id="getOrderUser" resultType="orderuser">
3   SELECT 
4     o.`id`, o.`user_id` userId, o.`number`, o.`createtime`, o.`note`, u.`username`,  u.`address`
5   FROM `order` o
6   LEFT JOIN `user` u
7   ON u.id = o.`user_id`
8 </select>

②方法二,使用resultMap

1 <!-- 一对一关联查询-resultMap --> 2 <resultMap type="order" id="order_user_map"> 3   <!-- id标签用于绑定主键 --> 4   <id property="id" column="id"/> 5   <!-- 使用result绑定普通字段 --> 6   <result property="userId" column="user_id"/> 7   <result property="number" column="number"/> 8   <result property="createtime" column="createtime"/> 9   <result property="note" column="note"/> 10   <!-- association:配置一对一关联 11     property:绑定的用户属性 12     javaType:属性数据类型,支持别名 13    --> 14   <association property="user" javaType="com.cenobitor.mybatis.pojo.User"> 15     <id property="id" column="user_id"/> 16     <result property="username" column="username"/> 17     <result property="address" column="address"/> 18     <result property="sex" column="sex"/> 19   </association> 20 </resultMap> 21 22 <!-- 一对一关联查询-使用resultMap --> 23 <select id="getOrderUser2" resultMap="order_user_map"> 24   SELECT 25   o.`id`,o.`user_id`, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address`, u.`sex` 26   FROM `order` o 27   LEFT JOIN `user` u 28   ON u.id = o.`user_id` 29 </select>

3.2一对多关联

 1 <!-- 一对多关联查询 -->
 2 <resultMap type="user" id="user_order_map">
 3   <id property="id" column="id" />
 4   <result property="username" column="username" />
 5   <result property="birthday" column="birthday" />
 6   <result property="address" column="address" />
 7   <result property="sex" column="sex" />
 8   <result property="uuid2" column="uuid2" />
 9   <!-- collection:配置一对多关系
10     property:用户下的order属性
11     ofType:property的数据类型,支持别名
12   -->
13   <collection property="orders" ofType="order">
14     <!-- id标签用于绑定主键 -->
15     <id property="id" column="oid"/>
16     <!-- 使用result绑定普通字段 -->
17     <result property="userId" column="id"/>
18     <result property="number" column="number"/>
19     <result property="createtime" column="createtime"/>
20   </collection>
21 </resultMap>
22 <!-- 一对多关联查询 -->
23 <select id="getUserOrder" resultMap="user_order_map">
24   SELECT
25   u.`id`, u.`username`,u.`birthday`,u.`sex`,u.`address`,u.`uuid2`,o.`id` oid,o.`number`,o.`createtime`
26   FROM `user` u
27   LEFT JOIN `order` o
28   ON o.`user_id` = u.`id`
29 </select>