项目目录
src/main/resourcesapplication.yml:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/blog1?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
mybatis:
type-aliases-package: app.repository
mapper-locations: classpath:/mapping/*.xml
mapping/userMapper.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="app.repository.UserMapper">
<insert id="insert" parameterType="app.model.User" keyColumn="id">
insert into user (id,username,password)
values (#{id},#{username},#{password})
</insert>
</mapper>
app/controller/UserController;
import app.model.User;
import app.service.imp.UserServiceImp;
import app.utils.ResultEnum;
import app.utils.ResultInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserServiceImp userServiceImp;
@PostMapping("/insert")
public ResultInfo insert(User user){
int num = userServiceImp.insert(user);
System.out.println("n:"+num);
ResultInfo resultInfo = new ResultInfo();
resultInfo.setMsg(ResultEnum.SUCCESS_CODE.getMsg());
return resultInfo;
}
}
app/model/User;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* <p>
*
* </p>
*
* @author huang
* @since 2023-05-03
*/
@Getter
@Setter
@Data
public class User {
private Integer id;
private String username;
private String password;
}
app/repository/UserMapper.java
import app.model.User;
public interface UserMapper {
public int insert(User user);
}
app/serviceUser;
import app.model.User;
public interface UserService {
public int insert(User user);
}
UserServiceImppackage;
import app.model.User;
import app.repository.UserMapper;
import app.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImp implements UserService {
@Resource
private UserMapper userMapper;
@Override
public int insert(User user) {
int num = userMapper.insert(user);
return num;
}
}
app/utils
ResultEnum.java;
public enum ResultEnum {
SUCCESS_CODE(200,"请求成功"),
SUCCESS_FAIL(500,"请求失败");
private Integer code;
private String msg;
ResultEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
ResultInfo;
public class ResultInfo {
private Integer code=200;
private String msg;
private Object result;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
app/config/SwaggerConfig;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//@Configuration是告诉Spring Boot需要加载这个配置类;@EnableSwagger2是启用Swagger2.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).
select().
apis(RequestHandlerSelectors.
basePackage("app.controller")). //扫码
paths(PathSelectors.any()).build().apiInfo(new ApiInfoBuilder().title("springboot blog app").version("1.0").build());
}
}
Applicationpackage app;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("app.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
启动数据库
创建数表
打开postman
代码debug
查询数据
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.project</groupId>
<artifactId>blog1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<!--web 启动的jar包,父包的子包-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Mybatis整合springboot的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--MySQL 的驱动依赖,点击就可以查看,此时默认的是8.0的,
也可以根据自己使用的mysql版本号,来指定版本号
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.2</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
</dependencies>
</project>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。