如图所示 User实体类中关联着部门表
而在数据库中是一个字段 office_id
我们需要使用resultMap来进行映射
<resultMap id="userResult" type="User">
<id property="id" column="id" />
<result property="loginName" column="loginName" />
<result property="password" column="password" />
<result property="loginFlag" column="login_flag"/>
<result property="office.id" column="office.id" /> //同理插入与更新都可以直接使用.属性名
<result property="office.name" column="office.name" />
<result property="office.parentIds" column="office.parentIds" />
</resultMap>
查询语句举例 查询出一个用户的所有信息(包括office信息)
<select id="get" resultMap="userResult">
SELECT
a.id,a.login_name AS "loginName",
a.office_id AS "office.id", //起别名直接与ResultMap的属性名映射
o.name AS "office.name",
o.parent_id AS "office.parent.id"
FROM sys_user a
LEFT JOIN sys_office o ON o.id = a.office_id //连接查询
WHERE a.id = #{id}
</select>
<select id="findListByOffice" resultMap="userResult">
SELECT
<include refid="userColumns"/>
FROM sys_user a
<include refid="userJoins"/>
WHERE a.del_flag = #{DEL_FLAG_NORMAL}
<if test="office != null and office.id != null and office.id != ''">
AND o.id = #{office.id}
</if>
<if test="office == null">
AND (o.id = '' or o.id is null)
</if>
<if test="loginName != null and loginName != ''">
AND a.login_name like
//多数据源判断
<if test="_databaseId == 'postgre'">'%'||#{loginName}||'%'
<if test="_databaseId == 'oracle'">'%'||#{loginName}||'%'
<if test="_databaseId == 'mysql'">CONCAT('%', #{loginName}, '%')
<if test="_databaseId == 'mssql'">'%'+#{loginName}+'%'
</if>
<!-- 排序 -->
ORDER BY a.name
</select>
返回给前端必须为List对象的时候
可以自己创建一个不存在于数据库的实体类属性赋值后返回给前端
插入、更新时 正常使用String类型的一串id或信息即可
该情景常见于1张表里有个字段存放多个id
使用collection标签的嵌套查询
此方法可能要实体类中加入注解 @JsonIgnoreProperties(value = { “handler”})
<resultMap id="MonitorModelResultMap" type="com.jeeplus.modules.system.entity.MonitorModel">
<id property="id" column="id"/>
<result property="monitorFactorids" column="monitor_factor_ids"/>
<collection property="monitorFactors" select="findInFactor" column="monitor_factor_ids" ofType="MonitorFactor"> // 映射到这个属性时候 先执行select里的sql 参数为column 解决集放入这个属性
</collection>
</resultMap>
//嵌套查询如下 查询出因子信息 会在映射时自动执行这个sql 此需要写入dao接口中
<select id="findInFactor" parameterType="string" resultType="com.jeeplus.modules.system.entity.MonitorFactor">
select f.id,f.name,f.fcode from monitor_factor f
where find_in_set(f.id,#{ids})!=0
</select>
数据字段查询量很大+多个sql反复使用的时候
可以将select字段放入标签里
<sql id="userColumns">
a.id,
a.office_id AS "office.id",
a.login_name AS "loginName",
a.password,
a.name,
a.email,
a.phone,
a.mobile,
a.login_ip,
a.login_date,
a.remarks,
a.login_flag,
a.photo,
a.qrcode,
a.sign,
a.create_by AS "createBy.id",
a.create_date,
a.update_by AS "updateBy.id",
a.update_date,
a.del_flag,
o.name AS "office.name",
o.parent_id AS "office.parent.id",
o.parent_ids AS "office.parentIds",
oa.id AS "office.area.id",
oa.name AS "office.area.name",
oa.parent_id AS "office.area.parent.id",
oa.parent_ids AS "office.area.parentIds"
</sql>
<sql id="userJoins">
LEFT JOIN sys_office o ON o.id = a.office_id
LEFT JOIN sys_area oa ON oa.id = o.area_id
</sql>
使用 调用写好的
<select id="get" resultMap="userResult">
SELECT
<include refid="userColumns"/>,
FROM sys_user a
<include refid="userJoins"/>
WHERE a.id = #{id}
</select>
如题 记得传List然后遍历即可
<select id="siteForON" parameterType="map" resultType="com.jeeplus.modules.siteinfo.entity.SiteInfo">
select s.id, o.name from site_info s
left join sys_office o on o.id=s.office_id
where o.type='6' and
s.id not in
<foreach collection="strings" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</select>