Mybatis多表查询
一对一查询
一对一查询的模型MapperScannerConfigurer
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
创建Order和User实体
public class Order {private int id;private Date ordertime;private double total;//代表当前订单从属于哪一个客户private User user;
}public class User {private int id;private String username;private String password;private Date birthday;}
创建接口
public interface OrderMapper {List<Order> findAll();
}
配置OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.controller.OrderMapper">
<resultMap id="orderMap" type="com.model.Order"><result column="id" property="id" /><result column="ordertime" property="ordertime"/><result column="total" property="total"/><association property="user" javaType="com.model.User"><result column="uid" property="id"/><result column="username" property="username"/><result column="password" property="password"/><result column="birthday" property="birthday"/></association>
</resultMap><select id="findAll" resultMap="orderMap">select * from orders o,user u where o.uid=u.id</select>
</mapper>
测试结果
@Testpublic void tset4() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);List<Order> all = mapper.findAll();System.out.println(all);}
一对多查询
一对多查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
1.2.3 修改User实体
public class Order {private int id;private Date ordertime;private double total;//代表当前订单从属于哪一个客户private User user;
}public class User {private int id;private String username;private String password;private Date birthday;//代表当前用户具备哪些订单private List<Order> orderList;
}
创建UserMapper接口
public interface UserMapper {List<User> findAll();
}
配置UserMapper.xml
<resultMap id="userMap" type="com.model.User"><result column="username" property="username" /><result column="password" property="password"/><result column="id" property="id"/><result column="birthday" property="birthday"/><collection property="orderList" ofType="com.model.Order"><result property="id" column="oid"/><result property="ordertime" column="ordertime"/><result property="total" column="total"/></collection></resultMap><select id="findAll" resultMap="userMap">select * ,o.id oid from user u left join orders o on u.id=o.uid</select>
测试结果
@Testpublic void tset4() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);userMapper mapper = sqlSession.getMapper(userMapper.class);List<User> all = mapper.findAll();System.out.println(all);}
多对多查询
多对多查询的模型
用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询出该用户的所有角色
创建Role实体,修改User实体
public class User {private int id;private String username;private String password;private Date birthday;//代表当前用户具备哪些订单private List<Order> orderList;//代表当前用户具备哪些角色private List<Role> roleList;
}public class Role {private int id;private String rolename;}
添加UserMapper接口方法
List<User> findAllUserAndRole();
配置UserMapper.xml
<resultMap id="roleMap" type="com.model.User"><result column="username" property="username" /><result column="password" property="password"/><result column="id" property="id"/><result column="birthday" property="birthday"/><collection property="roleList" ofType="com.model.Role"><result property="id" column="rid"/><result property="rolename" column="rolename"/></collection></resultMap><select id="findAllUserAndRole" resultMap="roleMap">select u.*,r.id rid,r.* from user u left join sys_user_role ur on u.id=ur.userid inner join sys_role r on r.id=ur.roleid</select>
测试结果
@Testpublic void tset4() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);userMapper mapper = sqlSession.getMapper(userMapper.class);List<User> all = mapper.findAllUserAndRole();System.out.println(all);}