SpringBoot – mybatis integration

1. Create projects

Check the Web module and JDBC and mysql modules

2. Import the MyBatis integration package

<! -- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> < the groupId > org. Mybatis. Spring. The boot < / groupId > < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.1.4 < / version >  </dependency>Copy the code

3. Project preparation and configuration

Database Connection Configuration

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
Copy the code

4. Create a project structure

Create poJO, Mapper package

Entity class: User (importing Lombook)

package com.gip.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
}

Copy the code

The tables in the database are shown in figure

5. Write interfaces

Define a method to query all users

package com.gip.mapper;

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

import java.util.List;
@Mapper
@Repository
public interface UserMapper {
    public List<User> selectAllUser(a);
}

Copy the code

@mapper indicates that this class is a Mapper, which is the annotation in myBatis integration package, myBatis will scan it and configure it accordingly

You can also scan mapper with annotations in the main entry of the program

@MapperScan("com.gip.mapper")
Copy the code

Registered in SpringBoot, @repository is scanned, which is equivalent to putting the bean instance into Spring’s IOC container for registration and use

Create directory mybatis under Resources to store mapper configuration (mapping) files

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

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

    <select id="selectAllUser" resultType="user">
        select * from mybatis.user
    </select>

</mapper>
Copy the code

Test 6.

And a controller

package com.gip.controller;

import com.gip.mapper.UserMapper;
import com.gip.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/getAll")
    public List<User> getAll(a){
        List<User> users = userMapper.selectAllUser();
        returnusers; }}Copy the code

Autowired Automatically assembles mapper

Add the mybatis configuration to the configuration file

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

Classpath: Write the path correctly without a slash

The slash is the project root path

Browser access, test successful!