diff --git a/pom.xml b/pom.xml index 5b0293a13e1b10e14e0adce527e5abb7a1e700f8..7e28bdbf135a104eeb5b9115cfc2cd13f351949f 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,32 @@ druid-spring-boot-starter 1.1.10 + + + org.springframework.boot + spring-boot-starter-data-redis + + + io.lettuce + lettuce-core + + + + + redis.clients + jedis + + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + diff --git a/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java b/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java deleted file mode 100644 index bd4a6e7e73109833e92f145a12f686fa663b6669..0000000000000000000000000000000000000000 --- a/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.kwan.springbootkwan.config; - -import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import javax.sql.DataSource; - -@Configuration -public class DataSourceConfig { - - @Bean - @ConfigurationProperties("spring.datasource.one") - DataSource dataSourceOne() { - return DruidDataSourceBuilder.create().build(); - } - - @Bean - @ConfigurationProperties("spring.datasource.two") - DataSource dataSourceTwo() { - return DruidDataSourceBuilder.create().build(); - } -} diff --git a/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java b/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..1c18b95b87a883ef166e400a13c176042476abd6 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java @@ -0,0 +1,40 @@ +package com.kwan.springbootkwan.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +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; + +@Configuration +public class RedisConfig { + + @Bean + @SuppressWarnings("all") + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + System.out.println("==================自定义的redisTemplate实例化了==================="); + RedisTemplate 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; + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java b/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java new file mode 100644 index 0000000000000000000000000000000000000000..aab803d51339f64e189f90dc119b067c85c62268 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java @@ -0,0 +1,46 @@ +package com.kwan.springbootkwan.config; + +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.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * 通过@Configuration注解,表明它是一个配置类 @EnableSwagger2开启swagger2。 apiINfo()配置一些基本的信息。apis()指定扫描的包会生成文档。 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 17:23 + */ +@Configuration +@EnableSwagger2 +public class Swagger2Config { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .groupName("SpringBoot学习") + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.kwan.springbootkwan.controller"))//需要生成接口文档的包名 + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("SpringBoot学习") + .description("最怕一生碌碌无为,还安慰自己平凡可贵。") + .termsOfServiceUrl("http://qinyingjie.top/") + .contact(new Contact("kwan", "http://qinyingjie.top/", "3327782001@qq.com")) + .version("Apache 2.0") + .build(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/controller/BookController.java b/src/main/java/com/kwan/springbootkwan/controller/BookController.java index ab087d22f45a6187a6012c5771f0c191d29d3406..89e0298774c521232d94cc6401b483b803eb4ead 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/BookController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/BookController.java @@ -1,5 +1,7 @@ package com.kwan.springbootkwan.controller; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; /** @@ -9,16 +11,18 @@ import org.springframework.web.bind.annotation.*; * @version : 2.2.0 * @date : 2022/12/19 16:10 */ +@Api(description = "书籍信息", tags = "BookController") @RestController public class BookController { + @ApiOperation(value = "添加书籍", notes = "添加书籍") @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") @PostMapping("addBook") public String addBook(String name) { return "receive:" + name; } - + @ApiOperation(value = "删除书籍", notes = "删除书籍") @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") @DeleteMapping("deleteBook/{id}") public String deleteBook(@PathVariable(value = "id") String id) { diff --git a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java index 5f1a1c2353c65419ec59f55e082cbdbad30dc230..df2b5206238a700e1b74acbb9ecb66aa359e768d 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java @@ -1,5 +1,7 @@ package com.kwan.springbootkwan.controller; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; @@ -19,6 +21,7 @@ import java.util.UUID; * @version : 2.2.0 * @date : 2022/12/19 16:07 */ +@Api(description = "文件上传信息", tags = "FileUploadController") @RestController public class FileUploadController { @@ -29,6 +32,7 @@ public class FileUploadController { * @param req * @return */ + @ApiOperation(value = "单个文件上传", notes = "单个文件上传") @PostMapping("/upload") public String upload(MultipartFile uploadFile, HttpServletRequest req) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); @@ -62,6 +66,7 @@ public class FileUploadController { * @param req * @return */ + @ApiOperation(value = "批量上传", notes = "批量上传") @PostMapping("/uploadBatch") public String uploadBatch(@RequestPart("uploadFile") MultipartFile[] uploadFiles, HttpServletRequest req) { //遍历 uploadFiles 数组分别存储 diff --git a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java index 9b601622812d8cbde8abfac74701eff1dbaf3b96..92ee4b74c96f04637c709c85e55a8a3202831832 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java @@ -3,6 +3,8 @@ package com.kwan.springbootkwan.controller; import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.service.IUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -16,6 +18,7 @@ import java.util.List; * @version : 2.2.0 * @date : 2022/12/19 16:08 */ +@Api(description = "person用户信息", tags = "PersonController") @RestController @RequestMapping("/person") public class PersonController { @@ -26,6 +29,7 @@ public class PersonController { * "birthday": "2022-12-19" * } */ + @ApiOperation(value = "json返回", notes = "json返回") @GetMapping("/person") public Person person() { Person person = new Person(); diff --git a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java new file mode 100644 index 0000000000000000000000000000000000000000..c28a5c5222dbc423f715a81f9102968945132bd5 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java @@ -0,0 +1,44 @@ +package com.kwan.springbootkwan.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; + +@Api(description = "redis测试信息", tags = "RedisController") +@RestController +@RequestMapping("/test") +public class RedisController { + + public static final String REDIS_KEY = "test:_w"; + + @Resource + private RedisTemplate redisTemplate; + + + @ApiOperation(value = "redis添加数据", notes = "redis添加数据") + @GetMapping("/add") + public void add() { + //当前时间时间戳 + String nowStr = String.valueOf(System.currentTimeMillis()); + //设置redis中的key值 + String redisKey = REDIS_KEY.concat(nowStr); + //向对应的key值添加数据 + redisTemplate.opsForValue().set(REDIS_KEY, "测试向redis中添加数据"); + //设置过期时间 + redisTemplate.expire(REDIS_KEY, 2000000, TimeUnit.SECONDS); + } + + @ApiOperation(value = "redis获取数据", notes = "redis获取数据") + @GetMapping("/get") + public void get() { + //获取redis中的数据 + String str = (String) redisTemplate.opsForValue().get(REDIS_KEY); + System.out.println("获取redis中的数据-->" + str); + } +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/UserController.java b/src/main/java/com/kwan/springbootkwan/controller/UserController.java index 4c5c08e727decda343b409c435a0f27fa0a0a25a..1d43e7e5c8dc130ab743e5619ffc9b77956cef5e 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/UserController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/UserController.java @@ -1,12 +1,12 @@ package com.kwan.springbootkwan.controller; -import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.service.IUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.Date; import java.util.List; /** @@ -16,6 +16,7 @@ import java.util.List; * @version : 2.2.0 * @date : 2022/12/19 16:08 */ +@Api(description = "用户信息", tags = "UserController") @RestController @RequestMapping("/user") public class UserController { @@ -23,11 +24,13 @@ public class UserController { @Autowired private IUserService userService; + @ApiOperation(value = "获取所有用户", notes = "获取所有用户") @RequestMapping(value = "/all", method = RequestMethod.GET) public List addAdvertise() { return userService.getUsers(); } + @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息") @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET) public User getUserById(@PathVariable Integer id) { return userService.getUserById(id); diff --git a/src/main/java/com/kwan/springbootkwan/entity/Person.java b/src/main/java/com/kwan/springbootkwan/entity/Person.java index 6e1b3993c2f6d19feb46778cca54b1b840a9eccf..a609748fff6ba33bf73a3eec7522bafd7a895826 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/Person.java +++ b/src/main/java/com/kwan/springbootkwan/entity/Person.java @@ -1,13 +1,18 @@ package com.kwan.springbootkwan.entity; import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import java.util.Date; +@ApiModel(value = "用户基本信息") public class Person { - + @ApiModelProperty(value = "姓名") private String name; + @ApiModelProperty(value = "年龄") private Integer age; + @ApiModelProperty(value = "生日") @JsonFormat(pattern = "yyyy-MM-dd") private Date birthday; diff --git a/src/main/java/com/kwan/springbootkwan/entity/User.java b/src/main/java/com/kwan/springbootkwan/entity/User.java index 27738fae599f455a97574dbaca34974ffee8cc86..20546ac70f2c1930d6fa6c1267e8f08d07ae17b9 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/User.java +++ b/src/main/java/com/kwan/springbootkwan/entity/User.java @@ -1,18 +1,27 @@ package com.kwan.springbootkwan.entity; + import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import lombok.Builder; import lombok.Data; @Data @Builder @TableName(value = "user") -public class User { +@ApiModel(value = "用户基本信息") +public class User { + @ApiModelProperty(value = "主键id") @TableId(value = "id", type = IdType.AUTO) private Integer id; + @ApiModelProperty(value = "姓名") private String name; + @ApiModelProperty(value = "性别") private String sex; + @ApiModelProperty(value = "密码") private String pwd; + @ApiModelProperty(value = "邮箱") private String email; } diff --git a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java index 19688a541c4d62198f6eab2f116a7ebc3b3bf613..4f210e9963ed54bf7cd16f67bde2c40014887978 100644 --- a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java +++ b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kwan.springbootkwan.entity.User; import org.apache.ibatis.annotations.Mapper; + /** * mapper文件 * diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 02e415c7be16b0c1eb8f91bd41e5090e39ed3760..b8c6909a92c25c555ee19a736b4d25af8d080646 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -6,6 +6,17 @@ swagger: #兼容swagger配置 spring: + redis: + database: 0 # Redis数据库索引(默认为0) + host: 120.79.36.53 #Redis服务器地址 + port: 6379 # Redis服务器连接端口 + password: # Redis服务器连接密码(默认为空) + jedis: + pool: + max-active: 200 # 连接池最大连接数(使用负值表示没有限制) + max-idle: 10 # 连接池中的最大空闲连接 + min-idle: 0 # 连接池中的最小空闲连接 + max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) mvc: pathmatch: matching-strategy: ant_path_matcher