SpringBoot整合Mybatis(配置文件)

一、引入jar


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.3

二、创建JavaBean

public class Employee {
    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;
    private Integer dId;

    public Integer getId() {
        return id;
    }
...

三、创建Mapper接口

public interface EmployeeMapper {
    public Employee getEmpById(Integer id);
    public void insertEmp(Employee employee);
}

四、搭建XML文件配置

1)、结构:可自行定义

2)、yml文件中指定mybatis全局配置文件和接口映射文件位置

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

3)、查看官网复制主配置文件模板修改如下

网址:http://www.mybatis.org/mybatis-3/




    
        
    

4)、查看官网复制接口映射文件模板修改如下




    
    
    
    
        insert  into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{d_id})
    

五、编写接口测试

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

你可能感兴趣的:(java,java,后端)