Springboot integrates mybatis documents

1. Import and integrate Mybatis dependencies

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
Copy the code

2. Configure the database connection pool information

spring.datasource.username=root
spring.datasource.password=19990802
spring.datasource.url=jdbc:mysql//localhost:3306/mybatis?? useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Copy the code

3. Test whether the database connection pool is connected successfully

@SpringBootTest
class Springboot05MybatisApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads(a) throws SQLException { System.out.println(dataSource.getClass()); System.out.println(dataSource.getConnection()); }}Copy the code

Connection successful:

4, IDEA connect to database and create entity class

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}
Copy the code

5, write mapper interface, UserMapper

@MapperMybatis mapper interface
@Repository
public interface UserMapper {
    List<User> queryUserList(a);

    User queryUserById(@Param("id") int id);

    int addUser(User user);

    int deleteUserById(@Param("id") int id);

    int updateUser(User user);
}
Copy the code

6. Supplement and integrate the configuration of Mybatis in the configuration file

spring.datasource.username=root
spring.datasource.password=19990802
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis? useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Integrate mybatis configuration
mybatis.type-aliases-package=com.cheng.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

Copy the code

7. Compile the mapping file corresponding to mapper, usermapper.xml


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

<mapper namespace="com.cheng.mapper.UserMapper">

    <select id="queryUserList" resultType="User">
        select * from user
    </select>

    <select id="queryUserById" parameterType="int" resultType="User">
        select * from user where id=#{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into user(name,pwd,id)
        values (#{name},#{pwd},#{id})
    </insert>

    <delete id="deleteUserById" parameterType="int">
        delete from user where id=#{id}
    </delete>

    <update id="updateUser" parameterType="User">
        update user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>
</mapper>
Copy the code

8. Write UserCortroller test (do not write service layer, directly call dao layer)

package com.cheng.controller;

import com.cheng.mapper.UserMapper;
import com.cheng.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryList")
    public List<User> queryUserList(a){
        return userMapper.queryUserList();
    }
    @GetMapping("/queryUserById/{id}")
    public User queryUserById(@PathVariable("id") int id){
        return userMapper.queryUserById(id);
    }
    @GetMapping("/addUser")
    public String addUser(a){
        userMapper.addUser(new User(1."Thousands of miles"."123456"));
        return "ok";
    }
    @GetMapping("/deleteUserById/{id}")
    public String deleteUserById(@PathVariable("id") int id){
        userMapper.deleteUserById(id);
        return "ok";
    }

    @GetMapping("/updateUser")
    public String updateUser(a){
        userMapper.updateUser(new User(3."Gods"."123456"));
        return "ok"; }}Copy the code

Test successful!