注解开发也是很常见的,但是作为我来说,注解开始基本没有使用过,这次刚好也全部回忆一遍吧。常用注解如下:

  • @Insert:新增
  • @Update:修改
  • @Delete:删除
  • @Select:查询
  • @Result:结果集封装
  • @Results:与Result一起使用,封装多个结果集
  • @One:实现一对一封装
  • @Many:实现一对多封装

    具体的案例如下的,实际回顾完还是感觉xml方便,习惯使然吧。
    代码链接: https://gitee.com/wzlove521/lagou_study/tree/master/mybatis/mybatis_multitable

    CRUD

    ```java /**

    • 插入操作
    • @param user */ @Insert(“INSERT INTO user values (#{id}, #{name})”) void insertUser(User user);

      /**

    • 更新操作
    • @param user */ @Update(“UPDATE user SET name = #{name} WHERE id = #{id} “) void updateUser(User user);

      /**

    • 根据id删除
    • @param id */ @Delete(“DELETE FROM user WHERE id = #{id}”) void deleteUser(int id);

      /**

    • 根据id进行查询
    • @param id
    • @return */ @Select(“SELECT id, name FROM user WHERE id = #{id} “) User findById(int id);

// 测试 @Test public void testInsert() throws IOException { User user = new User(); user.setId(3); user.setName(“大智”); mapper.insertUser(user); sqlSession.commit(); sqlSession.close(); }

@Test public void testAnnoUpdate() throws IOException { User user = new User(); user.setId(3); user.setName(“智会”); mapper.updateUser(user); sqlSession.commit(); sqlSession.close(); }

@Test public void testAnnoSelect() throws IOException { User byId = mapper.findById(1); System.out.println(byId); sqlSession.close(); }

@Test public void testAnnoDelete() throws IOException { mapper.deleteUser(3); sqlSession.commit(); sqlSession.close(); }

  1. <a name="4YESH"></a>
  2. ### 一对一
  3. ```java
  4. /**
  5. * 注解一对一查询
  6. * @return
  7. */
  8. @Select("SELECT * FROM orders")
  9. @Results({
  10. @Result(column = "id", property = "id"),
  11. @Result(column = "name", property = "name"),
  12. @Result(column = "create_time", property = "createTime"),
  13. @Result(column = "user_id", property = "user", javaType = User.class,
  14. one = @One(select = "com.wangzhi.mapper.UserMapper.findById"))
  15. })
  16. List<Orders> selectAllByAnno();
  17. // 测试
  18. // 测试类进行了优化
  19. private OrdersMapper ordersMapper;
  20. private SqlSession sqlSession;
  21. @Before
  22. public void createS() throws IOException {
  23. InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
  24. sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
  25. ordersMapper = sqlSession.getMapper(OrdersMapper.class);
  26. }
  27. @Test
  28. public void testAnnoOneToOne() {
  29. List<Orders> orders = ordersMapper.selectAllByAnno();
  30. orders.forEach(System.out::println);
  31. }

一对多

  1. /**
  2. * 一对多查询
  3. * @return
  4. */
  5. @Select("SELECT * FROM user")
  6. @Results({
  7. @Result(column = "id", property = "id"),
  8. @Result(column = "name", property = "name"),
  9. @Result(column = "id", property = "ordersList", javaType = List.class,
  10. many = @Many(select = "com.wangzhi.mapper.OrdersMapper.selectByUserId"))
  11. })
  12. List<User> findAllByAnno();
  13. @Select("SELECT * FROM orders WHERE user_id = #{userId} ")
  14. List<Orders> selectByUserId(Integer userId);

多对多

  1. @Select("SELECT * FROM user")
  2. @Results({
  3. @Result(column = "id", property = "id"),
  4. @Result(column = "name", property = "name"),
  5. @Result(column = "id", property = "ordersList", javaType = List.class,
  6. many = @Many(select = "com.wangzhi.mapper.OrdersMapper.selectByUserId")),
  7. @Result(column = "id", property = "roles", javaType = List.class,many = @Many(select = "com.wangzhi.mapper.SysRoleMapper.selectByUserId"))
  8. })
  9. List<User> findAllByAnnoForManyToMany();
  10. @Select("SELECT * FROM sys_role LEFT JOIN sys_user_role sur on sys_role.id = sur.roleid WHERE sur.userid = #{userId} ")
  11. Role selectByUserId(Integer userId);

总结一下吧,其实复杂的映射相对xml来说是将sql进行了分离,原来一个sql,现在多个sql,还是那句话多对多的本质还是一对多,就按主方在哪一方,另一方连中间表进行查询。使用还是挺方便的,自我感觉比较清晰吧,个人还是喜欢xml方式的。