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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章