mybatis使用註釋方式查詢出關聯的物件
阿新 • • 發佈:2019-01-24
mybatis使用註解進行關聯查詢時,需要使用@results手動配置結果對映
一對一關聯查詢:使用@one(一對多關聯查詢:使用@many)
- package com.zwk.dao;
- import com.zwk.domain.User;
- import com.zwk.domain.UserInfo;
- import org.apache.ibatis.annotations.*;
- import java.util.List;
- /**
- * Created by zwk on 16-11-22.
- */
-
publicinterface UserInfoDAO {
- //一個user對應一個userinfo
- @Select("select * from test_userinfo where id =#{id}")
- public UserInfo getUserInfoById(@Param("id") String id);
- //select ="com.zwk.dao.UserInfoDAO.getUserInfoById",getUserinfoById方法必須存在
- @Select("select * from test_user u where u.id = #{id}")
-
@Results({
- @Result(id = true,property = "id" ,column = "id"),
- @Result(property ="password",column = "password"),
- @Result(property ="userInfo",column="info_id"
- ,one =@One(select ="com.zwk.dao.UserInfoDAO.getUserInfoById"))}
- )
-
public User getById(@Param
- }