单表的Mybatis plus
平时单表Mybatis plus 挺简单的。最近遇到两个表的连接查询。想到用mybatis plus 不合适。项目又没有用pageHelper ,自己写SQL又要 自己写分页。
就琢磨下 mybatis plus 如何多表连接查询
@TableField 注解
官网描述
这个注解可以加在字段上 。
第一个解决是想要将查询的数据列 (放到当前对象里。)如何处理连接字段? xml 中描述
第二个问题 如何用到它的分页,不用管 生效了。
第三个问题,开干
DO 属性 加入不是当前表 的字段以及注解(偷懒不写 resultMap)
自己尝试封装 resulstMap 也是不影响的 -> 不想污染DO对象的话。(最好不要污染)
分页啥的不用自己考虑, 配置好了直接传递分页参数就行了,自然就增强了。 仔细想想 好像又没什么东东。
@TableField(exist = false)
private String xxxFile;
接口
IPage<XXDO> pageList(IPage<XXDO> page, @Param("query") XXQuery query);
xml
// 直接将结果放你的对象里去,注解处理了数据库不存在的字段
<select id="pageList" resultType="cn.hq.xxxDO">
select bed.*,bdd,xxx_file
from table1 bed
left join table2 bdd on
// 注意这里 随便连接,不影响
bed.xxx_no = bdd.xxx_no
<where>
<if test="query.xx != null and query.xxx != ''">
and bed.xxx LIKE concat('%',#{query.xxx,jdbcType=VARCHAR},'%')
</if>
<if test="query.xxxx != null and query.xxxx != ''">
and bed.xxxx = #{query.xxxx,jdbcType=VARCHAR}
</if>
<if test="query.xxxDateFrom != null">
and bdd.xxx_date >= #{query.declareDateFrom}
</if>
// 注意转义 < < > > >= > = ,老是忘记,记下
<if test="query.xxxDateTo != null">
and bdd.xxx_date <= #{query.declareDateTo}
</if>
</where>
</select>
没想到这样就解决了。 嘿嘿
如何集成mybatis plus 参考官网吧。
我叫huang__2