spring boot + MyBatis3 +MySql建立增删改查接口

spring boot + MyBatis3 +MySql建立增删改查接口,第1张

spring boot + MyBatis3 +MySql建立增删改查接口 1、建立如下项目结构


如果不使用swagger和CorsConfig可以不建立config文件夹

2、pom.xml配置


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent

        2.5.6
         
    
    com.school
    gaoxin
    0.0.1-SNAPSHOT
    gaoxin
    Demo project for Spring Boot
    
        1.8
    
    


        
            io.springfox
            springfox-swagger2
            2.9.2
        

        
            io.springfox
            springfox-swagger-ui
            2.9.2
        


        
            com.google.guava
            guava
            20.0
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.fasterxml.jackson.core
            jackson-core
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3、配置yml
server:
  port: 9580
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
                                  //test改为你的数据库名
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root  //数据库名称
    password: root  //密码
  web:
    resources:
      static-locations: file:E:springboottestsrcmainresourcesimages
  thymeleaf:
    cache: false


mybatis:
  mapper-locations: classpath:mapper/*.xml


spring.mvc.static-path-pattern: /images/**

4、建立实体类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
//使用Api需要swaager
@ApiModel("Boss直聘实体类")
public class Boss {
    @ApiModelProperty("ID")
    private int Id;
    @ApiModelProperty("职位名称")
    private String work_name;
    @ApiModelProperty("职位工资")
    private String work_money;
    @ApiModelProperty("工作地址")
    private String work_address;
    @ApiModelProperty("工作经验")
    private String work_year;
    @ApiModelProperty("学历要求")
    private String work_study;
    @ApiModelProperty("任职公司")
    private String work_company;
    @ApiModelProperty("职位内容")
    private String work_type;
    @ApiModelProperty("公司现状")
    private String work_solar;


    public int getId() {
        return Id;
    }

    public void setId(int id) {
        this.Id = id;
    }

    public String getWork_name() {
        return work_name;
    }

    public void setWork_name(String work_name) {
        this.work_name = work_name;
    }

    public String getWork_money() {
        return work_money;
    }

    public void setWork_money(String work_money) {
        this.work_money = work_money;
    }

    public String getWork_address() {
        return work_address;
    }

    public void setWork_address(String work_address) {
        this.work_address = work_address;
    }

    public String getWork_year() {
        return work_year;
    }

    public void setWork_year(String work_year) {
        this.work_year = work_year;
    }

    public String getWork_study() {
        return work_study;
    }

    public void setWork_study(String work_study) {
        this.work_study = work_study;
    }

    public String getWork_company() {
        return work_company;
    }

    public void setWork_company(String work_company) {
        this.work_company = work_company;
    }

    public String getWork_type() {
        return work_type;
    }

    public void setWork_type(String work_type) {
        this.work_type = work_type;
    }

    public String getWork_solar() {
        return work_solar;
    }

    public void setWork_solar(String work_solar) {
        this.work_solar = work_solar;
    }

    public Boss(int id, String work_name, String work_money, String work_address, String work_year, String work_study, String work_company, String work_type, String work_solar) {
        Id = id;
        this.work_name = work_name;
        this.work_money = work_money;
        this.work_address = work_address;
        this.work_year = work_year;
        this.work_study = work_study;
        this.work_company = work_company;
        this.work_type = work_type;
        this.work_solar = work_solar;
    }
}
5、建立Mapper接口
import com.school.gaoxin.entity.Boss;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface BossMapping {

       
       List findAll();


       
       Boss SearchBossByID(Integer Id);

       

       void save(Boss boss);

       
       void delete(Integer Id);


       
       void update(Boss boss);
}
6、建立Service
import com.school.gaoxin.Mapper.BossMapping;
import com.school.gaoxin.entity.Boss;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BossServer {


    @Autowired
    private BossMapping bossMapping;

    public List findAll(){
        return bossMapping.findAll();
    }

    public Boss SearchBossByID(Integer Id){ return bossMapping.SearchBossByID(Id);}

    

    public void save(Boss boss){
        bossMapping.save(boss);
    }

    
    public void delete(Integer Id){
         bossMapping.delete(Id);
    }

    
    public void update(Boss boss){
         bossMapping.update(boss);
    }
}
7、建立Mybatis的.xml文件





    
        SELECT * FROM boss where Id = #{Id}
    

    

    

    

8、建立控制类
import com.school.gaoxin.Service.BossServer;
import com.school.gaoxin.entity.Boss;
import com.school.gaoxin.utils.CommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "Boss直聘类")
@RestController
@CrossOrigin   //解决跨域问题
public class BossController {


    @Autowired
    private BossServer bossService;

    @ApiOperation("获取Boss数据")
    @GetMapping("/boss")
    public CommonResponse findAll(){
        return CommonResponse.ok().data("items" ,bossService.findAll());
    }


    @ApiOperation("使用ID获取Boss数据")
    @PostMapping("/bossByID/{id}")
    public CommonResponse SearchBossByID(@PathVariable Integer id){
            System.out.println(id);
            return CommonResponse.ok().data("items" ,bossService.SearchBossByID(id));
    }

    @ApiOperation("插入Boss数据")
    @PostMapping("/insert")
    public CommonResponse save(Boss boss){
        System.out.println(boss);
        bossService.save(boss);
        return CommonResponse.ok().data("items" ,"插入成功");
    }

    @ApiOperation("删除Boss数据")
    @PostMapping("/delete/{id}")
    public CommonResponse delete(@PathVariable Integer id){
        System.out.println(id);
        bossService.delete(id);
        return CommonResponse.ok().data("items" ,"删除成功");
    }


    @ApiOperation("更新Boss数据")
    @PostMapping("/update")
    public CommonResponse update(Boss boss){

        bossService.update(boss);
        return CommonResponse.ok().data("items" ,"更改成功");
    }





}

进入swagger调试代码

获取所有数据

通过Id获取信息


剩下的我就不演示了

到这里整个流程就走完了。下面都是一些配置以及响应类

swagger配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {


    //配置了Swagger的Docket的Bean实例
     @Bean
     public Docket docket(){
         return new Docket(documentationType.SWAGGER_2)
                 .apiInfo(apiInfo())
                 .groupName("又菜又爱玩")
                 .select()
                 //RequestHandlerSelectors配置要扫描的接口的方式
                 //basePackage指定扫描的包
                 .apis(RequestHandlerSelectors.basePackage("com.school.gaoxin"))
                 //过滤路径
                 //.paths(PathSelectors.any(""))
                 .build();  //build
     }

     //配置Swagger 信息 = apiInfo
     private ApiInfo apiInfo(){

         //作者信息
         Contact Contact = new Contact("李卓轩", "https://blog.csdn.net/biancheng4?spm=1000.2115.3001.5343", "1605724068@qq.com");
         return  new ApiInfo("高薪一站通",
                 "信息工程学院105工作室",
                 "1.0", "https://blog.csdn.net/biancheng4?spm=1000.2115.3001.5343",
                 Contact, "Apache 2.0",
                 "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());
     }




}
Cors配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


@Configuration
public class CorsConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("
    private final Integer code;
    
    private final String message;

    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer code(){
        return this.code;
    }
    public String message(){
        return this.message;
    }



}



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636588.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存