Springboot + mybatis + Vue (II) to implement the interface

Time:2022-1-7

directory structure

Springboot + mybatis + Vue (II) to implement the interface

sql

CREATE TABLE `user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `userName` varchar(100) NOT NULL,
  `userAge` varchar(100) NOT NULL,
  `createTime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

Create entity User

package com.example.demo10sss.entity;

public class User {
    private int id;
    private String userName;
    private int userAge;
    private String userAddress;

    public int getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
}

dao.UserMapper

package com.example.demo10sss.dao;

import com.example.demo10sss.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserMapper {

    @Select("select * from user")
    public List<User> selectUserAll();

    @Select("select * from user where id = #{id}")
    public User selectById(int id);

    @Insert("insert into user(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})")
    public void addUser(User user);

    @Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}")
    public void updateUser(User user);

    @Delete("delete from user where id = #{id}")
    public void deleteUser(int id);
}

controller.UserController

package com.example.demo10sss.controller;

import com.example.demo10sss.dao.UserMapper;
import com.example.demo10sss.entity.User;
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.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping(value="/selectAll",method = RequestMethod.GET)
    public List<User> selectAll(){
        return userMapper.selectUserAll();
    }

    @RequestMapping(value="/selectId",method = RequestMethod.GET)
    public User selectId(String id){
        User user = userMapper.selectById(Integer.parseInt(id));
        return user;
    }

    @RequestMapping(value="/addUser",method = RequestMethod.POST)
    public void addUser(User user){
        userMapper.addUser(user);
    }

    @RequestMapping(value="/updateUser",method = RequestMethod.POST)
    public void updateUser(User user){
        userMapper.updateUser(user);
    }

    @RequestMapping(value="/deleteUser",method = RequestMethod.POST)
    public void deleteId(String id){
        userMapper.deleteUser(Integer.parseInt(id));
    }
}

Cross domain

config/CORSConfiguration

package com.example.demo10sss.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class CORSConfiguration extends WebMvcConfigurerAdapter
{
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                .addMapping("/**")
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowedHeaders("*");
    }
}

Recommended Today

Front end development: webp adaptively improves development performance

Webp introduction WebPIt is a picture format launched by Google that provides both lossy and lossless compression methods. Its advantage is reflected in its excellent image compression algorithm, which can bring smaller picture volume and higher image quality. According to the official instructions, webp can reduce the volume of PNG by 26% in the case […]