Mybatis注解开发
Mybatis之常用注解
Mybatis可以使用常用的注解开发,可以方便减少编写Mapper.xml映射文件。
- @Insert:新增
- @Update:更新
- @Delete:删除
- @Select:查询
- @Result:结果集封装
- @Results:可以与@Result一起使用,封装多个结果集
- @One:一对一结果集封装
- @Many:一对多结果集封装
Mybatis的注解实现复杂映射开发
实现复杂关系映射之前我们可以在文件中通过配置来实现,使用注解开发后,可以使用@Results注解,@Result、@One、@Many注解来组合完成复杂关系映射。
一对一注解查询
用户表和订单表的关系为,一个用户有多个订单,一个订单只属于一个用户
一对一查询的需求:查询一个订单,同时查询出该订单所属的用户
sql语句:
select * from orders;
select * from user where id = 查询出订单的uid;
创建user和order实体类
public class Order {
private int id;
private Date ordertime;
private double total;
// 代表当前订单属于哪一个用户
private User user;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
}
使用注解的Mapper
public interface OrderMapper {
@Select("select * from orders")
@Results({
@Result(id=true,property = "id",column = "id"),
@Result(property = "ordertime",column = "ordertime"),
@Result(property = "total",column = "total"),
@Result(property = "user",column = "uid",
javaType = User.class,
one = @One(select = "com.demo.mapper.UserMapper.findById"))
})
List<Order> findAll();
}
public interface UserMapper {
@Select("select * from user where id=#{id}")
User findById(int id);
}
一对多注解查询
模型例子:用户表和订单表关系为:一个用户有多个订单,一个订单只从属于一个用户
一对多查询的需求:查询所有用户,以及每个用户的订单
sql语句:
select * from user;
select * from orders where uid = 查询出用户的id;
修改user实体类
public class Order {
private int id;
private Date ordertime;
private double total;
// 代表当前订单属于哪个用户
private User user;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
// 代表当前用户具有哪些订单
private List<Order> orderList;
}
注解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 = "orderList",column = "id",
javaType = List.class,
many = @Many(select ="com.demo.mapper.OrderMapper.findByUid"))
})
List<User> findAllUserAndOrder();
}
public interface OrderMapper {
@Select("select * from orders where uid=#{uid}")
List<Order> findByUid(int uid);
}
多对多注解查询
模型例子:用户表和角色表关系为,一个用户有多个角色,一个角色被多个用户拥有
多对多查询需求:查询用户同时查询出该用户所有角色
对应sql语句:
select * from user;
select * from role r , user_role ur where r.id = ur.role_id and ur.user_id = 用户id
创建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;
}
使用注解配置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.demo.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);
}