基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)

本篇介绍基于SpringBoot和Mybatis+mysql+maven的简单实现增删改查的案例,详细介绍如下,

代码地址附上:https://git.lug.ustc.edu.cn/nantian_javaBackground/springboot_mybatis/tree/SpringBoot_Mybatis

1---项目包介绍:

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第1张图片

2---pom文件依赖设计:

      



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    com.sang
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            com.alibaba
            druid
            1.0.11
        

        
            org.springframework.boot
            spring-boot-starter-jdbc
        

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

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        

        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

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


2---数据库相关配置文件application.properties设计:

#mysql数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/taotao?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml

3---数据库设计,使用mysql数据库创建的简单数据表  用来测试数据:

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第2张图片

     基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第3张图片

4---实体类设计:User类

package com.sang.entity;

/**
 * Created by 
 * on 2019/11/13 16:56
 */
public class User {
    private int id;
    private String name;
    private String password;
    private String number;

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", number='" + number + '\'' +
                '}';
    }

    public User(int id, String name, String password, String number) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.number = number;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}

   5---service层设计:UserService类:

package com.sang.service;

import com.sang.entity.User;
import com.sang.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 
 * on 2019/11/13 17:04
 */
@Service
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 通过用心姓名查找用户
     * @param name
     * @return
     */
    public List findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    /**
     * 通过用户信息插入用户   并返回插入的用户
     * @param user
     * @return
     */
    public User insertUser(User user){
        userMapper.insertUser(user);
        return user;
    }


    /**
     * 查询所有的用户
     * @return
     */
    public List ListUser(){
        return userMapper.ListUser();
    }

    /**
     * 借助用户信息更新用户表
     * @param user
     * @return
     */
    public int Update(User user){
        return userMapper.Update(user);
    }

    /**
     * 借助id删除用户信息
     * @param id
     * @return
     */
    public int delete(int id){
        return userMapper.delete(id);
    }

}

6---mapper接口设计:UserMapper接口:

package com.sang.mapper;

import com.sang.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by 
 * on 2019/11/13 16:59
 */
@Mapper
public interface UserMapper {

    //通过姓名查找用户
    List findUserByName(String name);

    //查询所有的用户信息
    public List ListUser();

    //插入一条用户信心  返回数字表示是否成功
    public int insertUser(User user);

    //通过id删除用户信息
    public int delete(int id);

    //更新用户信息
    public int Update(User user);

}

     7---controller层设计:CRUD类:

package com.sang.controller;

import com.sang.entity.User;
import com.sang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 
 * on 2019/11/13 17:18
 */
@RestController
@RequestMapping(value = "/CRUD",method = {RequestMethod.GET,RequestMethod.POST})
public class CRUD {

    @Autowired
    private UserService userService;

    /**
     * 通过员工id 删除员工------------GET----------------通过
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(int id){
        int result = userService.delete(id);
        if(result>=1){
            return "删除成功";
        }else{
            return "删除失败!";
        }
    }

    /**
     * 通过员工信息更新员工________________POST-----------通过
     * @param user
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(User user){
        int result=userService.Update(user);
        if(result>=1){
            return "修改成功!";
        }else{
            return "修改失败!";
        }
    }

    /**
     * 通过员工信息插入员工---------------POST----------------通过
     * @param user
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public User insert(User user) {
        return userService.insertUser(user);
    }

    /**
     * 返回用户的所有信息----------------------------------通过
     * @return
     */
    @RequestMapping("/ListUser")
    @ResponseBody
    public List ListUser(){
        return userService.ListUser();
    }

    /**
     * 通过员工姓名查找员工
     * @param name
     * @return
     */
    @RequestMapping("/ListUserByname")
    @ResponseBody
    public List ListUserByname(String name){
        return userService.findUserByName(name);
    }

}

8---资源包中UserMapper.xml文件设计:




    
        
        
        
    

    

    

    
		INSERT INTO user
		(
		id,name,password,number
		)
		VALUES (
		#{id},
		#{name, jdbcType=VARCHAR},
		#{password, jdbcType=VARCHAR},
		#{number}
		)
	

    
		delete from user where id=#{id}
	

    
	update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}
	

 

9---SpringBoot启动类设计:DemoApplication类:

package com.sang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

10--postman测试:

查询所有:http://localhost:8080/CRUD/ListUser

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第4张图片

11----删除员工:借助id删除员工

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第5张图片

12---插入员工

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第6张图片

13:---通过姓名查找员工用户信息:

本地测得:

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第7张图片

postman测得:

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第8张图片

14---修改员工:借助员工id信息锁定员工  在进行对应信息的修改;

基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)_第9张图片

 

 

你可能感兴趣的:(CRUD,SpringBoot)