springboot+mybatis+vue(二),实现接口

目录结构

springboot+mybatis+vue(二),实现接口_第1张图片

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;

创建实体 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 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 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));
    }
}

跨域

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("*");
    }
}

你可能感兴趣的:(springboot+mybatis+vue(二),实现接口)