springboot 整合tx-mybaits 实现crud操作

一 操作案例

1.1 工程结构

springboot 整合tx-mybaits 实现crud操作_第1张图片

1.2 pom文件的配置

 
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.3.2
    
    
    
      mysql
      mysql-connector-java
      runtime
    
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
    
    
      org.apache.commons
      commons-lang3
      3.4
    
    
      com.fasterxml.jackson.core
      jackson-core
    
    
      com.fasterxml.jackson.core
      jackson-databind
    
    
      com.fasterxml.jackson.datatype
      jackson-datatype-joda
      2.9.6
    
    
      com.fasterxml.jackson.module
      jackson-module-parameter-names
    
    
    
      com.github.pagehelper
      pagehelper-spring-boot-starter
      1.4.1
    
    
    
      com.alibaba
      druid-spring-boot-starter
      1.1.9
    

    
    
      org.springframework.boot
      spring-boot-starter-data-redis
    
    
      org.apache.commons
      commons-pool2
    

    
    
      redis.clients
      jedis
      4.3.1
    

    
      org.projectlombok
      lombok
      ${lombok.version}
      true
    

    
    
    
    
      io.springfox
      springfox-swagger2
      2.9.2
    
    
      io.springfox
      springfox-swagger-ui
      2.9.2
    
    
    
      org.redisson
      redisson
      3.11.2
    

    
    
      com.google.guava
      guava
      23.0
    
    
    
      javax.persistence
      persistence-api
      1.0.2
    
    
    
      tk.mybatis
      mapper
      4.1.5
    

主要依赖

springboot 整合tx-mybaits 实现crud操作_第2张图片

 1.3 dao层

@org.apache.ibatis.annotations.Mapper
public interface CustomerDao  extends Mapper {
}

 1.4 service层

1.接口

public interface CustormerService {
    public void addCustomerData(Customer customer);
}

2.实现类

@Service
public class CustomerServiceImpl  implements CustormerService {
    public static final String CACHA_KEY_CUSTOMER = "customer:";
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private RedisTemplate redisTemplate;
    public void addCustomerData(Customer customer){
        //1.先插入mysql中
       int k= customerDao.insertSelective(customer);
       if(k>0){
           //2.从mysql中查询一次
           Customer result=customerDao.selectByPrimaryKey(customer.getId());
           //3.新增到redis中
           String key=CACHA_KEY_CUSTOMER+customer.getId();
           redisTemplate.opsForValue().set(key,result);
       }
    }
}

1.5 controller层

@RestController
@Slf4j
public class CustomerController {
    @Resource
    private CustormerService customerSerivce;

    @ApiOperation("数据库初始化2条Customer记录插入")
    @RequestMapping(value = "/customer/add",method = RequestMethod.GET)
    public void addCustomer()
    {
        for (int i = 1; i < 3; i++) {
            Customer customer = new Customer();
            customer.setId(i);
            customer.setCname("customer"+i);
            customer.setAge(new Random().nextInt(30)+1);
            customer.setPhone("1381111XXXX");
            customer.setSex((byte) new Random().nextInt(2));
            customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
            customerSerivce.addCustomerData(customer);
        }
    }
}

1.6 启动类

springboot 整合tx-mybaits 实现crud操作_第3张图片

 1.7 测试类

1.初始化数据库

CREATE TABLE `t_customer` (
  `id` bigint(11) NOT NULL,
  `cname` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `birth` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.页面访问

 3.查看数据库:

springboot 整合tx-mybaits 实现crud操作_第4张图片

4.查看redis

springboot 整合tx-mybaits 实现crud操作_第5张图片

二 实现查询操作

2.1 service

    @Override
    public Customer findDataByNoFilter(Integer id) {
        //1.先查询redis
        String key=CACHA_KEY_CUSTOMER+id;
       Customer customer=(Customer) redisTemplate.opsForValue().get(key);
         if(customer==null){
             System.out.println("刚开始redis不存在。。。。。。");
             //2.redis为空,查询mysql
             customer=customerDao.selectByPrimaryKey(id);
             if(customer!=null){
                 //3.mysql中数据存在, 把mysq查询出来的数据回写redis,保持一致性
                 redisTemplate.opsForValue().set(key,customer);
             }
         }
        return customer;
    }
}

2.2 controller

    @ApiOperation("单个customer查询操作,按照customerid查询")
    @RequestMapping(value = "/customer/{id}",method = RequestMethod.GET)
    public Customer findCustomerById(@PathVariable Integer id)
    {
        return customerSerivce.findDataByNoFilter(id);
    }

2.3 测试

 

你可能感兴趣的:(springboot,spring,boot,java,spring)