The early stage of the work

  1. The importMybatis integrationRely on
        <! Mybatis -->
        <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
Copy the code
  1. Connecting to a Database
  2. As soon as you connect to the databaseapplicaton.ymlConfigure the database
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
Copy the code

To integrate

  1. Write entity classes that correspond to the database
package com.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String pwd;

}
Copy the code

Lombok was imported to be lazy

        <! -- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
Copy the code
  1. Write the mapper
package com.example.mapper;

import com.example.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    // Query all user information
    List<User> getUserList(a);

    //select the user whose id=1
    User getUserById(int id);

    //insert adds a user
    int insertUser(User user);

    //delete Delete the user whose ID is 4
    int deleteUser(int id);

    //update user name with id=2
    int updateUser(User user);

}

Copy the code
  1. writemapper.xmlfile

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.mapper.UserMapper">
    <select id="getUserList" resultType="user">
        select *
        from mybatis.user
    </select>

    <select id="getUserById" resultType="user" parameterType="int">
        select *
        from mybatis.user
        where id = #{id};
    </select>

    <insert id="insertUser" parameterType="user">
        insert into mybatis.user (id, name, pwd)
        values (#{id}, #{name}, #{pwd});
    </insert>

    <delete id="deleteUser" parameterType="int">
        delete
        from mybatis.user
        where id = #{id};
    </delete>

    <update id="updateUser" parameterType="user">
        update mybatis.user
        set name = #{name},
            pwd = #{pwd}
        where id = #{id};
    </update>

</mapper>
Copy the code

We’re using an alias here and we put the mapper. XML file in the Resources directory, so we’ll go to application.yml to configure it

mybatis:
  type-aliases-package: com.example.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml
Copy the code

Mapper.xml file is written in:

  1. Writing the controller
package com.example.controller;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/getUserList")
    public List<User> getUserList(a){
        return userMapper.getUserList();
    }

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") int id){
        return userMapper.getUserById(id);
    }

    @GetMapping("/insertUser")
    public String insertUser(a){
        userMapper.insertUser(new User(5."xiaoming"."111"));
        return "ok";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(a){
        userMapper.deleteUser(5);
        return "ok";
    }

    @GetMapping("/updateUser")
    public String updateUser(a){
        userMapper.updateUser(new User(5."xx"."111"));
        return "ok"; }}Copy the code
  1. test