创建项目之初,将需要的pom一次性导入
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.7.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.quanttrade_bk.QuantTradeBkApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
解释:3380是MySql的端口号
root是MySql的用户名
1234是MySql的密码
# 应用服务 WEB 访问端口
server.port=8080
# 配置数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3380/db_quant?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234
#配置mapper.xml的路径
mybatis.mapper-locations=classpath:mapper/*/*.xml
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class QuantTradeApplication {
public static void main(String[] args) {
SpringApplication.run(QuantTradeApplication.class, args);
}
/**
* 向spring注册RestTemplate工具
* @return
*/
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
启动项目,运行:
http://localhost:8080/swagger-ui/index.html
能够正常进入swagger2,说明SpringBoot项目已经启动成功
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RestObject {
private Integer code; //前端的状态码 0:业务失败 1:业务成功
private String msg; //前端需要展示的信息
private Object data; //前端需要的复杂数据
//业务成功的方法----------
/**
* 业务成功,自定义返回msg和返回的数据
* @param msg
* @param data
* @return
*/
public static RestObject OK(String msg,Object data){
return new RestObject(1,msg,data);
}
/**
* 业务成功,自定义返回msg,无返回数据
* @param msg
* @return
*/
public static RestObject OK(String msg){
return OK(msg,null);
}
/**
* 业务成功,自定义返回的数据,无返回msg
* @param data
* @return
*/
public static RestObject OK(Object data){
return OK(null,data);
}
//业务失败的方法----------
/**
* 业务失败,自定义返回msg和返回的数据
* @param msg
* @param data
* @return
*/
public static RestObject ERROR(String msg,Object data){
return new RestObject(0,msg,data);
}
/**
* 业务失败,自定义返回msg,无返回数据
* @param msg
* @return
*/
public static RestObject ERROR(String msg){
return ERROR(msg,null);
}
/**
* 业务失败,自定义返回的数据,无返回msg
* @param data
* @return
*/
public static RestObject ERROR(Object data){
return ERROR(null,data);
}
}
之所以不叫做User,是为了减少撞库问题。
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Member {
private Integer id;
//JsonProperty是为了和前端json请求对应
@JsonProperty("userName")
private String account;
@JsonProperty("passWord")
private String authstring;
//repeat非数据库字段,是为了进行密码一致性校验
private String repeat;
}
MemberMapper
import org.springframework.stereotype.Repository;
@Repository
public interface MemberMapper {
}
Application
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@MapperScan("com.quanttrade.member.mapper")
public class QuantTradeApplication {
public static void main(String[] args) {
SpringApplication.run(QuantTradeApplication.class, args);
}
/**
* 向spring注册RestTemplate工具
* @return
*/
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
MapperMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.quanttrade.member.mapper.MemberMapper">
</mapper>
MemberService
import com.quanttrade.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MemberService {
@Autowired
private MemberMapper memberMapper;
}
MemberController
import com.quanttrade.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/member")
@CrossOrigin //测试阶段进行跨域,项目正式上线时,会将资源整合到static中,到时候就无需跨域
public class MemberController {
@Autowired
private MemberService memberService;
}
MemberController
import com.quanttrade.member.javabean.Member;
import com.quanttrade.member.service.MemberService;
import com.quanttrade.utils.javabean.RestObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/member")
@CrossOrigin //测试阶段进行跨域,项目正式上线时,会将资源整合到static中,到时候就无需跨域
public class MemberController {
@Autowired
private MemberService memberService;
/**
* 注册接口
* @param param 用户名、密码、确认密码
* @return
*/
@ApiOperation("注册接口")
@PostMapping("/register")
public @ResponseBody RestObject register(@RequestBody Member param){
return memberService.register(param);
}
}
MemberService
/**
* 注册用户信息
* @param param 用户名、密码、确认密码
* @return 注册结果
*/
public RestObject register(Member param) {
//1、合法性校验
//1.0、防止空指针异常
if(param == null){
return RestObject.ERROR("注册失败,请联系管理员");
}
//1.1、用户名必须存在
if(param.getAccount()==null || "".equals(param.getAccount().trim())){
return RestObject.ERROR("注册失败,请先填写用户名!");
}
//1.2、密码必须存在
if(param.getAuthstring()==null || "".equals(param.getAuthstring().trim())){
return RestObject.ERROR("注册失败,请先填写密码!");
}
//1.3、确认密码必须存在
if(param.getRepeat()==null || "".equals(param.getRepeat().trim())){
return RestObject.ERROR("注册失败,请先填写确认密码!");
}
//1.4、密码和确认密码必须保持一致
if(!param.getAuthstring().equals(param.getRepeat())){
return RestObject.ERROR("注册失败,密码和确认密码必须保持一致!");
}
//2、先对密码进行MD5加密,替换未加密密码
param.setAuthstring(
DigestUtils.md5DigestAsHex(
param.getAuthstring().getBytes()
)
);
//3、调用mapper层保存数据
int rows = 0;
try {
rows = memberMapper.saveMemberInfo(param);
} catch (Exception e) {
String errorMsg = e.getMessage();
if(errorMsg.contains("Duplicate entry")&& errorMsg.contains("tb_member.account")){
//判断如果是用户名重复了,抛出用户名重复的问题
return RestObject.ERROR("注册失败,该用户名已注册,请更换一个新的用户名!");
}
return RestObject.ERROR("注册失败,请联系管理员");
}
//4、返回业务结果
if(rows>0){
return RestObject.OK("注册成功!");
}else{
return RestObject.ERROR("注册失败,请联系管理员");
}
}
MemberMapper
import com.quanttrade.member.javabean.Member;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface MemberMapper {
/**
* 保存用户名和密码信息
* @param param
* @return 保存成功了几条数据
*/
int saveMemberInfo(@Param("member") Member param);
}
MemberMapper.xml
<insert id="saveMemberInfo">
insert into tb_member (
account,
authstring
) values (
#{member.account},
#{member.authstring}
)
</insert>
测试:
打开swagger-ui
目前阶段echarts图表会报错,不用管
前后端项目地址下载:传送门