百味皆苦 java后端开发攻城狮

mysql爬坑记录

2019-04-03
百味皆苦

24小时内

select * from deptdevicepart where department_no=? and device_no=? AND insert_date >= (NOW() - INTERVAL 24 HOUR)

三天前零点

SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL - 3 DAY),'%Y-%m-%d 00:00:00');

当天零点

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')

数据上下移

主要思路:一般涉及到排序功能,数据库都会新增一个sort为int型的字段,新增一条数据时,sort取当前表里sort最大值再加1,上移功能,主要是拿当前需要移动数据的id,找到上一条记录,交换sort,再更新数据库这两条数据;下移反之,具体实现如下:

controller层只需要一个接口实现上移下移功能,传入的对象新增一个boolean类型即可
  • 上移(impl实现):
ArsDept arsDept1 = arsDeptMapper.selectArsDeptById(arsDept.getDeptId());
//查询上一条记录
ArsDept arsDept2 = arsDeptMapper.moveUp(arsDept1.getOrderNum());
//最上面的不能上移
if(null==arsDept2){return 0;}
//交换position的值
int temp=arsDept1.getOrderNum();
arsDept1.setOrderNum(arsDept2.getOrderNum());
arsDept2.setOrderNum(temp);
//更新到数据库中
arsDeptMapper.updateArsDept(arsDept1);
arsDeptMapper.updateArsDept(arsDept2);
return 1;
<select id="moveUp" parameterType="java.lang.Integer" resultMap="ArsDeptResult">
    	<include refid="selectArsDeptVo"/>
        where ad.order_num &lt; #{orderNum} order by ad.order_num desc limit 0,1
</select>
  • 下移: impl实现
ArsDept arsDept1 = arsDeptMapper.selectArsDeptById(arsDept.getDeptId());
//查询下一条记录
ArsDept arsDept2 = arsDeptMapper.moveDown(arsDept1.getOrderNum());
//最下面的不能下移
if(null==arsDept2){return 0;}
//交换position的值
int temp=arsDept1.getOrderNum();
arsDept1.setOrderNum(arsDept2.getOrderNum());
arsDept2.setOrderNum(temp);
//更新到数据库中
arsDeptMapper.updateArsDept(arsDept1);
arsDeptMapper.updateArsDept(arsDept2);
return 1;
<select id="moveDown" parameterType="java.lang.Integer" resultMap="ArsDeptResult">
    	<include refid="selectArsDeptVo"/>
        where ad.order_num &gt; #{orderNum} order by ad.order_num asc limit 0,1
</select>

下一篇 mysql训练题

Comments

Content