1. The background

Integrate Mybatis in the Springboot project. Official introduction: mybatis.org/spring-boot…

2. Knowledge

2.1 Creating a Project

Step:1 Using the IDEA editor, click Create New Project and select Spring Initializr

  • Note: Spring Initializr is an official Spring helper method for quickly creating Spring projects.

Step:2

Step:3

Here four libraries are applied to create the project:

  • MyBatis Framework: MyBatis library
  • MySQL Driver: the MySQL database Driver
  • Spring Web: Build Web applications to handle requests
  • Lombok: A powerful helper class library

2.2 configure mybatis

Springboot uses application.propery or application.yml to configure applications. I’m using application.yml here, and you can create one.

server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/springboot_mybatis? useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: Classpath :mapper/* mapper. XML type-aliases-package: com.example.entity # Prints logs. Mapper logging: level: cn: zyfvir: demo: debugCopy the code
  • Spring: datasource configuration is the configuration of the datasource of your database, where the database connection string, username and password are configured.
  • mapper-locations: Mapper. XML is the key sentence, it describes how to find the XML mapping file of Myabatis. This sentence means to find the file ending in mapper. XML under the mapper folder of resource.

2.3 Writing Code

Mybaits supports two types of mapping:

  • The XML file describes the mapping
  • Annotations map

You can mix them in real projects, and I’ve done examples here.

Take a look at my code structure:

Start coding

Let’s write an entity class

public class City {

    private Long id;
    private String name;
    private String state;
    private String country;
}
Copy the code

Mapper mapping class

@Mapper
public interface CityMapper {

    @Select("SELECT * FROM city WHERE status = #{status}")
    City findByStatus(@Param("status") int status);

    List<City> findAll();
}
Copy the code

Write the service class

public interface CityService { City findByStatus(int status); List<City> findAll(); @service public class CityServiceImpl implements CityService {@autoWired CityMapper CityMapper; @Override public City findByStatus(int status) { return cityMapper.findByStatus(status); } @Override public List<City> findAll() { return cityMapper.findAll(); }}Copy the code

The controller class

@RestController @RequestMapping("/city") public class CityController { @Autowired CityService cityService; @RequestMapping("/findByStatus") public City findByStatus(int status){ return cityService.findByStatus(status); } @RequestMapping("/findAll") public List<City> findAll(){ return cityService.findAll(); }}Copy the code

2.4 Demo access address

Open this address in a browser: http://localhost:8080/city/findAll http://localhost:8080/city/findByStatus? status=1

3. The appendix

My construction sentence:

CREATE TABLE city ( id INT PRIMARY KEY auto_increment, name VARCHAR(100) NULL, status INT NOT NULL, Country VARCHAR(100) NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'InnoDB '; INSERT INTO city (name,STATUS) VALUES (' Beijing ',0); INSERT INTO city (name,STATUS) VALUES (' Shanghai ',0); INSERT INTO city (name,STATUS) VALUES (' hebei ',1);Copy the code

Example of my code: github.com/vir56k/java…

5. Reference:

MyBatis spring-boot-starter will help you use MyBatis in Spring Boot github.com/mybatis/spr…

Mybatis.org/spring-boot…

Github.com/mybatis/spr…