刚来到一家新公司, 翻看项目代码, 发现一位同事写的查询逻辑很好, 不用插件, 一个语句完成了分页条件查询.

而我之前一般都是在业务层对参数进行判断, 如果有条件,就调用条件查询的方法, 如果没有条件, 就调用查询所有的方法, 代码冗余较多

贴下代码:

1, 首先定义resultMap:

  

<resultMap id="xxxModel" type="com.aaa.XxxModel">
<id column="id" javaType="java.lang.Long" jdbcType="BIGINT" property="id" />
<result column="ip" javaType="java.lang.String" jdbcType="VARCHAR" property="ip" />
<result column="port" javaType="java.lang.Integer" jdbcType="INTEGER" property="port" />
<result column="userName" javaType="java.lang.String" jdbcType="VARCHAR" property="userName" />
<result column="password" javaType="java.lang.String" jdbcType="VARCHAR" property="password" />
<result column="lineNum" javaType="java.lang.Integer" jdbcType="INTEGER" property="lineNum" />
<result column="isInternation" javaType="java.lang.Integer" jdbcType="INTEGER" property="isInternation" />
<result column="createDate" javaType="java.lang.String" jdbcType="VARCHAR" property="createDate" />
<result column="updateDate" javaType="java.lang.String" jdbcType="VARCHAR" property="updateDate" />
</resultMap> 2, 定义sql片段, 方便阅读:
//条件和分页参数的封装, 利用动态sql, 特别是模糊查询的%拼接, 很赞:
<sql id="pageListCount">
from anti_http_proxy a
<if test="groupId != null and groupId !=''">
LEFT JOIN anti_proxy_group b on a.`host` = b.`host`
LEFT JOIN anti_group_server c on b.group_id = c.group_id
</if>
where 1=1
<if test="ip != null and ip !=''">
and a.`host` like CONCAT('%','${ip}','%')
</if>
<if test="groupId != null and groupId !=''">
and c.id = #{groupId}
</if>
ORDER BY a.gmt_modified desc
<if test="(pageNumber != null and pageNumber != '' or pageNumber == 0) and pageSize != null and pageSize != ''">
limit #{pageNumber}, #{pageSize}
</if>
</sql> <sql id="resultCol">
a.id
,a.`host` as ip
,a.`port`
,a.username as userName
,a.`password`
,a.band as lineNum
,a.international as isInternation
,DATE_FORMAT(a.gmt_create,'%Y-%m-%d %H:%i:%s') as createDate
,DATE_FORMAT(a.gmt_modified,'%Y-%m-%d %H:%i:%s') as updateDate
</sql>
3, 完成查询方法
<select id="selectAllByPage" resultMap="xxxModel">
select
<include refid="resultCol" />
<include refid="pageListCount" />
</select>

最新文章

  1. bzoj-4517 4517: [Sdoi2016]排列计数(组合数学)
  2. awk中可以使用system来执行复杂的shell命令
  3. js验证手机号
  4. css3 使用SVG做0.5px 的边框细线
  5. static成员函数
  6. 19.python笔记之Rabbitmq
  7. spring 两个 properties
  8. HDU1227:Fast Food
  9. 20款最优秀的JavaScript编辑器
  10. java学习多线程之死锁
  11. Ubuntu 14.04根据系统,休眠后不能启动要解决的问题
  12. 强烈推荐visual c++ 2012入门经典适合初学者入门
  13. nginx 配置访问限制
  14. Catch him
  15. Java中的泛型类和泛型方法区别和联系
  16. gvim 技巧
  17. matlab中输入x. 与x的区别
  18. Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学
  19. Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
  20. Flex+blazeds实现与mySQL数据库的连接(已成功实现此文的例子)

热门文章

  1. ffmpeg参数说明
  2. grid编辑后时间格式不对问题
  3. node项目的基本构建流程或者打开一个node项目的流程
  4. rest api get 查询接口 多个参数
  5. 30岁程序员的选择,一线OR二线?
  6. java架构师负载均衡、高并发、nginx优化、tomcat集群、异步性能优化、Dubbo分布式、Redis持久化、ActiveMQ中间件、Netty互联网、spring大型分布式项目实战视频教程百度网盘
  7. OpenStack(企业私有云)万里长征第三步——调试成功
  8. [leetcode-575-Distribute Candies]
  9. GNU的makefile文件编写说明
  10. Spring Security Filter详解