<!-- 实现手动映射封装 resultMap id="userResultMap" 此标签唯一标识 type="user" 封装后的实体类型 <id column="uid" property="id"></id> 表中主键字段封装 column="uid" 表中的字段名 property="id" user实体的属性名 <result column="NAME" property="username"></result> 表中普通字段封装 column="NAME" 表中的字段名 property="username" user实体的属性名 补充:如果有查询结果有 字段与属性是对应的,可以省略手动封装 【了解】 --> <resultMapid="userResultMap"type="user"> <idcolumn="uid"property="id"></id> <resultcolumn="NAME"property="username"></result> <resultcolumn="PASSWORD"property="username"></result> </resultMap> <selectid="findAllResultMap"resultMap="userResultMap"> SELECT id AS uid,username AS NAME,password AS PASSWORD FROM USER </select>
3)代码测试
1 2 3 4 5 6 7 8
@Test publicvoidtestFindAllResultMap()throws Exception { UserMapperuserMapper= sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findAllResultMap(); for (User user : list) { System.out.println(user); } }
1.2 多条件查询(三种)
需求 根据id和username查询user表
1)方式一
使用#{arg0}-#{argn}或者 #{param1}-#{paramn}获取参数
UserMapper接口
1 2 3
publicinterfaceUserMapper { public List<User> findByIdAndUsername1(Integer id, String username); }
UserMapper.xml
1 2 3 4 5 6
<mappernamespace="com.lagou.mapper.UserMapper"> <selectid="findByIdAndUsername1"resultType="user"> <!-- select * from user where id = #{arg0} and username = #{arg1} --> select * from user where id = #{param1} and username = #{param2} </select> </mapper>
publicinterfaceUserMapper { public List<User> findByIdAndUsername2(@Param("id") Integer id,@Param("username") String username); }
UserMapper.xml
1 2 3 4 5
<mappernamespace="com.lagou.mapper.UserMapper"> <selectid="findByIdAndUsername2"resultType="user"> select * from user where id = #{id} and username = #{username} </select> </mapper>
publicinterfaceUserMapper { public List<User> findByIdAndUsername3(User user); }
UserMapper.xml
1 2 3 4 5
<mappernamespace="com.lagou.mapper.UserMapper"> <selectid="findByIdAndUsername3"parameterType="com.lagou.domain.User"resultType="com.lagou.domain.User"> select * from user where id = #{id} and username = #{username} </select> </mapper>
publicinterfaceUserMapper { public List<User> findByUsername1(String username); }
UserMapper.xml
1 2 3 4 5
<mappernamespace="com.lagou.mapper.UserMapper"> <selectid="findByUsername1"parameterType="string"resultType="user"> select * from user where username like #{username} </select> </mapper>
测试
1 2 3 4 5 6 7 8
@Test publicvoidtestFindByUsername()throws Exception { UserMapperuserMapper= sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findByUsername1("%王%"); for (User user : list) { System.out.println(user); } }
2)方式二
UserMapper接口
1 2 3
publicinterfaceUserMapper { public List<User> findByUsername2(String username); }
UserMapper.xml
1 2 3 4 5 6
<mappernamespace="com.lagou.mapper.UserMapper"> <!--不推荐使用,因为会出现sql注入问题--> <selectid="findByUsername2"parameterType="string"resultType="user"> select * from user where username like '${value}' </select> </mapper>
测试
1 2 3 4 5 6 7 8
@Test publicvoidtestFindByUsername()throws Exception { UserMapperuserMapper= sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findByUsername2("%王%"); for (User user : list) { System.out.println(user); } }
publicinterfaceUserMapper { public List<User> findAllWithOrder(); }
3)UserMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<resultMapid="userMap"type="com.lagou.domain.User"> <idcolumn="id"property="id"></id> <resultcolumn="username"property="username"></result> <resultcolumn="birthday"property="birthday"></result> <resultcolumn="sex"property="sex"></result> <resultcolumn="address"property="address"></result> <!--一对多使用collection标签关联 property="orderList" 封装到集合的属性名 ofType="order" 封装集合的泛型类型 --> <collectionproperty="orderList"ofType="com.lagou.domain.Order"> <idcolumn="oid"property="id"></id> <resultcolumn="ordertime"property="ordertime"></result> <resultcolumn="money"property="money"></result> </collection> </resultMap> <selectid="findAllWithOrder"resultMap="userMap"> SELECT *,o.id oid FROM USER u LEFT JOIN orders o ON u.`id`=o.`uid`; </select>
4)测试代码
1 2 3 4 5 6 7 8
@Test publicvoidtestUserWithOrder()throws Exception { UserMapperuserMapper= sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findAllWithOrder(); for (User user : list) { System.out.println(user); } }
4.4 多对多
4.4.1 介绍
多对多查询的模型
多对多查询语句
1 2 3 4 5
SELECT* FROMUSER u -- 用户表 LEFTJOIN user_role ur -- 左外连接中间表 ON u.`id` = ur.`uid` LEFTJOIN role r -- 左外连接中间表 ON ur.`rid` = r.`id` ;
publicinterfaceUserMapper { public List<User> findAllWithRole(); }
3)UserMapper.xml映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<resultMapid="userAndRoleMap"type="com.lagou.domain.User"> <idcolumn="id"property="id"></id> <resultcolumn="username"property="username"></result> <resultcolumn="birthday"property="birthday"></result> <resultcolumn="sex"property="sex"></result> <resultcolumn="address"property="address"></result> <collectionproperty="orderList"ofType="com.lagou.domain.Role"> <idcolumn="rid"property="id"></id> <resultcolumn="role_name"property="roleName"></result> <resultcolumn="role_desc"property="roleDesc"></result> </collection> </resultMap> <selectid="findAllWithRole"resultMap="userAndRoleMap"> SELECT * FROM USER u LEFT JOIN user_role ur ON u.`id`=ur.`uid` INNER JOIN role r ON ur.`rid` = r.`id`; </select>
4)测试代码
1 2 3 4 5 6 7 8
@Test publicvoidtestUserWithRole()throws Exception { UserMapperuserMapper= sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findAllWithRole(); for (User user : list) { System.out.println(user); } }
4.5 小结
MyBatis多表配置方式
多对一(一对一)配置:使用<resultMap>+<association>做配置
一对多配置:使用<resultMap>+<collection>做配置
多对多配置:使用<resultMap>+<collection>做配置
多对多的配置跟一对多很相似,难度在于SQL语句的编写。
5. MyBatis嵌套查询
5.1 什么是嵌套查询
嵌套查询就是将原来多表查询中的联合查询语句拆成单个表的查询,再使用mybatis的语法嵌套在一起。
举个栗子
需求:查询一个订单,与此同时查询出该订单所属的用户
联合查询SELECT * FROM orders o LEFT JOIN USER u ON o.uid=u.id;
嵌套查询
先查询订单 SELECT * FROM orders
再根据订单uid外键,查询用户 SELECT * FROM user WHERE id = #{根据订单查询的uid}
最后使用mybatis,将以上二步嵌套起来
5.2 一对一嵌套查询
5.2.1 介绍
需求:查询一个订单,与此同时查询出该订单所属的用户
一对一查询语句
1 2 3 4 5
-- 先查询订单 SELECT*FROM orders;
-- 再根据订单uid外键,查询用户 SELECT*FROM `user` WHERE id = #{订单的uid};
5.2.2 代码实现
1)OrderMapper接口
1 2 3
publicinterfaceOrderMapper { public List<Order> findAllWithUser(); }
publicinterfaceRoleMapper { public List<Role> findByUid(Integer uid); }
4)RoleMapper.xml映射
1 2 3 4 5
<selectid="findByUid"parameterType="int"resultType="role"> SELECT r.id,r.`role_name` roleName,r.`role_desc` roleDesc FROM role r INNER JOIN user_role ur ON r.`id` = ur.`rid` WHERE ur.`uid` = #{uid} </select>
5)测试代码
1 2 3 4 5 6 7 8
@Test publicvoidtestUserWithRole()throws Exception { UserMapperuserMapper= sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findAllWithRole(); for (User user : list) { System.out.println(user); } }