通上次学的SpringBoot来整合
1、实体类(entity)
package com.cxy.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
2、mapper
package com.cxy.mapper;
import com.cxy.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Insert("INSERT INTO `mybatis`.`user`(`id`, `name`, `age`, `email`) VALUES (null, #{name} ,#{age} , #{email} )")
int addUser(User user);
@Delete("DELETE FROM `mybatis`.`user` WHERE id = #{id}")
int deleteUser(Long id);
@Update("UPDATE `mybatis`.`user` SET `name` = #{name}, `age` = #{age}, `email` = #{email} WHERE `id` = #{id}")
int updateUser(User user);
@Select("SELECT * FROM `mybatis`.`user` WHERE id = #{id} ")
User gatById(Long id);
@Select("SELECT * FROM `mybatis`.`user`")
List gatAll ();
}
3、Service
(1)Service
package com.cxy.service;
import com.cxy.entity.User;
import java.util.List;
public interface UserService {
int addUser(User user);
int deleteUser(Long id);
int updateUser(User user);
User gatById(Long id);
List gatAll ();
}
(2)Impl
package com.cxy.service.impl;
import com.cxy.entity.User;
import com.cxy.mapper.UserMapper;
import com.cxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int deleteUser(Long id) {
return userMapper.deleteUser(id);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
@Override
public User gatById(Long id) {
return userMapper.gatById(id);
}
@Override
public List gatAll() {
return userMapper.gatAll();
}
}
4、读写pom文件
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
浏览器只能发送get
请求post得通过表单
delete put
1、SwaggerConfig类
Config 主要是提供各种格式的配置文件解析功能,包括 py、json、ymal 和 yml,是一个非常基础常用类,对该类的熟练使用和具体实现深入理解,有助于灵活使用 OpenMMLab 系列框架。Config 类用于解析 OpenMMLab 开源框架的各种格式配置文件,不仅如此还实现了很多方便使用的 API,例如配置读写、配置打印、配置合并等等功能
package com.cxy.config;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Configuration 标注在类上,表示这个类一个配置类,会自动注入到ioc容器中
* @EnableSwagger2 开启swagger2的功能
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//这里一定要标注你的控制器的位置
.apis(RequestHandlerSelectors.basePackage("com.cxy.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot测试接口")
.description("springboot测试")
.termsOfServiceUrl("https://angegit.gitee.io/myblog")
.contact(new Contact("name","https://angegit.gitee.io/myblog","[email protected]"))
.version("1.0")
.build();
}
}
2、controller类
package com.cxy.controller;
import com.cxy.entity.User;
import com.cxy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//@Controller+@ResponseBody 返回
@RestController
//application programing interface 应用程序接口
@Api(value = "用户管理" ,tags = "springboot中的用户管理接口")
public class UserController {
@Autowired
private UserService userService;
/**
* 如果是 页面则可以使用@RequestMapping()
* 如果是写的接口,一定要写rest风格的注解
* @return
*/
@GetMapping("/user")
@ApiOperation(value = "查询用户",notes = "查询用户所有的信息")
public List getAll(){
return userService.getAll();
}
/**
* 肯定是需要实现数据校验的,不管是前端还是后台
* JSR303 Hutool工具类
* @param id
* @return
*/
@GetMapping("/user/{id}")
@ApiOperation(value = "查询单个用户",notes = "根据id查询用户")
public User getById(@PathVariable("id")Long id){
return userService.getById(id);
}
@PostMapping("/user")
@ApiOperation(value = "添加用户",notes = "添加一个用户")
public int addUser(User user){
return userService.addUser(user);
}
@DeleteMapping("/user/{id}")
@ApiOperation(value = "删除用户",notes = "根据id删除用户")
public int deleteUser(@PathVariable("id") Long id){
return userService.deleteUser(id);
}
@PutMapping("/user")
@ApiOperation(value = "修改用户",notes = "根据id修改用户")
public int updateUser(User user){
return userService.updateUser(user);
}
}
3、其他的都一样
接口文档,接口名称、请求类型、入参、返回值、字段的注释....
yarmapi
1、删除mapper里的注解
package com.cxy.mapper;
import com.cxy.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {
List getAll ();
}
2、创建mapper.xml文件
3、使用service重写
public interface UserService {
List getAll ();
}
impl
package com.cxy.service.impl;
import com.cxy.entity.User;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List getAll() {
return userMapper.getAll();
}
}
4、controller层是连接前端和后端的
package com.cxy.controller;
import com.cxy.entity.User;
import com.cxy.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//@Controller+@ResponseBody 返回
@RestController
//application programing interface 应用程序接口
@Api(value = "用户管理" ,tags = "springboot中的用户管理接口")
public class UserController {
@Autowired
private UserService userService;
/**
* 如果是 页面则可以使用@RequestMapping()
* 如果是写的接口,一定要写rest风格的注解
* @return
*/
@GetMapping("/user")
@ApiOperation(value = "查询用户",notes = "查询用户所有的信息")
public List getAll(){
return userService.getAll();
}
}
5、会出现报错需要配置yml文件
#配置mybatis的内容
mybatis:
mapper-locations: mapper/*Mapper.xml
type-aliases-package: com.cxy.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1、pom配置文件
com.github.pagehelper
pagehelper-spring-boot-starter
1.3.0
2、配置yml文件
#配置分页插件
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
3、service
package com.cxy.service;
import com.cxy.entity.User;
import com.github.pagehelper.PageInfo;
public interface UserService {
PageInfo getByPgae(Integer pageNum,Integer pageSize);
}
impl
package com.cxy.service.impl;
import com.cxy.entity.User;
import com.cxy.mapper.UserMapper;
import com.cxy.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public PageInfo getByPgae(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum,pageSize);
List users = userMapper.getAll();
PageInfo pageInfo = new PageInfo<>(users);
return pageInfo;
}
}