建立基本的springboot项目

image.png
image.png

pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.atguigu</groupId>
  6. <artifactId>payment-demo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>payment-demo</name>
  9. <description>Demo project for Spring Boot</description>
  10. <properties>
  11. <java.version>1.8</java.version>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  14. <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
  15. </properties>
  16. <dependencies>
  17. <!--web-->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <!--Swagger-->
  23. <dependency>
  24. <groupId>io.springfox</groupId>
  25. <artifactId>springfox-swagger2</artifactId>
  26. <version>2.7.0</version>
  27. </dependency>
  28. <!--Swagger ui-->
  29. <dependency>
  30. <groupId>io.springfox</groupId>
  31. <artifactId>springfox-swagger-ui</artifactId>
  32. <version>2.7.0</version>
  33. </dependency>
  34. <!--lombok-->
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. </dependency>
  39. <!--mysql 驱动-->
  40. <dependency>
  41. <groupId>mysql</groupId>
  42. <artifactId>mysql-connector-java</artifactId>
  43. </dependency>
  44. <!--MyBatis-Plus:是MyBatis的增强-->
  45. <dependency>
  46. <groupId>com.baomidou</groupId>
  47. <artifactId>mybatis-plus-boot-starter</artifactId>
  48. <version>3.3.1</version>
  49. </dependency>
  50. <!-- 生成自定义配置的元数据信息 -->
  51. <dependency>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-configuration-processor</artifactId>
  54. <optional>true</optional>
  55. </dependency>
  56. <!--微信支付SDK-->
  57. <dependency>
  58. <groupId>com.github.wechatpay-apiv3</groupId>
  59. <artifactId>wechatpay-apache-httpclient</artifactId>
  60. <version>0.3.0</version>
  61. </dependency>
  62. <!--json处理器-->
  63. <dependency>
  64. <groupId>com.google.code.gson</groupId>
  65. <artifactId>gson</artifactId>
  66. </dependency>
  67. <!--微信支付 APIv2 SDK-->
  68. <dependency>
  69. <groupId>com.github.wxpay</groupId>
  70. <artifactId>wxpay-sdk</artifactId>
  71. <version>0.0.3</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework.boot</groupId>
  75. <artifactId>spring-boot-starter-test</artifactId>
  76. <scope>test</scope>
  77. <exclusions>
  78. <exclusion>
  79. <groupId>org.junit.vintage</groupId>
  80. <artifactId>junit-vintage-engine</artifactId>
  81. </exclusion>
  82. </exclusions>
  83. </dependency>
  84. </dependencies>
  85. <dependencyManagement>
  86. <dependencies>
  87. <dependency>
  88. <groupId>org.springframework.boot</groupId>
  89. <artifactId>spring-boot-dependencies</artifactId>
  90. <version>${spring-boot.version}</version>
  91. <type>pom</type>
  92. <scope>import</scope>
  93. </dependency>
  94. </dependencies>
  95. </dependencyManagement>
  96. <build>
  97. <!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
  98. <resources>
  99. <resource>
  100. <directory>src/main/java</directory>
  101. <includes>
  102. <include>**/*.xml</include>
  103. </includes>
  104. <filtering>false</filtering>
  105. </resource>
  106. </resources>
  107. <plugins>
  108. <plugin>
  109. <groupId>org.apache.maven.plugins</groupId>
  110. <artifactId>maven-compiler-plugin</artifactId>
  111. <version>3.8.1</version>
  112. <configuration>
  113. <source>1.8</source>
  114. <target>1.8</target>
  115. <encoding>UTF-8</encoding>
  116. </configuration>
  117. </plugin>
  118. <plugin>
  119. <groupId>org.springframework.boot</groupId>
  120. <artifactId>spring-boot-maven-plugin</artifactId>
  121. <version>2.3.7.RELEASE</version>
  122. <configuration>
  123. <mainClass>com.atguigu.paymentdemo.PaymentDemoApplication</mainClass>
  124. </configuration>
  125. <executions>
  126. <execution>
  127. <id>repackage</id>
  128. <goals>
  129. <goal>repackage</goal>
  130. </goals>
  131. </execution>
  132. </executions>
  133. </plugin>
  134. </plugins>
  135. </build>
  136. </project>

yml配置文件

  1. server:
  2. port: 8090 #服务端口
  3. spring:
  4. application:
  5. name: payment-demo #应用的名字
  6. jackson:
  7. date-format: yyyy-MM-dd HH:mm:ss
  8. time-zone: GMT+8
  9. datasource:
  10. driver-class-name: com.mysql.cj.jdbc.Driver
  11. url: jdbc:mysql://localhost:3306/payment_demo?serverTimezone=GMT%2B8&characterEncoding=utf-8
  12. username: root
  13. password: 123456
  14. mybatis-plus:
  15. configuration: #sql日志
  16. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  17. mapper-locations: classpath:com/atguigu/paymentdemo/mapper/xml/*.xml
  18. logging:
  19. level:
  20. root: info

swagger配置文件

  1. package com.btlord.springboot_demo.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.spi.DocumentationType;
  6. import springfox.documentation.spring.web.plugins.Docket;
  7. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  8. @Configuration
  9. @EnableSwagger2
  10. public class Swagger2Config {
  11. @Bean
  12. public Docket docket(){
  13. return new Docket(DocumentationType.SWAGGER_2)
  14. .apiInfo(new ApiInfoBuilder().title("案例接口文档").build());
  15. }
  16. }
  1. package com.atguigu.paymentdemo.controller;
  2. import com.atguigu.paymentdemo.entity.Product;
  3. import com.atguigu.paymentdemo.service.ProductService;
  4. import com.atguigu.paymentdemo.vo.R;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.web.bind.annotation.CrossOrigin;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.annotation.Resource;
  12. import java.util.Date;
  13. import java.util.List;
  14. @CrossOrigin //开放前端的跨域访问
  15. @Api(tags = "商品管理")
  16. @RestController
  17. @RequestMapping("/api/product")
  18. public class ProductController {
  19. @Resource
  20. private ProductService productService;
  21. @ApiOperation("测试接口")
  22. @GetMapping("/test")
  23. public R test(){
  24. return R.ok().data("message", "hello").data("now", new Date());
  25. }
  26. @ApiOperation("商品列表")
  27. @GetMapping("/list")
  28. public R list(){
  29. List<Product> list = productService.list();
  30. return R.ok().data("productList", list);
  31. }
  32. }

统一结果VO类

  1. package com.atguigu.paymentdemo.vo;
  2. import lombok.Data;
  3. import lombok.experimental.Accessors;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. @Data
  7. @Accessors(chain = true)
  8. public class R {
  9. private Integer code; //响应码
  10. private String message; //响应消息
  11. private Map<String, Object> data = new HashMap<>();
  12. public static R ok(){
  13. R r = new R();
  14. r.setCode(0);
  15. r.setMessage("成功");
  16. return r;
  17. }
  18. public static R error(){
  19. R r = new R();
  20. r.setCode(-1);
  21. r.setMessage("失败");
  22. return r;
  23. }
  24. public R data(String key, Object value){
  25. this.data.put(key, value);
  26. return this;
  27. }
  28. }
  29. // 以下是使用方法
  30. package com.atguigu.paymentdemo.controller;
  31. import com.atguigu.paymentdemo.entity.Product;
  32. import com.atguigu.paymentdemo.service.ProductService;
  33. import com.atguigu.paymentdemo.vo.R;
  34. import io.swagger.annotations.Api;
  35. import io.swagger.annotations.ApiOperation;
  36. import org.springframework.web.bind.annotation.CrossOrigin;
  37. import org.springframework.web.bind.annotation.GetMapping;
  38. import org.springframework.web.bind.annotation.RequestMapping;
  39. import org.springframework.web.bind.annotation.RestController;
  40. import javax.annotation.Resource;
  41. import java.util.Date;
  42. import java.util.List;
  43. @CrossOrigin //开放前端的跨域访问
  44. @Api(tags = "商品管理")
  45. @RestController
  46. @RequestMapping("/api/product")
  47. public class ProductController {
  48. @Resource
  49. private ProductService productService;
  50. @ApiOperation("测试接口")
  51. @GetMapping("/test")
  52. public R test(){
  53. return R.ok().data("message", "hello").data("now", new Date());
  54. }
  55. @ApiOperation("商品列表")
  56. @GetMapping("/list")
  57. public R list(){
  58. List<Product> list = productService.list();
  59. return R.ok().data("productList", list);
  60. }
  61. }

json配置

用的google的gson
yml已经配好了时间

数据库

image.pngimage.pngimage.pngimage.png

依赖

  1. <!--mysql驱动 默认版本8-->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. </dependency>
  6. <!--持久层-->
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus-boot-starter</artifactId>
  10. <version>3.3.1</version>
  11. </dependency>

MyBatis-Plus的配置文件

  1. package com.atguigu.paymentdemo.config;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.transaction.annotation.EnableTransactionManagement;
  5. @Configuration
  6. @MapperScan("com.atguigu.paymentdemo.mapper")
  7. @EnableTransactionManagement //启用事务管理
  8. public class MyBatisPlusConfig {
  9. }
  1. package com.atguigu.paymentdemo.entity;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import lombok.Data;
  5. import java.util.Date;
  6. @Data
  7. public class BaseEntity {
  8. //定义主键策略:跟随数据库的主键自增
  9. @TableId(value = "id", type = IdType.AUTO)
  10. private String id; //主键
  11. private Date createTime;//创建时间
  12. private Date updateTime;//更新时间
  13. }

image.pngimage.png

  1. <!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.xml</include>
  7. </includes>
  8. <filtering>false</filtering>
  9. </resource>
  10. </resources>
  11. mybatis-plus:
  12. configuration: #sql日志
  13. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  14. mapper-locations: classpath:com/atguigu/paymentdemo/mapper/xml/*.xml

image.pngimage.png