常用注解
- @Insert :实现新增
- @Update:修改
- @Delete:删除
- @Select:查询
- @Result:结果集封装
- @Results:跟result一起用,封装多个结果集
- @One:一对一结果集封装
-
增删改查
// 增加用户
@Insert("insert into user values (#{id},#{name})")
void insertUser(User user);
// 更新用户
@Update("update user set name = #{name} where id = #{id}")
void updateUser(User user);
// 查询
@Select("select * from user")
List<User> selectUser();
// 删除
@Delete("delete from user where id = #{id}")
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,
one = @One(select = "com.lagou.entity.dao.UserDao.findUserById"))
}) @Select(“select * from
order
“) ListfindOrderAndUser2();
@Select(“select * from user where id = #{id}”) User findUserById(Integer id);
<a name="tBhBy"></a>
## 一对多查询
```java
// 先查询order,返回的user_id字段用来当入参传给findUsers,再查询的结果返回封装
@Results({
@Result(property = "orderId",column = "order_id"),
@Result(property = "name",column = "name"),
@Result(property = "user",column = "user_id",javaType = List.class,
many = @Many(select = "com.lagou.entity.dao.UserDao.findUsers"))
})
@Select("select * from `order`")
List<Order> findOrderAndUser2();
@Select("select * from user where id = #{id}")
List<User> findUsers(Integer id);
多对多查询
@Select("select * from user")
@Result({
@Result(property = "id",column = "id")
@Result(property = "name",column = "name")
@Result(property = "roleList",column = "id",javaType = List.class,
many = @Many(select = "com.lagou.entity.dao.RolerDao.findRoleByUid"))
})
List<User> findAllUserAndRole();
@Select("select * from role r,user_role ur where r.rid = ur.rid and ur.uid = #{uid}")
List<Role> findRoleByUid(Integer uid);