乐观锁

INSERT INTO tb_wallet VALUES (null,#{userId},#{money},#{ver},NOW(),NOW())

UPDATE tb_wallet SET money=#{money},update_time=NOW(),ver=ver+1 WHERE user_id=#{userId} AND ver=#{ver}



package com.bway.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.bway.cache.RedisPool;
import com.bway.cache.RedisUtil;
import com.bway.dao.IRecordMapper;
import com.bway.pojo.Record;
import com.bway.pojo.Record2;
import com.bway.service.IRecordService;
import com.bway.utils.SsmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class RecordServiceImpl implements IRecordService{
@Autowired
    private IRecordMapper mapper;
    @Override
    public List selectRecordByUId(int uid) {
        if (exit(uid, SsmConstants.CACHE_USER_RECORD_LIST)){//先判断redis缓存内是否存在要查的集合
            System.out.println("使用缓存机制");
            return getCacheRecordList(uid,SsmConstants.CACHE_USER_RECORD_LIST);//存在就直接从缓存取值,不需要再查询数据库,增加速度
        }
        List recordList=mapper.selectRecordByUid(uid);
        System.out.println("没有使用缓存机制");
        return mapper.selectRecordByUid(uid);
    }
    @Override
    public void insertRecord(Record record) {
    mapper.insertRecord(record);
    }
    @Override
    public int reid(int id) {
        System.out.print("zzz"+id);
        return mapper.reid(id);
    }
    @Override
    public Record byid(int uid) {
        return mapper.byid(uid);
    }


    private boolean exit(int id,String key){//判断键是否存在
        RedisPool redisPool= RedisUtil.getRedisPool("pool1");
        return redisPool.exit(String.format(key,id));
    }
    private void cacheRecordList(int id,String key,List list){//将集合转为string存入缓存
        RedisPool redisPool= RedisUtil.getRedisPool("pool1");
        String recordList= JSONArray.toJSONString(list);
        redisPool.set(String.format(key,id),recordList);
    }
    private List getCacheRecordList(int id,String key){//从缓存取出字符串并转为集合类型
        RedisPool redisPool=RedisUtil.getRedisPool("pool1");
        String list=redisPool.get(String.format(key,id));
        List recordList= JSONArray.parseArray(list,Record.class);
        return recordList;
    }
}







//祝你好运,加油
package com.bawei.start;
import com.bawei.util.ContextLoader;
import com.bawei.util.PropertiesUtil;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
import java.util.Properties;
public class JobMain {
    public static void main(String[] args) throws IOException{
       AnnotationConfigApplicationContext con=new AnnotationConfigApplicationContext(ContextLoader.class);
        Properties properties = PropertiesUtil.getProperties("properties/log4j.properties","utf-8");
        if(properties!=null){
            PropertyConfigurator.configure(properties);
        }
 new AnnotationConfigApplicationContext(JobMain.class);

    }
}


package com.bawei.task;
import com.bawei.service.INewsService;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.logging.Logger;
@Service("addJob")
@EnableScheduling
@PropertySource("classpath:properties/jobConfig.properties")
public class AddJob {
    private Logger log=Logger.getLogger("log");
    @Resource
    private INewsService newsService;
    @Scheduled(cron = "${addJob}")
    public void run(){
        long time =System.currentTimeMillis();
        log.info("开始添加数据");
        log.info(String.valueOf("添加成功,添加的行数为:"));
        log.info("插入结束,耗时:"+(System.currentTimeMillis()-time));


    }
}

#查询
findJob=0/10 * * * * ?


#添加
addJob=0/10 * * * * ?



#删除
#deleteJob=0/20 * * * * ?




你可能感兴趣的:(乐观锁)