SpringBoot_数据访问-整合MyBatis-配置版MyBatis

Posted 猴子特种兵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot_数据访问-整合MyBatis-配置版MyBatis相关的知识,希望对你有一定的参考价值。

1.添加配置文件
    mybatis-config.xml
    
    <?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>


    EmployeeMapper.xml
    
    <?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.baoxing.springboot.mapper.EmployeeMapper">
  <!--
       public Employee getEmpById(Integer id);

       public void insertEmp(Employee employee);
  -->
        <select id="getEmpById" resultType="com.baoxing.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>
application.yml配置文件 主要看mybatis对应的配置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
#    schema:
#      - classpath:sql/department.sql
#      - classpath:sql/employee.sql

#   数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,\'wall\'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
EmployeeMapper类

@Mapper
public interface EmployeeMapper {

    public Employee getEmpById(Integer id);

    public void insertEmp(Employee employee);
}
Controller类
    
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping(value = "/dept/{id}")
    public Department getDepartment(@PathVariable("id") Integer id){

        return departmentMapper.getDeptById(id);
    }

    @GetMapping(value = "/dept")
    public Department insertDept(Department department){

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

}

 

以上是关于SpringBoot_数据访问-整合MyBatis-配置版MyBatis的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot数据访问 SpringBoot整合Mybatis

SpringBoot:4.SpringBoot整合Mybatis实现数据库访问

SpringBoot整合Mybatis访问数据库和阿里巴巴数据源

SpringBoot整合MyBatis

SpringBoot:Mybatis + Druid 数据访问

kotlin + springboot整合mybatis操作mysql数据库及单元测试