2.1 MyBatis的常用注解

这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper

映射文件了。我们先围绕一些基本的CRUD来学习,再学习复杂映射多表操作。

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result 一起使用,封装多个结果集

@One:实现一对一结果集封装

@Many:实现一对多结果集封装

2.2 MyBatis的增删改查

我们完成简单的user表的增删改查的操作

  1. private UserMapper userMapper;
  2. @Before
  3. public void before() throws IOException {
  4. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
  5. SqlSessionFactory sqlSessionFactory = new
  6. SqlSessionFactoryBuilder().build(resourceAsStream);
  7. SqlSession sqlSession = sqlSessionFactory.openSession(true);
  8. userMapper = sqlSession.getMapper(UserMapper.class);
  9. }
  10. @Test
  11. public void testAdd() {
  12. User user = new User();
  13. user.setUsername("测试数据");
  14. user.setPassword("123");
  15. user.setBirthday(new Date());
  16. userMapper.add(user);
  17. }
  18. @Test
  19. public void testUpdate() throws IOException {
  20. User user = new User();
  21. user.setId(16);
  22. user.setUsername("测试数据修改");
  23. user.setPassword("abc");
  24. user.setBirthday(new Date());
  25. userMapper.update(user);
  26. }
  27. @Test
  28. public void testDelete() throws IOException {
  29. userMapper.delete(16);
  30. }
  31. @Test
  32. public void testFindById() throws IOException {
  33. User user = userMapper.findById(1);
  34. System.out.println(user);
  35. }
  36. @Test
  37. public void testFindAll() throws IOException {
  38. List<User> all = userMapper.findAll();
  39. for(User user : all){
  40. System.out.println(user);
  41. }
  42. }

修改MyBatis的核心配置文件,我们使用了注解替代的映射文件,所以我们只需要加载使用了注解的Mapper接口即可

  1. <mappers>
  2. <!--扫描使用注解的类-->
  3. <mapper class="com.itheima.mapper.UserMapper"></mapper>
  4. </mappers>

或者指定扫描包含映射关系的接口所在的包也可以

  1. <mappers>
  2. <!--扫描使用注解的类所在的包-->
  3. <package name="com.itheima.mapper"></package>
  4. </mappers>

2.3 MyBatis的注解实现复杂映射开发

实现复杂关系映射之前我们可以在映射文件中通过配置来实现,使用注解开发后,我们可以使用@Results注解,@Result注解,@One注解,@Many注解组合完成复杂关系的配置

06_Mybatis注解开发 - 图1

06_Mybatis注解开发 - 图2

2.4 一对一查询

2.4.1 一对一查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

06_Mybatis注解开发 - 图3

2.4.2 一对一查询的语句

对应的sql语句:

  1. select * from orders;
  2. select * from user where id=查询出订单的uid;

查询的结果如下:

06_Mybatis注解开发 - 图4

2.4.3 创建Order和User实体

  1. public class Order {
  2. private int id;
  3. private Date ordertime;
  4. private double total;
  5. //代表当前订单从属于哪一个客户
  6. private User user;
  7. }
  8. public class User {
  9. private int id;
  10. private String username;
  11. private String password;
  12. private Date birthday;
  13. }

2.4.4 创建OrderMapper接口

  1. public interface OrderMapper {
  2. List<Order> findAll();
  3. }

2.4.5 使用注解配置Mapper

  1. public interface OrderMapper {
  2. @Select("select * from orders")
  3. @Results({
  4. @Result(id=true,property = "id",column = "id"),
  5. @Result(property = "ordertime",column = "ordertime"),
  6. @Result(property = "total",column = "total"),
  7. @Result(property = "user",column = "uid",
  8. javaType = User.class,
  9. one = @One(select = "com.itheima.mapper.UserMapper.findById"))
  10. })
  11. List<Order> findAll();
  12. }
  1. public interface UserMapper {
  2. @Select("select * from user where id=#{id}")
  3. User findById(int id);
  4. }

