美文网首页
Springboot+mybatis的CRUD

Springboot+mybatis的CRUD

作者: lirensoso | 来源:发表于2020-03-02 00:08 被阅读0次

搭建一套框架出来:

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====================

<?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="cn.match.hesuan.mapper.UserMapper">

<resultMap id="UserResultMap" type="cn.match.hesuan.entity.User">

<result column="id" jdbcType="INTEGER" property="id" />

<result column="user_name" jdbcType="VARCHAR" property="userName" />

<result column="head_url" jdbcType="VARCHAR" property="headUrl" />

</resultMap>

    <insert id="insertUser" parameterType="cn.match.hesuan.entity.User" >

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

</insert>

    <update id="updateUser" parameterType="cn.match.hesuan.entity.User">

update sys_user

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

head_url = #{headUrl,jdbcType=VARCHAR}

where id = #{id,jdbcType=INTEGER}

</update>

    <delete id="deleteUser" parameterType="java.lang.Integer">

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

</delete>

    <select id="selectUserInfo" parameterType="INTEGER" resultMap="UserResultMap">

select* from sys_user WHERE

1=1

and id = #{id,jdbcType=INTEGER}

</select>

</mapper>

===============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

      本文链接:https://www.haomeiwen.com/subject/bovdkhtx.html