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.写数据库的查询语句
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 >= #{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>