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

本文档展示了如何在Spring Boot项目中配置MyBatis进行数据库操作,包括数据源配置、Mapper接口及XML映射文件、Entity实体类、Service及ServiceImpl实现、Controller层接口实现,并通过Swagger2生成API文档。详细步骤从数据库连接到业务逻辑实现,再到API的可视化展示,为Spring Boot应用的开发提供了清晰的示例。
摘要由CSDN通过智能技术生成

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<People> findAll();

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

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

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

<?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="com.example.demo.mapper.PeopleMapper">

    <!--  添加   -->
    <insert id="save" parameterType="com.example.demo.entity.People">
        INSERT INTO people (id,name,age,sex,tel,wage,username,password) values (null,#{name},#{age},#{sex},#{tel},#{wage},#{username},#{password})
    </insert>

    <!--  删除  -->
    <delete id="delete" parameterType="int">
        DELETE FROM people WHERE id=#{id}
    </delete>

    <!--  查找单个  -->
    <select id="findById" parameterType="int" resultType="People">
        SELECT * FROM people WHERE id=#{id}
    </select>
    <!--  显示全部管理员的信息  -->
    <select id="findAll" resultType="People">
        SELECT * FROM people
    </select>


    <!--  修改  -->
    <update id="update" parameterType="People">
        UPDATE people SET name =#{name},age=#{age},sex=#{sex},tel=#{tel},wage=#{wage}, username=#{username},password=#{password} WHERE id=#{id}
    </update>
    
    <!--  登录  -->
    <select id="login" parameterType="String" resultType="People">
        select  * from  people where username=#{username} and password=#{password}
    </select>


</mapper>

 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<People> 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<People> 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<People> 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/","2219827537@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }


    //配置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");
//    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值