Code Monkey home page Code Monkey logo

springboot-example's Introduction

springBoot-example

1、技术架构

后端以springboot、maven多模块为基础框架,数据库为 mysql + redis ,实现简单的 CRUD 功能。前后端以RESTFUL风格的ajax请求来进行交互。

2、项目分层

  • springBoot-api 控制层,主要是各类controller

    • 实现对mysql常见的CRUD请求(PUT、DELETE、PATCH、POST、GET等),以自定义的Response来返回至客户端(主要体现在 RedisExampleController.java类中)
    • 实现SpringBoot下redis的set与get(主要体现在 RedisExampleController.java类中)
  • springBoot-base 接口层,包含service接口和entiy实体类

  • springBoot-util 工具类层

  • 项目代码总体结构如下:

项目代码总体结构

3、项目启动

编辑 springBoot-example/springBoot-api/src/main/resources/application-dev.properties 文件,修改其中的jdbc链接信息,例如:

spring.datasource.username=root
spring.datasource.password=admin

再修改其redis链接信息:

# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379

注意:该redis 也可以不配置,不影响项目正常启动,但与redis相关功能无法使用

项目成功启动时,控制台: 项目成功启动时,控制台

4、springboot + redis 相关

  • 代码如下:
@RestController
public class RedisExampleController {


    @Autowired
    private IRedisService redisService;


    @RequestMapping("/redis/set")
    public Object redisSet(@RequestParam("value")String value){
        boolean isOk = redisService.setString("name", value);
        if(isOk){
            return new XPFSingleResponse("redis新增成功");
        }else{
            return new XPFBadRequestException("redis新增失败");
        }
    }

    @RequestMapping("/redis/get")
    public Object redisGet(){
        String name = redisService.getString("name");
        return new XPFSingleResponse("redis获取:" + name);
    }
}

redis赋值测试

redis赋值测试

5、springboot + swagger2 相关

1、添加依赖信息 <!--使用swagger start--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <!--使用swagger end--> 2、添加swagger配置

  • SwaggerConfig.java
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.backstage.api.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("springBoot-example RESTful APIs")
                    .description("关注我 https://github.com/jiangcaijun/")
                    .termsOfServiceUrl("https://github.com/jiangcaijun/")
                    .contact("jiangcaijun")
                    .version("1.0.0")
                    .build();
        }
    }
    
  • ServletContextConfig.java如下:
    /**
     * 拦截器配置
     * @author jiangcaijun
     *
     */
    @Configuration
    public class ServletContextConfig extends WebMvcConfigurationSupport {
    
        /**
         * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。
         * 需要重新指定静态资源
         * @param registry
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }
    
    
        /**
         * 配置servlet处理
         */
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
    }
    

3、在控制层的类或者方法上添加注解

@Api(description = "redis测试接口")
@RestController
public class RedisExampleController {

    @Autowired
    private IRedisService redisService;


    @ApiOperation(value = "redis赋值")
    @GetMapping("/redis/set")
    public Object redisSet(@RequestParam("value")String value){

4、访问localhost:端口号/项目名/swagger-ui.html 例如,该项目可访问:http://localhost:7500/zeus/swagger-ui.html

swagger接口

springboot-example's People

Contributors

jiangcaijun avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.