美文网首页
Springboot多模块搭建 一Service构建

Springboot多模块搭建 一Service构建

作者: 尼尔君 | 来源:发表于2018-09-25 17:33 被阅读0次

    Service

    这个层我们要做缓存还有一些其他的处理比如事物,还有一些其他的业务逻辑,验证

    项目结构

    image.png

    缓存

    image.png

    缓存注解的使用

    @Cacheable
    Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。
    参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件

    @CachePut
    和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法

    @CacheEvict
    方法执行成功后会从缓存中移除相应数据。
    参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据(设置为true时会移除所有缓存)

    pom

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.project</groupId>
        <artifactId>service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>service</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>com.project</groupId>
            <artifactId>parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>com.project</groupId>
                <artifactId>dao</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-data-redis</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
    
    </project>
    
    

    Java例子

    package com.frog.mvcdemo.controller;
    
    import com.frog.mvcdemo.entity.Frog;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.cache.annotation.CacheConfig;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.*;
    
    @CacheConfig(cacheNames = "frogtest")
    @RestController
    @RequestMapping(value = "/frogtest")
    public class FrogTestController {
    
        @Cacheable()
        @ApiOperation(value = "获取Frog的列表")
        @RequestMapping(value = "",method = RequestMethod.GET)
        public List<Frog> show(){
            List<Frog> list = new ArrayList<>();
            list.add(new Frog(1,"one",2,new Date(),"controllertest"));
            list.add(new Frog(2,"two",3,new Date(),"controllertest"));
            return list;
        }
    
        @Cacheable()
        @ApiOperation(value = "获取Frog详细信息",notes = "根据id查询对应Frog信息")
        @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int", paramType = "path")
        @RequestMapping(value = "/{id}",method = RequestMethod.GET)
        public Map<String,Object> showById(@PathVariable int id){
            Map<String,Object> map = new HashMap<>();
            if(id == 1){
                map.put("FROG",new Frog(1,"one",3,new Date(),"showById"));
                map.put("RESULT","SUCCESS");
            }else{
                map.put("FROG",null);
                map.put("RESULT","ERROR");
            }
            return map;
        }
    
        @CacheEvict(allEntries = true)
        @ApiOperation(value = "添加Frog详细信息",notes = "添加一条Frog的详细信息")
        @ApiImplicitParam(name = "frog", value = "Frog实体", required = true, dataType = "Frog")
        @RequestMapping(value = "",method = RequestMethod.POST)
        public Map<String,Object> add(@RequestBody Frog frog){
            Map<String,Object> map = new HashMap<>();
            map.put("FROG",frog);
            map.put("RESULT","SUCCESS");
            return map;
        }
    
        @CacheEvict(allEntries = true)
        @ApiOperation(value = "删除Frog详细信息",notes = "根据id删除对应Frog的详细信息")
        @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int", paramType = "path")
        @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
        public Map<String,Object> deleteById(@PathVariable int id){
            Map<String,Object> map = new HashMap<>();
            if(id != 0){
                map.put("FROG",new Frog(id,"testfrog",id,new Date(),"deleteById"));
                map.put("RESULT","SUCCESS");
            }else{
                map.put("FROG",null);
                map.put("RESULT","ERROR");
            }
            return map;
        }
    
        @CacheEvict(allEntries = true)
        @ApiOperation(value = "更新Frog详细信息",notes = "根据id更新Frog的详细信息")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int",paramType = "path"),
                @ApiImplicitParam(name = "frog", value = "Frog实体", required = true, dataType = "Frog")
        })
        @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
        public Map<String,Object> updateById(@PathVariable int id,@RequestBody Frog frog){
            Map<String,Object> map = new HashMap<>();
                map.put("FROG",frog);
                map.put("RESULT","SUCCESS");
                map.put("ID",id);
            return map;
        }
    }
    

    缓存一般加在dao层或service层,发生回滚时保持redis中数据不被清空,这里测试在Controller层加缓存。
    @CacheConfig(cacheNames = “frogtest”)代表这个Controller下需要缓存的方法对应redis中的缓存名为frogtest~keys。
    查询方法都用@Cacheable注解加入缓存
    添加更新删除@CacheEvict(allEntries = true)注解表示执行方法时删除redis中缓存名frogtest~keys下所有缓存

    事务

    image.png image.png

    相关文章

      网友评论

          本文标题:Springboot多模块搭建 一Service构建

          本文链接:https://www.haomeiwen.com/subject/duvloftx.html