mysql+mybatis 分页

过去工作的几年一直使用的oracle,如今改成mysql,感觉有点不大习惯,在这里将昨天写的mysql+mybatis分页记录一下
user.xml

    <select id="getUserPageList" resultType="UserDO"
        parameterType="com.zhibi.data.api.jhs.param.UserSearchParam">
        select * from user
        <where>
            <if test="phoneNum!=null and phoneNum!=''"> phoneNum like CONCAT(#{phoneNum} ,'%') </if>
            <if test="nickName!=null and nickName!=''"> and nickName like CONCAT(#{nickName},'%') </if>
            <if test="startDate!=null"> and gmtCreate &gt; #{startDate}</if>
            <if test="endDate!=null"> and gmtCreate &lt; #{endDate}</if>
        </where>
        order by
        <if test="orderField!=null and orderField!=''"> ${orderField} ${order}, </if>
        gmtCreate desc limit #{pageNo},#{pageSize}
    </select>

    <select id="countPageList" resultType="int"
        parameterType="com.zhibi.data.api.jhs.param.UserSearchParam">
        select count(*) from (
        select * from user
        <where>
            <if test="phoneNum!=null and phoneNum!=''"> phoneNum like CONCAT(#{phoneNum} ,'%') </if>
            <if test="nickName!=null and nickName!=''"> and nickName like CONCAT(#{nickName},'%') </if>
            <if test="startDate!=null"> and gmtCreate &gt; #{startDate}</if>
            <if test="endDate!=null"> and gmtCreate &lt; #{endDate}</if>
        </where>
        )resultList
    </select>

遇到的一些问题:

  1. 模糊查询。在oracle中模糊查询只需要 #{phoneNum}||‘%’就可以了,但是放在mysql中不管用,必须concat拼接起来
  2. 子查询结果列表取别名,在求结果总条数的时候,需要对结果基求count,发现不加resultList别名会报错,oracle就可以不加
  3. 日期的加减必须通过date_add和date_sub函数完成,不能直接加减,如
  select DATE_sub(now() ,INTERVAL 1 DAY)from dual;--昨天
  select date_add(now(),interval 1 day); --明天
  1. 日期格式化 date_format,而不是to_char,且格式表达式不一样,mysql中:
    %S, %s 两位数字形式的秒( 00,01, …, 59)

    %i 两位数字形式的分( 00,01, …, 59)

    %H 两位数字形式的小时,24 小时(00,01, …, 23)

    %h, %I 两位数字形式的小时,12 小时(01,02, …, 12)

    %k 数字形式的小时,24 小时(0,1, …, 23)

    %l 数字形式的小时,12 小时(1, 2, …, 12)

    %T 24 小时的时间形式(h h : m m : s s)

    %r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)

    %p AM 或P M

    %W 一周中每一天的名称( S u n d a y, Monday, …, Saturday)

    %a 一周中每一天名称的缩写( Sun, Mon, …, Sat)

    %d 两位数字表示月中的天数( 00, 01, …, 31)

    %e 数字形式表示月中的天数( 1, 2, …, 31)

    %D 英文后缀表示月中的天数( 1st, 2nd, 3rd, …)

    %w 以数字形式表示周中的天数( 0 = S u n d a y, 1=Monday, …, 6=Saturday)

    %j 以三位数字表示年中的天数( 001, 002, …, 366)

    % U 周(0, 1, 52),其中Sunday 为周中的第一天

    %u 周(0, 1, 52),其中Monday 为周中的第一天

    %M 月名(J a n u a r y, February, …, December)

    %b 缩写的月名( J a n u a r y, February, …, December)

    %m 两位数字表示的月份( 01, 02, …, 12)

    %c 数字表示的月份( 1, 2, …, 12)

    %Y 四位数字表示的年份

    %y 两位数字表示的年份

    %% 直接值“%”
    如:

select DATE_FORMAT(NOW(),'%Y-%m-%d') from DUAL;
--结果   2016-07-14
  1. xml中大于号小于号有特殊意义需要转义

  2. 排序时因为是$取值,有安全隐患,可以在取值的时候做校验,如:

public String getOrderField() {
        if(orderField.indexOf(" ")>-1){
            return "";
        }

        return orderField;
    }

    public void setOrderField(String orderField) {
        this.orderField = orderField;
    }

    public String getOrder() {
        if(!"desc".equalsIgnoreCase(order)){
            return "ASC";
        }
        return "DESC";
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值