Springboot+mybatis的CRUD

搭建一套框架出来:

Springboot+Mybatis+Mysql

用的Restful的风格

增删改查对应的是:

增:postmapping

删:deletemapping

改:putmapping

查:getmapping

注意事项:

1.增和改的请求头Content-Type application/json

2.Pathvariable("id") Integer id

============UserController===============

import cn.match.hesuan.entity.User;

import cn.match.hesuan.param.UserParam;

import cn.match.hesuan.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

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

/**

* Created by mac120 on 20-2-29.

*/

@RestController

@RequestMapping("/sys/user")

public class UserController {

@Autowired

    UserServiceuserService;

/**

* 增加用户

*

    * @param user

    * @return

    */

    @PostMapping(value ="/saveUser")

public int saveUser(@RequestBody UserParam param) {

int flag =userService.insertUser(param);

if (flag ==1) {

System.out.println("插入成功");

return 1;

}else {

System.out.println("插入失败");

return 0;

}

}

/**

* 更新用户

*

    * @param user

    * @return

    */

    @PutMapping(value ="/updateUser")

public int updateUser(@RequestBody UserParam userParam) {

int flag =userService.updateUser(userParam);

if (flag ==1) {

System.out.println("更新成功");

return 1;

}else {

System.out.println("更新失败");

return 0;

}

}

/**

*

*/

    @DeleteMapping(value ="/deleteUser/{id}")

public int removeUser(@PathVariable("id")  Integer id) {

int flag =userService.deleteUser(id);

if (flag ==1) {

System.out.println("删除成功");

return 1;

}else {

System.out.println("删除失败");

return 0;

}

}

@GetMapping(value ="/getUser")

public User getUserInfo(int id) {

return userService.selectUserInfo(id);

}

}

===============UserMapper.xml====================


   

INSERT into sys_user(id,user_name,head_url) VALUE (#{id,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{headUrl,jdbcType=VARCHAR})


   

update sys_user

set user_name = #{userName,jdbcType=VARCHAR},

head_url = #{headUrl,jdbcType=VARCHAR}

where id = #{id,jdbcType=INTEGER}


   

delete from sys_user where id = #{id,jdbcType=INTEGER}


   

===============application.yml==================================

spring:

profiles:

    active: dev


==================application-dev.yml=========================

server:

  port: 8080

spring:

datasource:

    username: root

password: root

url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC

driver-class-name: com.mysql.jdbc.Driver

mybatis:

  mapper-locations: classpath:mapping/*Mapper.xml

type-aliases-package: cn.match.hesuan.entity

#showSql

=============

http://localhost:8080/sys/user/updateUser

{"id":"2","userName":"美国","headUrl":"美国1"}

你可能感兴趣的:(Springboot+mybatis的CRUD)