2.4.6 测试结果

  1. @Test
  2. public void testSelectOrderAndUser() {
  3. List<Order> all = orderMapper.findAll();
  4. for(Order order : all){
  5. System.out.println(order);
  6. }
  7. }

06_Mybatis注解开发 - 图5

2.5 一对多查询

2.5.1 一对多查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单

06_Mybatis注解开发 - 图6

2.5.2 一对多查询的语句

对应的sql语句:

  1. select * from user;
  2. select * from orders where uid=查询出用户的id;

查询的结果如下:

06_Mybatis注解开发 - 图7

2.5.3 修改User实体

  1. public class Order {
  2. private int id;
  3. private Date ordertime;
  4. private double total;
  5. //代表当前订单从属于哪一个客户
  6. private User user;
  7. }
  8. public class User {
  9. private int id;
  10. private String username;
  11. private String password;
  12. private Date birthday;
  13. //代表当前用户具备哪些订单
  14. private List<Order> orderList;
  15. }

2.5.4 创建UserMapper接口

  1. List<User> findAllUserAndOrder();

2.5.5 使用注解配置Mapper

  1. public interface UserMapper {
  2. @Select("select * from user")
  3. @Results({
  4. @Result(id = true,property = "id",column = "id"),
  5. @Result(property = "username",column = "username"),
  6. @Result(property = "password",column = "password"),
  7. @Result(property = "birthday",column = "birthday"),
  8. @Result(property = "orderList",column = "id",
  9. javaType = List.class,
  10. many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid"))
  11. })
  12. List<User> findAllUserAndOrder();
  13. }
  14. public interface OrderMapper {
  15. @Select("select * from orders where uid=#{uid}")
  16. List<Order> findByUid(int uid);
  17. }

2.5.6 测试结果

  1. List<User> all = userMapper.findAllUserAndOrder();
  2. for(User user : all){
  3. System.out.println(user.getUsername());
  4. List<Order> orderList = user.getOrderList();
  5. for(Order order : orderList){
  6. System.out.println(order);
  7. }
  8. System.out.println("-----------------------------");
  9. }

06_Mybatis注解开发 - 图8

2.6 多对多查询

2.6.1 多对多查询的模型

用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用

多对多查询的需求:查询用户同时查询出该用户的所有角色

06_Mybatis注解开发 - 图9

2.6.2 多对多查询的语句

对应的sql语句:

  1. select * from user;
  2. select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=用户的id

查询的结果如下:

06_Mybatis注解开发 - 图10

2.6.3 创建Role实体,修改User实体

  1. public class User {
  2. private int id;
  3. private String username;
  4. private String password;
  5. private Date birthday;
  6. //代表当前用户具备哪些订单
  7. private List<Order> orderList;
  8. //代表当前用户具备哪些角色
  9. private List<Role> roleList;
  10. }
  11. public class Role {
  12. private int id;
  13. private String rolename;
  14. }

2.6.4 添加UserMapper接口方法

  1. List<User> findAllUserAndRole();

2.6.5 使用注解配置Mapper

  1. public interface UserMapper {
  2. @Select("select * from user")
  3. @Results({
  4. @Result(id = true,property = "id",column = "id"),
  5. @Result(property = "username",column = "username"),
  6. @Result(property = "password",column = "password"),
  7. @Result(property = "birthday",column = "birthday"),
  8. @Result(property = "roleList",column = "id",
  9. javaType = List.class,
  10. many = @Many(select = "com.itheima.mapper.RoleMapper.findByUid"))
  11. })
  12. List<User> findAllUserAndRole();}
  13. public interface RoleMapper {
  14. @Select("select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=#{uid}")
  15. List<Role> findByUid(int uid);
  16. }

2.6.6 测试结果

  1. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  2. List<User> all = mapper.findAllUserAndRole();
  3. for(User user : all){
  4. System.out.println(user.getUsername());
  5. List<Role> roleList = user.getRoleList();
  6. for(Role role : roleList){
  7. System.out.println(role);
  8. }
  9. System.out.println("----------------------------------");
  10. }

06_Mybatis注解开发 - 图11