输入映射和输出映射
parameterType(输入类型)
1 传递简单类型
使用#{}占位符,或者${}进行sql拼接。
2 传递pojo对象
Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。
3 传递pojo包装对象
开发中通过可以使用pojo传递查询条件。
查询条件可能是综合的查询条件,不仅包括用户查询条件还包括其它的查询条件(比如查询用户信息的时候,将用户购买商品信息也作为查询条件),这时可以使用包装对象传递输入参数。
包装对象:Pojo类中的一个属性是另外一个pojo。
需求:根据用户名模糊查询用户信息,查询条件放到QueryVo的user属性中。
- 编写QueryVo
public class QueryVo {
// 包含其他的pojo
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
- Sql语句
SELECT * FROM user WHERE username LIKE ‘%张%’
- Mapper.xml文件
在UserMapper.xml中配置sql,如下图。
- Mapper接口
在UserMapper接口中添加方法,如下图:
- 测试方法
在UserMapeprTest增加测试方法,如下:
_@_Test
public void testQueryUserByQueryVo() {
// mybatis和spring整合,整合之后,交给spring管理
SqlSession sqlSession = this.sqlSessionFactory.openSession();
// 创建Mapper接口的动态代理对象,整合之后,交给spring管理
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 使用userMapper执行查询,使用包装对象
QueryVo queryVo = new QueryVo();
// 设置user条件
User user = new User();
user.setUsername(“张”);
// 设置到包装对象中
queryVo.setUser(user);
// 执行查询
List list = userMapper.queryUserByQueryVo(queryVo);
for (User u : list) {
System.out.println(u);
}
// mybatis和spring整合,整合之后,交给spring管理
sqlSession.close();
}
- 效果
测试结果如下图:
resultType(输出类型)
1 输出简单类型
需求:查询用户表数据条数
sql:SELECT count(*) FROM user
- Mapper.xml文件
在UserMapper.xml中配置sql,如下图:
- Mapper接口
在UserMapper添加方法,如下图:
- 测试方法
在UserMapeprTest增加测试方法,如下:
@Testpublic void testQueryUserCount() {// mybatis和spring整合,整合之后,交给spring管理SqlSession sqlSession = this.sqlSessionFactory.openSession();// 创建Mapper接口的动态代理对象,整合之后,交给spring管理UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 使用userMapper执行查询用户数据条数int count = userMapper.queryUserCount();System.out.println(count);// mybatis和spring整合,整合之后,交给spring管理sqlSession.close();}
- 效果
测试结果如下图:
注意:输出简单类型必须查询出来的结果集有一条记录,最终将第一个字段的值转换为输出类型。
2 输出pojo对象
参考第一天内容
3 输出pojo列表
参考第一天内容
resultMap
resultType可以指定将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。
如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
需求:查询订单表order的所有数据-
sql:SELECT id, user_id, number, createtime, note FROM order
动态SQL
1 If
演示基于完成用户列表查询功能,由多查询条件拼装引出if标签。
<!-- 演示动态sql-if标签的使用情景 --><select id="getUserByWhere" parameterType="user" resultType="com.carven.mybatis.pojo.User"><!-- SELECT * FROM USER WHERE username LIKE '%${username}%' and id = #{id} -->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 where
<!-- 演示动态sql-where标签的使用情景 --><select id="getUserByWhere2" parameterType="user"resultType="com.carven.mybatis.pojo.User">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>
3 Foreach
<!-- 演示动态sql-foreach标签的使用情景 --><select id="getUserByIds" parameterType="queryvo"resultType="com.carven.mybatis.pojo.User">SELECT * FROM USER<!-- where会自动加上where同处理多余的and --><where><!-- id IN(1,10,25,30,34) --><!-- foreach循环标签collection:要遍历的集合,来源入参open:循环开始前的sqlseparator:分隔符close:循环结束拼接的sql--><foreach item="uid" collection="ids" open="id IN(" separator=","close=")">#{uid}</foreach></where></select>
4 SQL片段
<sql id="user_column">`id`,`username`,`birthday`,`sex`,`address`,`uuid2`</sql>使用方式:SELECT<!-- sql片段的使用:include:引入sql片段,refid引入片段id --><include refid="user_column" />FROM USER
关联查询
一对一
方式一: resultType
1.新建OrderUser的pojo,继承自Order。
public class OrderUser extends Order {private String username;private String address;……get,set}
2.修改order的映射文件,新增查询方法getOrderUser。
<!-- 一对一关联查询,使用resultType --><select id="getOrderUser" resultType="orderuser">SELECTo.`id`,o.`user_id` userId,o.`number`,o.`createtime`,o.`note`,u.`username`,u.`address`FROM `order` oLEFT JOIN `user` uON u.id = o.`user_id`</select>
方式二: resultMap
1.改造order的pojo
- 修改order的映射文件
<!-- 一对一关联查询-resultMap --><resultMap type="order" id="order_user_map"><!-- id标签用于绑定主键 --><id property="id" column="id"/><!-- 使用result绑定普通字段 --><result property="userId" column="user_id"/><result property="number" column="number"/><result property="createtime" column="createtime"/><result property="note" column="note"/><!-- association:配置一对一关联property:绑定的用户属性javaType:属性数据类型,支持别名--><association property="user" javaType="com.carven.mybatis.pojo.User"><id property="id" column="user_id"/><result property="username" column="username"/><result property="address" column="address"/><result property="sex" column="sex"/></association></resultMap><!-- 一对一关联查询-使用resultMap --><select id="getOrderUser2" resultMap="order_user_map">SELECTo.`id`,o.`user_id`,o.`number`,o.`createtime`,o.`note`,u.`username`,u.`address`,u.`sex`FROM `order` oLEFT JOIN `user` uON u.id = o.`user_id`</select>
一对多
- 改造user的pojo

- 修改user的映射文件
<!-- 一对多关联查询 --><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">SELECTu.`id`,u.`username`,u.`birthday`,u.`sex`,u.`address`,u.`uuid2`,o.`id` oid,o.`number`,o.`createtime`FROM `user` uLEFT JOIN `order` oON o.`user_id` = u.`id`</select>
