这里简单总结下各个组件常用依赖 基于SpringBoot2.2.10.RELEASE 可能因为版本不同有所区别,实际使用时自由发挥
一.数据库相关
1.mysql驱动
<!--Mysql依赖包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2.mybatis
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency>
3.druid数据源驱动
<!-- druid数据源驱动 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.24</version> </dependency>
二.SpringBoot相关
1.必备的父依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.10.RELEASE</version> </parent>
2.一般web开发需要
<!--Spring boot Web容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
3.单元测试junit
<!--Spring boot 测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
4.spring-data-jpa
<!--Spring boot 核心--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
5.security安全框架
<!--Spring boot 安全框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
6.spring boot缓存
<!-- spring boot 缓存 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
7.spring boot集成redis
<!--Spring boot Redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
8.spring boot集成redis所需的common-pool2
<!--spring boot 集成redis所需common-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.5.0</version> </dependency>
9.springboot集成thymeleaf模板引擎
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
10.springboot websocket
<!-- Spring boot websocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
11.AOP
<!--AOP--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
相应切面类实现例子:
@Aspect @Component public class ServiceLogAspect { public static final Logger log = LoggerFactory.getLogger(ServiceLogAspect.class); /** * AOP通知: * 1. 前置通知:在方法调用之前执行 * 2. 后置通知:在方法正常调用之后执行 * 3. 环绕通知:在方法调用之前和之后,都分别可以执行的通知 * 4. 异常通知:如果在方法调用过程中发生异常,则通知 * 5. 最终通知:在方法调用之后执行 */ /** * 切面表达式: * execution 代表所要执行的表达式主体 * 第一处 * 代表方法返回类型 *代表所有类型 * 第二处 包名代表aop监控的类所在的包 * 第三处 .. 代表该包以及其子包下的所有类方法 * 第四处 * 代表类名,*代表所有类 * 第五处 *(..) *代表类中的方法名,(..)表示方法中的任何参数 * * @param joinPoint * @return * @throws Throwable */ @Around("execution(* com.xuxu.service.impl..*.*(..))") public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable { log.info("====== 开始执行 {}.{} ======", joinPoint.getTarget().getClass(), joinPoint.getSignature().getName()); // 记录开始时间 long begin = System.currentTimeMillis(); // 执行目标 service Object result = joinPoint.proceed(); // 记录结束时间 long end = System.currentTimeMillis(); long takeTime = end - begin; if (takeTime > 3000) { log.error("====== 执行结束,耗时:{} 毫秒 ======", takeTime); } else if (takeTime > 2000) { log.warn("====== 执行结束,耗时:{} 毫秒 ======", takeTime); } else { log.info("====== 执行结束,耗时:{} 毫秒 ======", takeTime); } return result; } }
三.常用第三方工具
1.commons
<!-- apache 工具类 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
2.lombok
<!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
3.slf4j日志门面+logback实现
<!--slf4j日志门面:可以通过适配器让老的日志框架实现对slf4j的支持,也可以在不改动原有日志框架的基础上通过桥接升级为别的日志框架--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <!--logback日志--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
4.离线IP地址定位库
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>1.7.2</version> </dependency>
5.数据传输对象(DTO)
<!--mapStruct依赖--> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${mapstruct.version}</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
6.fastjson
<!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency>
7.java图形验证码
<!-- Java图形验证码 --> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
8.解析客户端操作系统,浏览器信息
<!-- 解析客户端操作系统、浏览器信息 --> <dependency> <groupId>nl.basjes.parse.useragent</groupId> <artifactId>yauaa</artifactId> <version>5.23</version> </dependency>
9.JWT(token验证)
<!-- jwt --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.11.1</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.11.1</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.11.1</version> </dependency>
10.定时任务Quartz
<!-- SpringBoot 整合 Quartz 定时任务 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> <version>2.3.5.RELEASE</version> </dependency>
四.辅助类工具
1.swagger-ui接口文档
<!-- Swagger UI 相关 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency>
需要配置config配置文件:
@Configuration @EnableSwagger2 public class SwaggerConfig { // http://localhost:8080/swagger-ui.html 原路径 // http://localhost:8080/doc.html 原路径 // 配置swagger2核心配置 docket @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2 .apiInfo(apiInfo()) // 用于定义api文档汇总信息 .select() .apis(RequestHandlerSelectors .basePackage("com.xuxu.controller")) // 指定controller包 .paths(PathSelectors.any()) // 所有controller .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("swagger-api") // 文档页标题 .contact(new Contact("xuxu", "https://www.xuxu.com", "abc@xuxu.com")) // 联系人信息 .description("xuxu提供的api文档") // 详细信息 .version("1.0.1") // 文档版本号 .termsOfServiceUrl("https://www.xuxu.com") // 网站地址 .build(); } }
实体类和接口上使用注解的列子:
@ApiModel(value = "用户业务对象",description = "从客户端传入用户信息数据") @Data public class UserBO { @ApiModelProperty(value = "用户名",name="username",example = "xuxu",required = true,dataType ="String") private String username; @ApiModelProperty(value = "密码",name="password",example = "12345",required = true) private String password; @ApiModelProperty(value = "确认密码",name="confirmPassword",example = "12345",required = true) private String confirmPassword; }
@Api(value = "用户接口",tags = {"用于用户注册相关接口"}) @RestController public class UserController { @Autowired private UserService userService; @ApiOperation(value = "查询用户名是否存在",notes = "查询用户名是否存在",httpMethod = "GET") @GetMapping("/queryUsernameExist/{username}") public IMOOCJSONResult queryUsernameExist(@PathVariable("username") String username){ if(StringUtils.isBlank(username)){ return IMOOCJSONResult.errorMsg("用户名为空"); } boolean result = userService.queryUsernameExist(username); if(result){ return IMOOCJSONResult.errorMsg("用户名已存在"); } return IMOOCJSONResult.ok(); } @ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST") @PostMapping("/register") public IMOOCJSONResult register(@RequestBody UserBO userBO){ String username =userBO.getUsername(); String password = userBO.getPassword(); String comfirmPwd = userBO.getConfirmPassword(); if(StringUtils.isBlank(username) ||StringUtils.isBlank(password) ||StringUtils.isBlank(comfirmPwd)){ return IMOOCJSONResult.errorMsg("参数不能为空"); } Users user = userService.register(userBO); return IMOOCJSONResult.ok(user); } }
参考链接:https://blog.csdn.net/baiyan3212/article/details/104426735/
slf4j