mybatis 多层对象引用查询

pokerWu 发布于 2015/05/14 21:17
阅读 2K+
收藏 0
<select id="getMessageById" parameterType="string" resultMap="message">
        select me.id,
        me.theme,
        me.content,
        me.time,
        ad.id as ad_id,
        ad.name as ad_name,
   		ro.id as ro_id,
   		ro.name as ro_name
        from message me 
        left outer join admin ad on me.adminID = ad.id
        left outer join role ro on ad.roleID = ro.id 
        where me.id = #{id} 
    </select>


<!-- Message resultMap -->
	 <resultMap type="Message" id="message">
	    <id column="id" property="id"/>
	    <result column="theme" property="theme"/>
	    <result column="content" property="content"/>
	    <result column="time" property="time"/>
	    <association property="author" resultMap="admin" columnPrefix="ad_"/>
	 </resultMap>
	 
	 <!-- admin resultMap -->
	 <resultMap type="AdminDto" id="admin">
	     <id column="id" property="id"/>
	     <result column="name" property="name"/>
	     <result column="last_login_time" property="last_login_time"/>
	     <result column="last_login_ip" property="last_login_ip"/>   
	     <association property="role" resultMap="adminRole" columnPrefix="ro_"/>
	 </resultMap>
	 <!-- role -->
	 <resultMap type="Role" id="adminRole">
	     <id column="id" property="id"/>
	     <result column="name" property="name"/>

</resultMap>


public class Message {
	/* 消息为缓存消息 */
	public static final int CACHE_MESSAGE = 1;
	/* 不是缓存消息 */
	public static final int NOT_CACHE_MESSAGE = 0;
	/* 消息已阅 */
	public static final int HAS_READED = 1;
	/* 消息未读 */
	public static final int NOT_READ = 0;
	/* 消息id */
	private String id;
	/*消息主题 (标题)*/
	private String theme;
	/* 消息正文  (内容)*/
	private String content;
	/* 消息发布者  */
	private AdminDto author;
	/* 消息发布时间  */
	private long time;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTheme() {
		return theme;
	}
	public void setTheme(String theme) {
		this.theme = theme;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	public AdminDto getAuthor() {
		return author;
	}
	public void setAuthor(AdminDto author) {
		this.author = author;
	}
}
public class AdminDto {
	private int id;
	private String name;
	/*上次登录时间*/
	private long last_login_time;
	/*上次登录ip*/
	private String last_login_ip;
	/*角色*/
	private Role role;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getLast_login_time() {
		return last_login_time;
	}
	public void setLast_login_time(long last_login_time) {
		this.last_login_time = last_login_time;
	}
	public String getLast_login_ip() {
		return last_login_ip;
	}
	public void setLast_login_ip(String last_login_ip) {
		this.last_login_ip = last_login_ip;
	}
}



当message返回后调用msg.getAuthor.getRole()为空,能得到Admin



加载中
返回顶部
顶部