Mybatis及聯查尋對象自動封裝(相似hibernate的級聯)java
注:在級聯的時候,可能會出現數據庫表中的個別列(column)名稱相同,在這裏咱們須要將列名取個[AS]惟一的名稱,在select 原來數據庫表中名稱 as 自定義名稱。下面舉個例子數據庫
<resultMap id="BaseResultMap" type="com.zxxk.xybb.model.FdZoneShare"> <!-- WARNING - @mbggenerated --> <id column="id" jdbcType="INTEGER" property="id" /> <result column="fd_user_id" jdbcType="INTEGER" property="fdUserId" /> <result column="fd_user_name" jdbcType="VARCHAR" property="fdUserName" /> <result column="content" jdbcType="VARCHAR" property="content" /> <result column="image_url" jdbcType="VARCHAR" property="imageUrl" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="school_id" jdbcType="INTEGER" property="schoolId" /> <result column="class_id" jdbcType="INTEGER" property="classId" /> <result column="is_share" jdbcType="BIT" property="isShare" /> <result column="is_del" jdbcType="BIT" property="isDel" /> <result column="comment_count" jdbcType="INTEGER" property="commentCount" /> <result column="praise_count" jdbcType="INTEGER" property="praiseCount" /> <result column="student_id" jdbcType="INTEGER" property="studentId" /> <result column="share_time" jdbcType="TIMESTAMP" property="shareTime" /> </resultMap> <resultMap id="ResultMap" type="com.zxxk.xybb.model.FdZoneShare" extends="BaseResultMap"> <association property="fdUser" javaType="com.zxxk.xybb.model.FdUser"> <id column="uid" property="id"/> <result column="avatar" property="avatar"/> </association> <collection property="fdZoneComments" ofType="com.zxxk.xybb.model.FdZoneComment" javaType="list"> <id column="commentId" property="id" /> <result column="c_zone_share_id" property="zoneShareId"/> <result column="c_fd_user_id" property="fdUserId"/> <result column="c_fd_user_name" property="fdUserName"/> <result column="c_content" property="content"/> <result column="c_create_time" property="createTime"/> </collection> <collection property="fdZonePraises" ofType="com.zxxk.xybb.model.FdZonePraise" javaType="list"> <id column="praiseId" property="id"/> <result column="p_zone_share_id" property="zoneShareId"/> <result column="p_fd_user_id" property="fdUserId"/> <result column="p_fd_user_name" property="fdUserName"/> <result column="p_create_time" property="createTime"/> </collection> </resultMap> <!-- 查詢媽媽圈主題 相同名字在column上進行新型修改 --> <select id="findZoneShares" parameterType="map" resultMap="ResultMap"> SELECT zs.*, zc.id as commentId,zc.zone_share_id as c_zone_share_id,zc.fd_user_id as c_fd_user_id,zc.fd_user_name as c_fd_user_name,zc.content as c_content,zc.create_time as c_create_time, zp.id as praiseId,zp.zone_share_id as p_zone_share_id,zp.fd_user_id as p_fd_user_id,zp.fd_user_name as p_fd_user_name,zp.create_time as p_create_time, u.avatar FROM fd_zone_share zs LEFT JOIN fd_user u ON u.id=zs.fd_user_id LEFT JOIN fd_zone_comment zc ON zs.id=zc.zone_share_id LEFT JOIN fd_zone_praise zp ON zs.id=zp.zone_share_id WHERE zs.is_share = 1 <if test="classId != null and classId != ''"> AND zs.class_id=#{classId} </if> <if test="share!=0"> <choose> <when test="moreThan != null and moreThan != ''"> AND zs.id <![CDATA[>]]> #{id} </when> <otherwise> AND zs.id <![CDATA[<]]> #{id} </otherwise> </choose> </if> ORDER BY zs.share_time DESC </select>
一、多對一app
(1)在某個類中關聯某個對象dom
<resultMap id="BaseResultMap" type="com.yujin.domain.User"> <id property="id" column="id" javaType="long" jdbcType="BIGINT"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="username" column="username" javaType="string" jdbcType="VARCHAR"/> <result property="password" column="password" javaType="string" jdbcType="VARCHAR"/> <result property="roleId" column="role_id" javaType="long" jdbcType="BIGINT"/> <association property="role" javaType="com.yujin.domain.Role"> <!-- column能夠自定義 ,建議使用對應表中的名稱--> <id column="id" property="id" /> <result column="name" property="name" /> <result column="description" property="description" /> </association> </resultMap> <!-- 查詢全部 --> <select id="selectAll" resultMap="BaseResultMap"> SELECT user.id,user.name,user.username,user.password,user.role_id,role.id as roleId,role.name,role.description FROM t_users user LEFT JOIN t_roles role ON user.role_id = role.id </select>
Daofetch
package com.yujin.dao; import java.util.List; import com.yujin.domain.User; public interface UserDao { List<User> selectAll(); }
客戶端調用ui
public void selectUser() throws Exception{ List<User> userList = userService.findUsers(); for(User user : userList){ System.err.println(user.getName()); System.err.println(user.getRole().getDescription()); } }
分析Mapper中<resultMap>的各類形式url
形式一:spa
<resultMap id="BaseResultMap" type="com.yujin.domain.User"> <id property="id" column="id" javaType="long" jdbcType="BIGINT"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="username" column="username" javaType="string" jdbcType="VARCHAR"/> <result property="password" column="password" javaType="string" jdbcType="VARCHAR"/> <result property="roleId" column="role_id" javaType="long" jdbcType="BIGINT"/> <association property="role" javaType="com.yujin.domain.Role"> <!-- column能夠自定義 ,建議使用對應表中的名稱--> <id column="id" property="id" /> <result column="name" property="name" /> <result column="description" property="description" /> </association> </resultMap>
形式二:hibernate
<resultMap id="BaseResultMap" type="com.yujin.domain.User"> <id property="id" column="id" javaType="long" jdbcType="BIGINT"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="username" column="username" javaType="string" jdbcType="VARCHAR"/> <result property="password" column="password" javaType="string" jdbcType="VARCHAR"/> <result property="roleId" column="role_id" javaType="long" jdbcType="BIGINT"/> <association property="role" resultMap="roleMap" fetchType="lazy"/> </resultMap> <!-- 對應的實體類 --> <resultMap id="roleMap" type="com.yujin.domain.Role"> <!-- column能夠自定義 ,建議使用對應表中的名稱--> <id column="id" property="id" /> <result column="name" property="name" /> <result column="description" property="description" /> </resultMap>
形式三:code
<resultMap id="BaseResultMap" type="com.yujin.domain.User"> <id property="id" column="id" javaType="long" jdbcType="BIGINT"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="username" column="username" javaType="string" jdbcType="VARCHAR"/> <result property="password" column="password" javaType="string" jdbcType="VARCHAR"/> <result property="roleId" column="role_id" javaType="long" jdbcType="BIGINT"/> </resultMap> <!—-專門寫關聯對象 --> <resultMap type="com.yujin.domain.User" id="ResultMap" extends="BaseResultMap"> <association property="role" javaType="com.yujin.domain.Role"> <!-- column能夠自定義 ,建議使用對應表中的名稱--> <id column="id" property="id" /> <result column="name" property="name" /> <result column="description" property="description" /> </association> </resultMap> <!-- 查詢全部 --> <select id="selectAll" resultMap="ResultMap"> SELECT user.id,user.name,user.username,user.password,user.role_id,role.id as roleId,role.name,role.description FROM t_users user LEFT JOIN t_roles role ON user.role_id = role.id </select>
形式四:(推薦使用)
<!-- association的用法 --> <resultMap id="BaseResultMap" type="com.yujin.domain.User"> <id property="id" column="id" javaType="long" jdbcType="BIGINT"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="username" column="username" javaType="string" jdbcType="VARCHAR"/> <result property="password" column="password" javaType="string" jdbcType="VARCHAR"/> <result property="roleId" column="role_id" javaType="long" jdbcType="BIGINT"/> <!-- column:user表中的對應的role_id select:對應實體類中 namespace+id --> <association property="role" column="role_id" select="com.yujin.dao.RoleDao.getById"/> </resultMap>
二、一對多
形式一:
<resultMap type="com.yujin.domain.Role" id="BaseResultMap"> <id column="id" jdbcType="BIGINT" property="id" javaType="long"/> <result column="name" jdbcType="VARCHAR" property="name" javaType="string"/> <result column="description" jdbcType="VARCHAR" property="description" javaType="string"/> <!-- ofType:集合中泛型的類型類型的全路徑 --> <collection property="users" ofType="com.yujin.domain.User"> <!-- column:和查詢語句as後面的名稱,最好不要和上面實體類重名 --> <id column="userId" property="id"/> <result column="uname" property="name"/> <result column="username" property="username"/> <result column="password" property="password"/> </collection> </resultMap> <!-- 查詢全部 --> <select id="selectAll" resultMap="BaseResultMap"> SELECT role.id,role.name,role.description,user.id as userId,user.name as uname,user.username,user.password FROM t_roles role RIGHT JOIN t_users user ON role.id = user.role_id </select>
形式二
<resultMap type="com.yujin.domain.Role" id="BaseResultMap"> <id column="id" jdbcType="BIGINT" property="id" javaType="long"/> <result column="name" jdbcType="VARCHAR" property="name" javaType="string"/> <result column="description" jdbcType="VARCHAR" property="description" javaType="string"/> </resultMap> <resultMap type="com.yujin.domain.Role" id="ResultMap" extends="BaseResultMap"> <!-- ofType:集合中泛型的類型類型的全路徑 --> <collection property="users" ofType="com.yujin.domain.User"> <!-- column:和查詢語句as後面的名稱,最好不要和上面實體類重名 --> <id column="userId" property="id"/> <result column="uname" property="name"/> <result column="username" property="username"/> <result column="password" property="password"/> </collection> </resultMap>
(3)形式三
<resultMap type="com.yujin.domain.Role" id="BaseResultMap"> <id column="id" jdbcType="BIGINT" property="id" javaType="long"/> <result column="name" jdbcType="VARCHAR" property="name" javaType="string"/> <result column="description" jdbcType="VARCHAR" property="description" javaType="string"/> <collection property="users" resultMap="ResultMap"/> </resultMap> <resultMap type="com.yujin.domain.Role" id="ResultMap"> <!-- ofType:集合中泛型的類型類型的全路徑 --> <collection property="users" ofType="com.yujin.domain.User"> <!-- column:和查詢語句as後面的名稱,最好不要和上面實體類重名 --> <id column="userId" property="id"/> <result column="uname" property="name"/> <result column="username" property="username"/> <result column="password" property="password"/> </collection> </resultMap>
(4)形式四
RoleMapper.xml <resultMap type="com.yujin.domain.Role" id="BaseResultMap"> <id column="id" jdbcType="BIGINT" property="id" javaType="long"/> <result column="name" jdbcType="VARCHAR" property="name" javaType="string"/> <result column="description" jdbcType="VARCHAR" property="description" javaType="string"/> <collection property="users" column="id" ofType="com.yujin.domain.User" select="com.yujin.dao.UserDao.getUserByRoleId"/> </resultMap> <!-- 查詢全部 --> <select id="selectAll" resultMap="BaseResultMap"> SELECT role.id,role.name,role.description FROM t_roles role </select> UserMapper.xml <select id="getUserByRoleId" resultMap="BaseResultMap"> SELECT user.id,user.name,user.username,user.password,user.role_id,role.id as roleId,role.name,role.description FROM t_users user LEFT JOIN t_roles role ON user.role_id = role.id WHERE user.role_id = #{id} </select>