栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

快速开始 springboot项目的一个模板

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

快速开始 springboot项目的一个模板

导入依赖(采用maven去构建)

导入父依赖


        org.springframework.boot
        spring-boot-starter-parent
        2.5.12
        
    

web项目+测试+lombook+mybatisPlus+swagger。。。

  
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
        
            org.projectlombok
            lombok
            true
        
        

        
            com.auth0
            java-jwt
            3.4.0
        



        
            cn.hutool
            hutool-all
            5.7.20
        


       
        
        
            io.springfox
            springfox-boot-starter
            3.0.0
        

	
        
            com.alibaba
            druid
            1.1.19
        
        
            mysql
            mysql-connector-java
            runtime
        
		 
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        


	
        
            com.baomidou
            mybatis-plus-generator
            3.5.1
        

        
            org.apache.velocity
            velocity
            1.7
        
        
            org.freemarker
            freemarker
            2.3.31
        
	


 
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.2
        
        
        
            com.alibaba
            druid
            1.1.19
        
        
        
            mysql
            mysql-connector-java
            runtime
        




        
            org.springframework.boot
            spring-boot-starter-data-redis
        

   

        
            org.apache.shiro
            shiro-spring-boot-starter
            1.5.3
        


   
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
            jstl
            jstl
            1.2
        


 		
            org.apache.poi
            poi-ooxml
            4.1.2
        




打jar包插件


        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.6.4
            
        
    

application模板

spring:
  profiles:
    active: dev
  application:
    name: AppName
mybatis-plus:
  mapper-locations: classpath:/com/yan/*.xml
  type-aliases-package: com.yan.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    

application-dev

server:
  port: 8888
spring:
  datasource:
    username: root
    password: 1234
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/hotelsys?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useSSL=false&failOverReadOnly=false
    driver-class-name: com.mysql.cj.jdbc.Driver
  redis:
    host: 192.168.83.100
    port: 6380
    database: 1
logging:
  level:
    com.yan.mapper: debug

mapper.xml模板



      

配置类

swagger

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("组一")
                .apiInfo(apiInfo(""))
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yan.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(String title, String version) {
        return new ApiInfoBuilder()
                .title(title)
                .description("")
                .termsOfServiceUrl("")
                .version(version)
                .build();
    }
}

页面地址 http://localhost:8080/swagger-ui/index.html

redis格式化

package com.yan.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 使用Jackson2JsonRedisSerialize替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(new LaissezFaireSubTypeValidator(),
                ObjectMapper.DefaultTyping.EVERYTHING);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号