首页 > 数据库 >SpringBoot项目常用配置文件MybatisPlusConfig、RedisConfig、RedissonConfig、SwaggerConfig、WebMvcConfig

SpringBoot项目常用配置文件MybatisPlusConfig、RedisConfig、RedissonConfig、SwaggerConfig、WebMvcConfig

时间:2024-09-03 16:16:02浏览次数:4  
标签:MybatisPlusConfig SpringBoot 配置文件 class return API template new public

MybatisPlusConfig:

@Configuration
@MapperScan("com.yupi.usercenter.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

RedisConfig:

@Configuration
public class RedisConfig {
    @Bean
    //配置redisTemplate
    // 默认情况下的模板只能支持 RedisTemplate<String,String>,
    // 只能存入字符串,很多时候,我们需要自定义 RedisTemplate ,设置序列化器
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> template = new RedisTemplate <>();
        template.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

}

RedissonConfig:

@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {
    private String host;
    private String port;
    @Bean
    public RedissonClient reddissonClient(){
        // 1.创建配置
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s", host,port);
        config.useSingleServer().setAddress(redisAddress).setDatabase(3);

        //2. 创建实例
        RedissonClient redisson = Redisson.create(config);

        return redisson;

    }
}

SwaggerConfig:


@Configuration // 标明是配置类
@EnableSwagger2WebMvc//开启swagger功能
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // DocumentationType.SWAGGER_2 固定的,代表swagger2
                //.groupName("分布式任务系统") // 如果配置多个文档的时候,那么需要配置groupName来分组标识
                .apiInfo(apiInfo()) // 用于生成API信息
                .select() // select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
                // 扫描指定包下的接口,最为常用
                .apis(RequestHandlerSelectors.basePackage("com.yupi.usercenter.controller"))
                //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                //.withMethodAnnotation(PostMapping.class) // 扫描带有指定注解的方法接口
                //.apis(RequestHandlerSelectors.any()) // 扫描所有

                // 选择所有的API,如果你想只为部分API生成文档,可以配置这里
                .paths(PathSelectors.any()
                        //.any() // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/user/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }


    /**
     * 用于定义API主界面的信息,比如可以声明所有的API的总标题、描述、版本
     *
     * @return
     */
    private ApiInfo apiInfo() {

        Contact contact = new Contact(
                "Alex", // 作者姓名
                "https://www.cnblogs.com/Alex-goforit", // 作者网址
                ""); // 作者邮箱

        return new ApiInfoBuilder()
                .title("学术伙伴匹配项目API") //  可以用来自定义API的主标题
                .description("学术伙伴匹配项目SwaggerAPI管理") // 可以用来描述整体的API
                .termsOfServiceUrl("https://www.cnblogs.com/Alex-goforit") // 用于定义服务的域名(跳转链接)
                .version("1.0") // 可以用来定义版本
                .contact(contact)
                .build(); //
    }
}

WebMvcConfig:

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                //当**Credentials为true时,**Origin不能为星号,需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
                /* 放自己的前端域名*/
                .allowedOrigins("http://localhost:3000/")
                .allowedHeaders("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }
}

标签:MybatisPlusConfig,SpringBoot,配置文件,class,return,API,template,new,public
From: https://www.cnblogs.com/Alex-goforit/p/18394814

相关文章