前后台交互搭建的完善和定时任务的配置

1.UserServiceImpl.java  数据的新增操作

    @Override
    public void create(User user) {
        if(user.getId()!=null && user.getId()!=0){
            //修改
            userDao.update(user);
        }else{
            //新增
        userDao.create(user);
        }
        
    }

2.定时任务的配置

1)applicationContext.xml的配置 task

xmlns:task="http://www.springframework.org/schema/task"

 http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-4.3.xsd">

<!-- 放开定时任务配置 -->
    <task:annotation-driven></task:annotation-driven>

2)新建包job Task类

package com.zq.job;

import java.util.Date;

import org.apache.tools.ant.util.DateUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务类
 * @author Administrator
 *
 */
@Component
public class Taskjob {

    @Scheduled(cron="0/10 * * * * ?")
    public void userGrade(){
        System.out.print("定时器执行时间:"+DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }
}

3.User加入新字段

private Integer grade;//级别
    private Double total;//消费总金额
    private Double account;//账户余额

public Integer getGrade() {
        return grade;
    }
    public void setGrade(Integer grade) {
        this.grade = grade;
    }
    public Double getTotal() {
        return total;
    }
    public void setTotal(Double total) {
        this.total = total;
    }
    public Double getAccount() {
        return account;
    }
    public void setAccount(Double account) {
        this.account = account;
    }

4.Taskjob

public class Taskjob {
    @Autowired
    UserService userService;

    @Scheduled(cron="0/10 * * * * ?")
    public void userGrade(){
        System.out.print("定时器执行时间:"+DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    userService.updateGrade();
    }
}

5.UserServiceImpl

@Override
    public void updateGrade(){
        //会员等级
        List<Map<String,Object>> gradeList=new ArrayList<Map<String,Object>>();
        Map<String,Object> map= new HashMap<String,Object>();
        map.put("grade", "1");
        map.put("total", "0");
        gradeList.add(map);
        
     map= new HashMap<String,Object>();
        map.put("grade", "2");
        map.put("total", "5000");
        gradeList.add(map);
        
    map= new HashMap<String,Object>();
        map.put("grade", "3");
        map.put("total", "20000");
        gradeList.add(map);
        
        List<User> users = userDao.findByTotal(gradeList);
        if(users!=null && users.size()>0){
            userDao.updateGrade(users);
        }

6.写数据库的查询语句

e56717c69c04d41cc3f3c76d68241434b19.jpg

7.查询语句写入代码中 UserDao.xml mybatis写法

<select id="findByTotal" parameterType="list" resultType="user">
    select * from user 
    <where>
      <foreach item="item" index="index" collection="list" open="("separator=")
      or ("close=")">
      grade=#{item.grade} - 1 and total  &gt;= #{item.total}<!--大于和小于号要用转义符  -->
      </foreach>
    </where>
</select>

 

<update id="updateGrade" parameterType="List">
    update user set grade= grade + 1 where id in 
    <where>
      <foreach item="item" index="index" collection="list" open="("separator=", "close=")">
      #{item.id}
      </foreach>
    </where>
    </update>

 

 

转载于:https://my.oschina.net/u/4092026/blog/3034219

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值