软件编程
位置:首页>> 软件编程>> java编程>> MybatisPlus分页排序查询字段带有下划线的坑及解决

MybatisPlus分页排序查询字段带有下划线的坑及解决

作者:__Nexo  发布时间:2022-08-16 22:26:28 

标签:MybatisPlus,分页,排序查询,字段,下划线

MybatisPlus分页排序查询字段带有下划线

如果使用MybatisPlus的自动转驼峰命名法,分页排序查询的字段带有下划线时,会出问题。


page = new Page<>(pageNo, pageSize);
OrderItem orderItem = new OrderItem();
orderItem.setColumn("create_date");
orderItem.setAsc(isAsc);
page.addOrder(orderItem);
return page;

如果这样封装分页对象,字段是create_date的话,在最终执行时,order by的字段会自动变成createDate。

但是数据库是下划线的,就导致找不到字段,如果改成createDate,他还是按createDate去排序,无法解决。不知道大家遇到过这个坑没有。

最终我是改数据库字段为createdate了

MybatisPlus字段名称有下划线查询为null

在Mybatis-plus中自己写sql查询时,采用 resultType 做字段映射,带下划线的字段值查询不到。


<select id="pageW_XKXMSJLXRSearch"
        resultType="cjw.nic.niceasy.szyzgts.module.wy_ysgl.wy_01.wy_01_03.entity.W_XKXMSJLXR">
 SELECT temp.*
 FROM (
 SELECT t.*
 from w_xkxmsjlxr t
 INNER JOIN w_xkxmjbxx wx ON t.SSXKXMJBXX_ID = wx.ID AND wx.SCBJ = 0 and t.scbj = 0
 INNER JOIN s_xzqh sx ON sx.QHDM = #{qhdm} and wx.XZQHDM like CONCAT('%',sx.XJQHDMBDS,'%')
 ) as temp
 ${ew.customSqlSegment}
</select>

原因分析

Mybatis-plus默认开启驼峰转换,ssxkxmjbxx_id 字段会被转换为 ssxkxmjbxxId ,自然查询不到。

解决方案

(1)关闭 Mybatis-plus 驼峰转换,这样虽然能解决问题,但是会影响整个项目,得不偿失。

(2)采用 resultMap 做映射,自己写映射关系。


<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="cjw.nic.niceasy.szyzgts.module.wy_ysgl.wy_01.wy_01_03.entity.W_XKXMSJLXR">
 <id column="ID" property="id"/>
 <result column="SSXTDM" property="ssxtdm"/>
 <result column="SSGCDM" property="ssgcdm"/>
 <result column="SSXKXMJBXX_ID" property="ssxkxmjbxx_id"/>
 <result column="SSGLDW_ID" property="ssgldw_id"/>
 <result column="GLDWLX" property="gldwlx"/>
 <result column="LXRJSDM" property="lxrjsdm"/>
 <result column="XM" property="xm"/>
 <result column="SZBM" property="szbm"/>
 <result column="ZW" property="zw"/>
 <result column="SJH" property="sjh"/>
 <result column="BGDH" property="bgdh"/>
 <result column="CZ" property="cz"/>
 <result column="YX" property="yx"/>
 <result column="LXDZ" property="lxdz"/>
 <result column="QQ" property="qq"/>
 <result column="BZ" property="bz"/>
 <result column="SCBJ" property="scbj"/>
 <result column="SCJL_ID" property="scjl_id"/>
 <result column="CJR_ID" property="cjr_id"/>
 <result column="CJSJ" property="cjsj"/>
 <result column="ZHXGR_ID" property="zhxgr_id"/>
 <result column="ZHXGSJ" property="zhxgsj"/>
</resultMap>
<select id="pageW_XKXMSJLXRSearch"
        resultMap="BaseResultMap">
 SELECT temp.*
 FROM (
 SELECT t.*
 from w_xkxmsjlxr t
 INNER JOIN w_xkxmjbxx wx ON t.SSXKXMJBXX_ID = wx.ID AND wx.SCBJ = 0 and t.SCBJ = 0
 INNER JOIN s_xzqh sx ON sx.QHDM = #{qhdm} and wx.XZQHDM like CONCAT('%',sx.XJQHDMBDS,'%')
 ) as temp
 ${ew.customSqlSegment}
</select>

ps: 采用 Mybaits-plus 代码生成器,免去写繁琐映射的烦恼!

来源:https://blog.csdn.net/bhy5683/article/details/107608394

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com