项目目录
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
insert into user (id,username,password)
values (#{id},#{username},#{password})
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;
/**
*
*
*
*
* @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
查询数据
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">
4.0.0
org.project
blog1
1.0-SNAPSHOT
14
14
org.springframework.boot
spring-boot-starter-parent
2.4.5
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
8.0.29
org.projectlombok
lombok
true
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
com.github.xiaoymin
swagger-bootstrap-ui
1.9.2
org.freemarker
freemarker
2.3.30
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: Java开发 – 不知道算不算详细的分布式事务详解
前言 前日对JUC进行了一个深度总结,不过现在博主能记得的也不多了,只是这东西,不是看几遍写几遍就能完全记住的,功夫在平时,很多知识点都需要反复的看,不光要看,还要用,这样才能了解其原理,掌握其原理,从而能更好的运用于平时的开发中,提高开发效率。今天给大家带来…