一, 输入映射和输出映射
1. parameterType(输入类型)
1.1 传递简单类型
- <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
- SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
- </select>
1.2 传递 pojo 对象
- <insert id="insertUser" parameterType="com.cenobitor.pojo.User">
- INSERT INTO USER (`username`,`birthday`,`sex`,`address`)
- VALUES (#{username},#{birthday},#{sex},#{address})
- </insert>
Mybatis 使用 ognl 表达式解析对象字段的值,#{} 或者 ${} 括号中的值为 pojo 属性名称.
1.3 传递 pojo 包装对象
<!-- 1,resultType: 如果要返回数据集合, 只需设定为每一个元素的数据类型
2, 包装的 pojo 取值通过 "." 来获取, 如取包装的 pojo 中 user 属性对象里的 username 属性的表达式为: user.username
-->
- <select id="getUserByQueryVo" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
- SELECT * FROM USER WHERE username LIKE '%${user.username}%'
- </select>
- 2. resultType(输出类型)
2.1 输出简单类型
- <!-- 查询用户总记录数, 演示返回简单类型 -->
- <select id="getUserCount" resultType="int">
- SELECT COUNT(1) FROM USER
- </select>
2.2 输出 pojo 对象:
- <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
- SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
- </select>
2.3 输出 pojo 列表
- <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
- SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE sex = #{sex}
- </select>
2.4 输出 resultMap
表字段与 pojo 属性不一致时引出的 resultMap.
<!-- resultMap 入门
type: 映射成的 pojo 类型
id:resultMap 唯一标识
-->
- <resultMap type="order" id="orderMap">
- <!-- id 标签用于绑定主键 -->
- <id property="id" column="id"/>
- <!-- 使用 result 绑定普通字段 -->
- <result property="number" column="number"/>
- <result property="createtime" column="createtime"/>
- <result property="note" column="note"/>
- </resultMap>
- <!-- 使用 resultMap -->
- <select id="getOrderListResultMap" resultMap="orderMap">
- SELECT * FROM `order`
- </select>
二, 动态 sql
2.1 If
由多查询条件拼装引出 if 标签.
- <!-- 演示动态 sql-if 标签的使用情景 -->
- <select id="getUserByWhere" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
- SELECT * FROM USER where 1 = 1
- <!-- if 标签的使用 -->
- <if test="id != null">
- and id = #{id}
- </if>
- <if test="username != null and username !=''">
- and username LIKE '%${username}%'
- </if>
- </select>
- 2.2 Where
- <!-- 演示动态 sql-where 标签的使用情景 -->
- <select id="getUserByWhere2" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
- <!-- include: 引入 sql 片段, refid 引入片段 id -->
- SELECT * FROM USER
- <!-- where 会自动加上 where 同处理多余的 and -->
- <where>
- <!-- if 标签的使用 -->
- <if test="id != null">
- and id = #{id}
- </if>
- <if test="username != null and username !=''">
- and username LIKE '%${username}%'
- </if>
- </where>
- </select>
- 2.3 Foreach
- <!-- 演示动态 sql-foreach 标签的使用情景 -->
- <select id="getUserByIds" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
- SELECT * FROM USER
- <!-- where 会自动加上 where 同处理多余的 and -->
- <where>
- <!-- id IN(1,10,25,30,34) -->
- <!-- foreach 循环标签
- collection: 要遍历的集合, 来源入参
- open: 循环开始前的 sql
- separator: 分隔符
- close: 循环结束拼接的 sql
- -->
- <foreach item="uid" collection="ids" open="id IN(" separator="," close=")">
- #{uid}
- </foreach>
- </where>
- </select>
2.4 Sql 片段
演示通过 select * 不好引出查询字段名, 抽取共用 sql 片段.
定义
- <!-- sql 片段 定义, id: 片段唯一标识 -->
- <sql id="user_column">
- `id`, `username`, `birthday`, `sex`, `address`, `uuid2`
- </sql>
使用
SELECT
<!-- sql 片段的使用: include: 引入 sql 片段, refid 引入片段 id -->
- <include refid="user_column" />
- FROM USER
三, 关联查询
3.1 一对一关联
方法一, 使用 resultType
- <!-- 一对一关联查询, 使用 resultType -->
- <select id="getOrderUser" resultType="orderuser">
- SELECT
o.`id`, o.`user_id` userId, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address`
- FROM `order` o
- LEFT JOIN `user` u
- ON u.id = o.`user_id`
- </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 一对多关联
- <!-- 一对多关联查询 -->
- <resultMap type="user" id="user_order_map">
- <id property="id" column="id" />
- <result property="username" column="username" />
- <result property="birthday" column="birthday" />
- <result property="address" column="address" />
- <result property="sex" column="sex" />
- <result property="uuid2" column="uuid2" />
- <!-- collection: 配置一对多关系
- property: 用户下的 order 属性
- ofType:property 的数据类型, 支持别名
- -->
- <collection property="orders" ofType="order">
- <!-- id 标签用于绑定主键 -->
- <id property="id" column="oid"/>
- <!-- 使用 result 绑定普通字段 -->
- <result property="userId" column="id"/>
- <result property="number" column="number"/>
- <result property="createtime" column="createtime"/>
- </collection>
- </resultMap>
- <!-- 一对多关联查询 -->
- <select id="getUserOrder" resultMap="user_order_map">
- SELECT
u.`id`, u.`username`,u.`birthday`,u.`sex`,u.`address`,u.`uuid2`,o.`id` oid,o.`number`,o.`createtime`
- FROM `user` u
- LEFT JOIN `order` o
- ON o.`user_id` = u.`id`
- </select>
来源: http://www.bubuko.com/infodetail-2583697.html