SpringBoot+MybatisPlus+Mysql之增删查改

0 前言

上一篇对于SpringBoot+MybatisPlus+Mysql的简单应用做了说明,这一篇将使用MybatisPlus并在网页端进行显示

1 代码

项目结构如下图所示:

SpringBoot+MybatisPlus+Mysql之增删查改_第1张图片

pom.xml文件如下:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.example
	sbtMybatisP
	0.0.1-SNAPSHOT
	sbtMybatisP
	Demo project for Spring Boot

	
		11
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		com.baomidou
		mybatis-plus-boot-starter
		3.1.0
		
	

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


实例entity下T_user.java代码如下:

package com.example.sbtMybatisP.entity;

import lombok.Data;

@Data
public class T_user {
    private Integer id;
    private String name;
    private String sex;
    private String score;
    private Integer age;
}

mapper文件为:

package com.example.sbtMybatisP.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sbtMybatisP.entity.T_user;

public interface UserMapper extends BaseMapper{
}

主启动程序为:

package com.example.sbtMybatisP;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.sbtMybatisP.mapper")
public class SbtMybatisPApplication {

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

}

controller层代码为:

注解RequestMapping中即为URL

package com.example.sbtMybatisP.controller;

import org.springframework.web.bind.annotation.*;

import com.example.sbtMybatisP.mapper.UserMapper;
import com.example.sbtMybatisP.entity.T_user;

import javax.annotation.Resource;
import java.util.List;
/**
*@Autor:cll
*@CreateDate:2019/3/25 16:23
*@version:1.0
*@Description:controller file,curd
*/
@RestController
public class UserController {
    @Resource
    private UserMapper um;

    //Increase one record
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    public int addRecord(T_user ur){
        //
        //T_user nUser = new T_user(5, "er", "nan", 190, 90);
        //T_user nUser = new T_user();
        //nUser.setName("ll");
        return um.insert(ur);

    }
    //Delete
    @RequestMapping(value = "/delete/id/{id}", method = RequestMethod.DELETE)
    public int deleteById(@PathVariable("id") Integer id){
        //
        return um.deleteById(id);
    }
    //updateByid
    @RequestMapping(value = "/update", method=RequestMethod.PUT)
    public int updateById(T_user ur){
        return um.updateById(ur);
    }
    //selectList:Query All
    @RequestMapping("/select")
    public List selectAll(){
        //
        List user = um.selectList(null);
        for(int i = 0; i < user.size(); i++){
            System.out.println(user.get(i));
            System.out.println(user.get(i).getClass());
            System.out.println("==");
        }
        //System.out.println(user);
        return user;
    }
}

2 测试

get:查询

post:新增

put:修改

delete:删除

启动主程序,打开postman,输入URL,为:http://localhost:8080/select

得到如下结果,查询成功。

SpringBoot+MybatisPlus+Mysql之增删查改_第2张图片

新增一条记录如下:

SpringBoot+MybatisPlus+Mysql之增删查改_第3张图片

删除

SpringBoot+MybatisPlus+Mysql之增删查改_第4张图片

修改一条记录:

SpringBoot+MybatisPlus+Mysql之增删查改_第5张图片

3 总结

本博客的增删查改利用的mybatisplus的接口,并显示在网页上

代码已上传https://github.com/chengleilei/SpringBootRoad

你可能感兴趣的:(Springboot)