实现复杂关系映射之前我们可以在映射文件中通过配置
多表查询:一对一查询
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
对应的sql语句:
select * from orders;
select * from user where id=查询出订单的uid;
主要注解:
@Result(column = "id",property = "id")
one = @One(select = "com.study.mapper.UserMapper.findById")
步骤1:创建订单类
public class Order {
private int id;
private Date ordertime;
private double total;
//当前订单属于哪一个用户
private User user;
步骤2:创建OrderMapper接口
第一种方式:
public interface OrderMapper {
@Select("select *,o.id oid from orders o,user u where o.uid=u.id")
@Results({
@Result(column = "oid",property = "id"),
@Result(column = "ordertime",property = "ordertime"),
@Result(column = "total",property = "total"),
@Result(column = "uid",property = "user.id"),
@Result(column = "username",property = "user.username"),
@Result(column = "password",property = "user.password")
})
public List<Order> findAll();
}
第二种方式:
public interface OrderMapper {
@Select("select * from orders")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "ordertime",property = "ordertime"),
@Result(column = "total",property = "total"),
@Result(
property = "user", //要封装的属性名称
column = "uid", //根据那个字段去查询user表的数据
javaType = User.class, //要封装的实体类型
//select属性 代表查询那个接口的方法获得数据 UserMapper的findById方法
one = @One(select = "com.study.mapper.UserMapper.findById")
)
})
public List<Order> findAll();
}
步骤3:配置核心配置文件
<!--加载映射关系-->
<mappers>
<!--指定接口所在的包-->
<package name="com.study.mapper"></package>
</mappers>
步骤4:编写测试类
public class MyBatisTest2 {
private OrderMapper mapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
mapper = sqlSession.getMapper(OrderMapper.class);
}
@Test
public void testSave(){
List<Order> all = mapper.findAll();
for (Order order : all) {
System.out.println(order);
}
}
}
多表查询:一对多查询
1个用户对应多个订单
对应的sql语句:
select * from user;
select * from orders where uid=查询出用户的id;
使用主要注解:
@Many
@Result
步骤1:创建User类
public class User {
private int id;
private String username;
private String password;
private Date birthday;
//当前用户具有的订单
private List<Order> orderList;
}
步骤2: 修改OrderMapper接口:
@Select("select * from orders where uid=#{uid}")
public List<Order> findByUid(int uid);
步骤3:修改Usermapper接口
@Select("select * from user")
@Results({
<!--id=true代表标识为一个id-->
@Result(id=true ,column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "password",property = "password"),
<!--orderList与 User类里面的保持一致-->
@Result(
property = "orderList",
column = "id",
javaType = List.class,
many = @Many(select = "com.study.mapper.OrderMapper.findByUid")
)
})
public List<User> findUserAndOrderAll();
步骤4 核心配置文件导入mapper接口所在的包:
<!--加载映射关系-->
<mappers>
<!--指定接口所在的包-->
<package name="com.study.mapper"></package>
</mappers>
步骤5 测试:
public class MyBatisTest3 {
private UserMapper mapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testSave(){
List<User> userAndOrderAll = mapper.findUserAndOrderAll();
for (User user : userAndOrderAll) {
System.out.println(user);
}
}
}
多对多查询
用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询出该用户的所有角色
对应的sql语句:
select * from user;
select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=用户的id
步骤1:创建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;
}
步骤2:添加UserMapper接口方法
List<User> findAllUserAndRole();
步骤3:使用注解配置Mapper
public interface UserMapper {
@Select("select * from user")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "username",column = "username"),
@Result(property = "password",column = "password"),
@Result(property = "birthday",column = "birthday"),
@Result(property = "roleList",column = "id",
javaType = List.class,
many = @Many(select = "com.study.mapper.RoleMapper.findByUid"))
})
List<User> findAllUserAndRole();}
public interface RoleMapper {
@Select("select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=#{uid}")
List<Role> findByUid(int uid);
}
步骤4:测试结果
public class MyBatisTest4 {
private UserMapper mapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testSave(){
List<User> userAndRoleAll = mapper.findUserAndRoleAll();
for (User user : userAndRoleAll) {
System.out.println(user);
}
}
}