常用注解

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

    增删改查

    1. // 增加用户
    2. @Insert("insert into user values (#{id},#{name})")
    3. void insertUser(User user);
    4. // 更新用户
    5. @Update("update user set name = #{name} where id = #{id}")
    6. void updateUser(User user);
    7. // 查询
    8. @Select("select * from user")
    9. List<User> selectUser();
    10. // 删除
    11. @Delete("delete from user where id = #{id}")
    12. void deleteUser(Integer id);

    一对一查询

    ```java @Results({ @Result(property = “orderId”,column = “order_id”), @Result(property = “name”,column = “name”), @Result(property = “user”,column = “user_id”,javaType = User.class,

    1. one = @One(select = "com.lagou.entity.dao.UserDao.findUserById"))

    }) @Select(“select * from order“) List findOrderAndUser2();

@Select(“select * from user where id = #{id}”) User findUserById(Integer id);

  1. <a name="tBhBy"></a>
  2. ## 一对多查询
  3. ```java
  4. // 先查询order,返回的user_id字段用来当入参传给findUsers,再查询的结果返回封装
  5. @Results({
  6. @Result(property = "orderId",column = "order_id"),
  7. @Result(property = "name",column = "name"),
  8. @Result(property = "user",column = "user_id",javaType = List.class,
  9. many = @Many(select = "com.lagou.entity.dao.UserDao.findUsers"))
  10. })
  11. @Select("select * from `order`")
  12. List<Order> findOrderAndUser2();
  13. @Select("select * from user where id = #{id}")
  14. List<User> findUsers(Integer id);

多对多查询

  1. @Select("select * from user")
  2. @Result({
  3. @Result(property = "id",column = "id")
  4. @Result(property = "name",column = "name")
  5. @Result(property = "roleList",column = "id",javaType = List.class,
  6. many = @Many(select = "com.lagou.entity.dao.RolerDao.findRoleByUid"))
  7. })
  8. List<User> findAllUserAndRole();
  9. @Select("select * from role r,user_role ur where r.rid = ur.rid and ur.uid = #{uid}")
  10. List<Role> findRoleByUid(Integer uid);