spring boot+mybatis+swagger整合增删查改代码

1.目录结构

 

spring boot+mybatis+swagger整合增删查改代码_第1张图片

2.配置文件(application.properties)

spring.datasource.name=shixi
spring.datasource.url=jdbc:mysql://你的地址/数据库名
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis.mapper-locations=classpath:mapper/PeopleMapper.xml,classpath:mapper/AdminMapper.xml,classpath:mapper/EmployMapper.xml
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity


#spring.profiles.active=dev

3.entity层

package com.example.demo.entity;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("父类people实体类")
public class People {
    @ApiModelProperty("people的id")
    public Integer id;  //people表的主键,也是两个子表关联的外键
    @ApiModelProperty("people的姓名")
    public String name;  //姓名
    @ApiModelProperty("people的年龄")
    public Integer age;  //年龄
    @ApiModelProperty("people的性别")
    public String sex;  //性别
    @ApiModelProperty("people的电话号码")
    public String tel;  //电话号码
    @ApiModelProperty("people的工资")
    public Integer wage;  //工资
    @ApiModelProperty("people的登录账号")
    public String username;   //登录账号
    @ApiModelProperty("people的登录密码")
    public String password;   //登录密码

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public Integer getWage() {
        return wage;
    }

    public void setWage(Integer wage) {
        this.wage = wage;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

4.mapper层的接口

package com.example.demo.mapper;

import com.example.demo.entity.People;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface PeopleMapper {
    //插入
    void save(People people);

    //删除
    int delete(int id);

    //根据id查找单个
    People findById(int id);

    //展示全部人员
    List findAll();

    //根据id修改该id值的人的信息
    int update(People people);

    //登录验证
    People login(String username,String password);
}

5.对应的mapper.xml文件的sql语句





    
    
        INSERT INTO people (id,name,age,sex,tel,wage,username,password) values (null,#{name},#{age},#{sex},#{tel},#{wage},#{username},#{password})
    

    
    
        DELETE FROM people WHERE id=#{id}
    

    
    
    
    


    
    
        UPDATE people SET name =#{name},age=#{age},sex=#{sex},tel=#{tel},wage=#{wage}, username=#{username},password=#{password} WHERE id=#{id}
    
    
    
    




 6.service层接口定义方法

package com.example.demo.service;

import com.example.demo.entity.People;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public interface PeopleService {

    void save(People people);

    int delete(int id);

    People findById(int id);

    List findAll();

    int update(People people);

    People login(String username,String password);
}

 7.serviceImpl层接口实现类(这里并不能直接调用service层的方法,不然会报错栈溢出)

package com.example.demo.serviceImpl;


import com.example.demo.entity.People;
import com.example.demo.mapper.PeopleMapper;
import com.example.demo.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PeopleServiceImpl implements PeopleService {

    @Autowired
    private PeopleMapper peopleMapper;

    @Override
    public void save(People people) {
        peopleMapper.save(people);
    }

    @Override
    public int delete(int id) {
        return peopleMapper.delete(id);
    }

    @Override
    public People findById(int id) {
        return peopleMapper.findById(id);
    }

    @Override
    public List findAll() {
        return peopleMapper.findAll();
    }

    @Override
    public int update(People people) {
        return peopleMapper.update(people);
    }

    @Override
    public People login(String username,String password){
        return peopleMapper.login(username, password);
    }


}

8.controller层

package com.example.demo.controller;

import com.example.demo.entity.People;
import com.example.demo.serviceImpl.PeopleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "people控制类")
@RestController
@RequestMapping("people")
public class PeopleController {

    @Autowired
    private PeopleServiceImpl peopleServiceImpl;

    //增加用户
    @ApiOperation("增加一个people对象,用post方法")
    @RequestMapping("add")
    public void save(@RequestBody People people) {
        peopleServiceImpl.save(people);
    }

    //删除用户
    @ApiOperation("删除一个people对象,用get方法")
    @RequestMapping("delete/{id}")
    public int delete(@PathVariable int id) {
        return peopleServiceImpl.delete(id);
    }

    //根据id查找单个用户
    @ApiOperation("根据id查找单个people对象,用get方法")
    @RequestMapping("get/{id}")
    public People findById(@PathVariable int id) {
        return peopleServiceImpl.findById(id);
    }


    //展示所有用户的列表
    @ApiOperation("展示出所有people对象,用get方法")
    @RequestMapping("list")
    public List findAll() {
        return peopleServiceImpl.findAll();
    }

    //修改用户信息              修改的信息中,id是?,修改的就是id为?的人员的信息
    @ApiOperation("修改id对应的一个people对象,用post方法")
    @RequestMapping("update")
    public int update(@RequestBody People people) {
        return peopleServiceImpl.update(people);
    }

    //登录验证
    @ApiOperation("根据账号密码判断登录,用post方法")
    @RequestMapping("login/{username}/{password}")
    public Object login(@PathVariable String username, @PathVariable String password){
        People people= peopleServiceImpl.login(username, password);
        if(people!=null){
            return people;
        }else {
            return "账号或密码错误,请重新输入!!!";
        }
    }

}

9.config层的SwaggerConfig插件

package com.example.demo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


//url:http://localhost:8080/swagger-ui.html

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //配置Swagger信息
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "ZTY",
                "张天钰的Swagger API文档",
                "1.0",
                "https://blog.csdn.net/haiOKba?spm=1001.2014.3001.5343/",
                new Contact("ZTY","https://blog.csdn.net/haiOKba?spm=1001.2014.3001.5343/","[email protected]"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }


    //配置Swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
        Profiles profiles=Profiles.of("dev","test");
        boolean flag=environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("zty")
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

//    @Bean
//    public Docket docket1(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("cf");
//    }
//
//    @Bean
//    public Docket docket2(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("lh");
//    }

}

你可能感兴趣的:(spring,spring,boot,java)