Bootstrap

Springboot+Mybatis实现最简单的增删改查

1.创建一个SpringBoot项目

 2.导入依赖

勾选上web下面的SpringWeb和sql下面的MySQL和Mybatis 依赖  然后等待依赖下载导入

2.1 依赖导入后的页面

3.创建需要的包和Mybatis配置文件 
3.1在原来的com.mybatis包下创建了entity,controller,service,mapper四个包
3.2在resource下创建了mybatis-config.xml为Mybatis核心配置文件
3.3还在resource下创建了一个包com.mybatis.mapper   (注意)

resource下创建包的格式  为 com/mybatis/mapper  

3.4将application.properties改为application.yml  后缀名改为yml

4.数据库的创建和连接
4.1数据库SQL语句

先创建名为mybatis的数据库然后执行以下sql语句

/*
 Navicat Premium Data Transfer

 Source Server         : localhost_3306
 Source Server Type    : MySQL
 Source Server Version : 80033
 Source Host           : localhost:3306
 Source Schema         : mybatis

 Target Server Type    : MySQL
 Target Server Version : 80033
 File Encoding         : 65001

 Date: 10/08/2024 11:15:28
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '李四', '123456', '17896325478');
INSERT INTO `user` VALUES (2, 'username', 'password', '17896325478');
INSERT INTO `user` VALUES (3, 'username', 'password', '17896325478');

SET FOREIGN_KEY_CHECKS = 1;
4.2 application.yml的配置
#设置端口号
server:
  port: 8282
#设置数据配置信息
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis
    username: root
    password: 123456

4.3mybatis-config,xml的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!--将表中下划线的字段自动转成驼峰命名-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <!--给所有返回结果起别名-->
        <package name="com.mybatis.entity"/>
    </typeAliases>
    <mappers>
        <!--xml需和mapper接口同样的目录-->
        <package name="com.mybatis.mapper"/>
    </mappers>
</configuration>
5.实体层,controller层,service层和mapper层的编写
5.1实体层(entity)

根据数据库字段创建对应的属性

package com.mybatis.entity;

public class User {
    private   Integer id;
    private   String username;
    private   String password;
    private   String phone;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    public User() {
    }

    public User(Integer id, String username, String password, String phone) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.phone = phone;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
5.2mapper层(接口)
package com.mybatis.mapper;

import com.mybatis.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface UserMapper {
    /*
    * 查询所有用户
    * */
    List<User> getAllUser();
    /*
    * 添加用户
    * */
    Integer addUser(User user);
    /*
    * 更新用户信息
    * */
    Integer updateUser(User user);
    /*
    * 删除用户信息
    * */
    Integer deleteUser(Integer id);

}
5.3 service层
package com.mybatis.sevice;

import com.mybatis.entity.User;
import com.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List<User> getAllUser(){
        return userMapper.getAllUser();
    }
    public Integer addUser(User user){
        return userMapper.addUser(user);
    }
    public Integer updateUser(User user){
        return userMapper.updateUser(user);
    }
    public Integer deleteUser(Integer id){
        return userMapper.deleteUser(id);
    }
}
5.4controller层
package com.mybatis.controller;

import com.mybatis.entity.User;
import com.mybatis.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    private UserService userService;

    @GetMapping("/all")
    public List<User> getAllUser(){
        return userService.getAllUser();
    }
    @PostMapping("/add")
    public Integer addUser(@RequestBody User user){
        return userService.addUser(user);
    }
    @PutMapping("/update")
    public Integer updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
    @DeleteMapping("/delete/{id}")
    public Integer deleteUser(@PathVariable("id") Integer id){
        return userService.deleteUser(id);
    }
}
5.5UserMapper.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="com.mybatis.mapper.UserMapper">


   <!-- List<User> getAllUser();-->
  <select id="getAllUser" resultType="com.mybatis.entity.User">
      select * from user
  </select>
   <!-- Integer addUser(User user);-->
  <insert id="addUser" parameterType="com.mybatis.entity.User">
      insert into user (username,password,phone)
      values (#{username},#{password},#{phone})
  </insert>

<!--    Integer updateUser(User user);-->
  <update id="updateUser" parameterType="com.mybatis.entity.User">
      update user set username = #{username},password =#{password},phone=#{phone}
      where id = #{id}
  </update>
<!--    Integer deleteUser(Integer id);-->
   <delete id="deleteUser" parameterType="java.lang.Integer">
       delete from user where id =#{id}
   </delete>
</mapper>
5.6整体页面 

6.测试接口

发现都能正常使用

;