Mybatis 使用注解实现复杂的关系映射

使用注解实现复杂的关系映射

@Results  注解
	代替的是标签<resultMap>
	该注解中可以使用单个@Result 注解,也可以使用@Result 集合
	@Results({@Result(),@Result()})或@Results(@Result())
	
@Resut 注解
	代替了 <id> 标签和<result> 标签
	
@Result  中  属性介绍:
	id 是否是主键字段
	column 数据库的列名
	property 需要装配的属性名
	one 需要使用的@One 注解(@Result(one=@One)()))
	many 需要使用的@Many 注解(@Result(many=@many)()))
	
@One  注解(一对一)
	代替了<assocation> 标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
	
@One  注解属性介绍:
	select 指定用的 来多表查询的 sqlmapper
	fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。。
	使用格式:
		@Result(column=" ",property="",one=@One(select=""))
		
@Many  注解(多对一)
	代替了<Collection> 标签, 是是多表查询的关键,在注解中用来指定子查询返回对象集合。
	注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType
	(一般为 ArrayList)但是注解中可以不定义;
	使用格式:
		@Result(property="",column="",many=@Many(select=""))

一对一

  • Account 类中添加 user 属性
UserDao
public interface UserDao {
    @Select("select * from user")
    @Results(id="userMap",
            value ={@Result(id=true,column = "id",property = "id"),
                    @Result(column = "username",property = "username"),
                    @Result(column = "birthday",property = "birthday"),
                    @Result(column = "address" ,property = "address"),
                    @Result(column = "sex",property = "sex")
            } )
    public List<User> findAll();

    @Select("select * from user where id=#{uid}")
    @ResultMap("userMap")
    public User findById(int id);
}
AccountDao
public interface AccountDao {
    @Results(id = "accountMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(column = "uid",property = "user",
                    one = @One(select = "com.itheima.dao.UserDao.findById",
                                        fetchType = FetchType.LAZY
                    ))})
    @Select("select * from account")
    public List<Account> findAll();
}


一对多

  • User类中添加 Account属性
UserDao
public interface UserDao {
    @Select("select * from user")
    @Results(id="userMap",
            value ={@Result(id=true,column = "id",property = "id"),
                    @Result(column = "username",property = "username"),
                    @Result(column = "birthday",property = "birthday"),
                    @Result(column = "address" ,property = "address"),
                    @Result(column = "sex",property = "sex"),
                    @Result(column = "id",property = "accounts",many = @Many(
                            select = "com.itheima.dao.AccountDao.findById",
                            fetchType = FetchType.LAZY
                    ))
            } )
    public List<User> findAll();

    @Select("select * from user where id=#{uid}")
    @ResultMap("userMap")
    public User findById(int id);
}

AccountDao
public interface AccountDao {
    @Results(id = "accountMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
           })
    @Select("select * from account")
    public List<Account> findAll();

    @Select("select *from account where uid = #{uid}")
    public Account findById(int id);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值