SpringBoot data access -- integrating MyBatis

catalogue

1. MyBatis configuration

1.1. Import the official starter of mybatis

1.2. myBatis automatically introduces dependency

1.3. MyBatis configuration file

1.3.1 annotation mode: directly in the springboot configuration file application Configure in yaml

1.3.2 configuration mode: create MyBatis global configuration file for configuration

2. Use of MyBatis

2.1 configuration mode

2.1.1. Write Mapper interface. Annotation @ Mapper

2.1.2. Write sql mapping file and bind mapper interface

2.1.3. In application Specify the location of the Mapper configuration file in yaml

2.1.4. mapper method call

2.2 annotation mode

2.2.1. Write Mapper interface and mark @ Mapper annotation

2.2.2. Directly add annotations on the mapper interface to represent the sql statements and parameter attributes of the interface

2.2.3. mapper method call is the same as that in section 2.1.4

2.3 small problems in the use of MyBatis

2.3.1. The database variable is named with underline, and the corresponding attribute name of java is named with hump

1. MyBatis configuration

1.1. Import the official starter of mybatis

Method 1: manual import

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

Method 2: when creating a project, select Mybatis Framework

1.2. myBatis automatically introduces dependency

1.3. MyBatis configuration file

1.3.1 annotation mode: directly in the springboot configuration file application Configure in yaml

# Configure mybatis rules
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml  #sql mapping file location

  configuration:
    # Variable name underline to hump
    map-underscore-to-camel-case: true

1.3.2 configuration mode: create MyBatis global configuration file for configuration

1) In application Declare global profile location in yaml

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #Global profile location
  mapper-locations: classpath:mybatis/mapper/*.xml  #sql mapping file location

2) Create the MyBatis global configuration file MyBatis config XML and configure it

<?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>
        <!-- Variable name underline to hump-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

</configuration>

2. Use of mytis

Object city used by database crud:

package com.uclass.thymeleaf.springbootthymeleaf.bean;

import lombok.Data;

@Data
public class City {
    private Long id;
    private String name;
    private String state;
    private String country;
}

2.1 configuration mode

2.1.1. Write Mapper interface. Annotation @Mapper

@Mapper
public interface CityMapper {

    public City getById(Long id);

    public void insertCity(City city);
}

2.1.2. Write sql mapping file and bind mapper interface

Bind mapper interface:

  • <mapper namespace="com.uclass.thymeleaf.springbootthymeleaf.mapper.CityMapper">
  • The namespace is the full path representation of the mapper interface
<?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.uclass.thymeleaf.springbootthymeleaf.mapper.CityMapper">
    <!-- public void insertCity(City city);-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into city(`name` ,`state`, `country`) values (#{name}, #{state}, #{country})
    </insert>

</mapper>

2.1.3. In application Specify the location of the Mapper configuration file in yaml

sql mapping file location: mapper locations: classpath: mybatis / mapper / * xml  

# Configure mybatis rules
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml  #sql mapping file location
  configuration:
    map-underscore-to-camel-case: true

2.1.4. mapper method call

1) Write the service layer code and directly call the mapper interface

@Service
public class CityService {

    @Autowired
    CityMapper cityMapper;

    public City getById(Long id) {
        return cityMapper.getById(id);
    }

    public void saveCity(City city) {
        cityMapper.insertCity(city);
    }
}

2) The controller layer receives the mapping, calls the service layer method, and returns the operation result

@Slf4j
@Controller
public class IndexController {

    @Autowired
    CityService cityService;

    @PostMapping("/city")
    public City saveCity(City city) {
        cityService.saveCity(city);
        return city;
    }

    @ResponseBody
    @GetMapping("/city")
    public City getCityById(@RequestParam("id") Long id){
        return cityService.getById(id);
    }
}

2.2 annotation mode

2.2.1. Write Mapper interface and mark @ Mapper annotation

@Mapper
public interface CityMapper {

    public City getById(Long id);

    public void insertCity(City city);
}

2.2.2. Directly add annotations on the mapper interface to represent the sql statements and parameter attributes of the interface

@Mapper
public interface CityMapper {

    @Select("select * from city where id = #{id}")
    public City getById(Long id);

    @Insert("insert into city(`name` ,`state`, `country`) values (#{name}, #{state}, #{country})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insertCity(City city);
}

2.2.3. mapper method call is the same as that in section 2.1.4

2.3 small problems in the use of MyBatis

2.3.1. The database variable is named with underline, and the corresponding attribute name of java is named with hump

1) mybatis does not enable the hump naming policy by default

When querying directly, some fields will be empty

2) Configure Mybatis and enable hump naming policy

Note: only one of the two configuration methods can be selected, otherwise it will conflict

Method 1: open in the global configuration file:

<?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>

</configuration>

Method 2: in the springboot configuration file application Open in yaml

# Configure mybatis rules
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #Global profile location
  mapper-locations: classpath:mybatis/mapper/*.xml  #sql mapping file location
  configuration:
    map-underscore-to-camel-case: true

Final query result:

Tags: Java Database SQL Spring Boot

Posted by robin01 on Mon, 18 Apr 2022 20:56:13 +0930