日常开发使用到的mybatis注解 @Results与@Result

  1. 首先说明一下@Results各个属性的含义,id为当前结果集声明唯一标识,value值为结果集映射关系,@Result代表一个字段的映射关系,column指定数据库字段的名称,property指定实体类属性的名称,jdbcType数据库字段类型,@Result里的id值为true表明主键,默认false;使用@ResultMap来引用映射结果集,其中value可省略。
  2. 贴一下代码
	@Select("select * from user where email=#{email}")
    @Results(id = "groupWithUsers",
            value = {
                    @Result(property = "userRole", javaType = List.class, many = @Many(select = "userRole"), column = "id"),
                    @Result(property = "userInFo", one = @One(select = "userInFos"), column = "id"),
                    @Result(property = "id", column = "id")
            })
    User userInFo(String email);

    @Select("select * from user_info where uId=#{id}")
    UserInFo userInFos(Integer id);
    
    @Select("select * from user_role where uId=#{id}")
    @Results(id = "UserInFo",
            value = {
                    @Result(property = "rolePermissions", javaType = List.class, many = @Many(select = "rolePermissions"), column = "rId"),
                    @Result(property = "Role", javaType = List.class, many = @Many(select = "Role"), column = "rId"),
                    @Result(property = "rId", column = "rId")
            })
    List<UserRole> userRole(Integer id);
  1. 在实体类里面做一下映射
	private UserInFo userInFo;
    private List<UserRole> userRole;
    private List<Role> rolesle;
    private List<RolePermission> olePermission;

你可能感兴趣的:(mybatis,java)