Spring Boot uses XML file to configure MyBatis

1. Create an interface, @ Mapper or @ MapperScan assemble the interface scan into the container

JavaBean:

public class Employee {

    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;
    private Integer dId;

    public void setId(Integer id) {
        this.id = id;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setdId(Integer dId) {
        this.dId = dId;
    }

    public Integer getId() {
        return id;
    }

    public String getLastName() {
        return lastName;
    }

    public Integer getGender() {
        return gender;
    }

    public String getEmail() {
        return email;
    }

    public Integer getdId() {
        return dId;
    }
}
public interface EmployeeMapper {

    public Employee getEmpById(Integer id);

    public void insertEmp(Employee employee);
}

Add:

@MapperScan(value = "com.mydata.springboot.mapper")

2. Create an XML 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>
    <!--Open hump nomenclature rules-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

3. Create an XML SQL mapping file

<?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.mydata.springboot.mapper.EmployeeMapper">

   <!--    public Employee getEmpById(Integer id);
    public void insertEmp(Employee employee);-->

    <select id="getEmpById" resultType="com.mydata.springboot.bean.Employee">
        SELECT * FROM employee WHERE id=#{id}
    </select>

    <insert id="insertEmp">
        INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{dId})
    </insert>
</mapper>

4. To make the created XML file effective, you can configure it in the application.yml configuration file:

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

5. Create a Controller for testing

@RestController
public class DeptController {

    @Autowired
    EmployeeMapper employeeMapper;


    @GetMapping("/emp/{id}")
    public Employee getEmp(@PathVariable("id") Integer id){
       return employeeMapper.getEmpById(id);
    }

}

6. Add test data to the database

7. Start the project input url access: localhost: 8080/emp/1

Display data, so the configuration is successful!

 

 

 

 

Tags: Mybatis xml SpringBoot encoding

Posted on Sun, 10 Nov 2019 09:10:46 -0800 by travelkind