SpringBoot和Mybatis整合二(基于配置文件)

ps:可先看SpringBoot和Mybatis整合一(基于注解),再看整合二

项目结构:

SpringBoot和Mybatis整合二(基于配置文件)_第1张图片

EmployeeMapper:

package com.lucifer.springboot.mapper;

import com.lucifer.springboot.bean.Employee;

public interface EmployeeMapper {
    public Employee getEmployee(Integer id);

    public void insertEmp(Employee employee);
}

EmployeeMapper.xml:    mapper映射文件 




    

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

mybatis-config.xml: mybatis配置文件,mapUnderscoreToCamelCase设置为true,就可以开启驼峰命名规则。

参考:mybatis官方文档




    
        
    

application.yml: 增加两行配置 

SpringBoot和Mybatis整合二(基于配置文件)_第2张图片

EmployeeController:

package com.lucifer.springboot.controller;

import com.lucifer.springboot.bean.Employee;
import com.lucifer.springboot.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Lucifer
 * @create: 2018-09-20 15:00
 * @description:
 **/
@RestController
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;

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

}

浏览器输入路径:

SpringBoot和Mybatis整合二(基于配置文件)_第3张图片

ps:如果did对应的value是null的话,看数据库中表中该字段数据是否为空,再看是否开启了驼峰命名规则。

 

附:springboot整合mybatis代码

你可能感兴趣的:(SpringBoot技术篇)