Mybatis spring boot starter configuration

  springboot

maven

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

Yml configuration

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/demo
    driverClassName: org.postgresql.Driver
    username: postgres
    password: postgres
    validation-query: SELECT 1
    test-while-idle: true
    test-on-borrow: true
mybatis:
    config-locations: classpath:mybatis/mybatis-config.xml
    mapper-locations: classpath:mybatis/mapper/*.xml
    configuration:
        map-underscore-to-camel-case: true
        default-fetch-size: 50
        default-statement-timeout: 10
        auto-mapping-unknown-column-behavior: WARNING

Configuration item
mybatis-spring-boot-autoconfigure-1.2.1-sources.jar! /org/mybatis/spring/boot/autoconfigure/MybatisProperties.java

  • configLocation

Xml location

  • mapperLocations

Specifies the location of mapper’s xml

  • typeAliasesPackage

Packages that specify aliases can be multiple, separated by commas

  • typeHandlersPackage

Specifies the handler’s scan code path

  • configuration

Nested configurations are detailed in myatis-3.4.4-sources.jar! /org/apache/ibatis/session/Configuration.java

- defaultStatementTimeout
设置超时时间,它决定驱动等待数据库响应的秒数。
- defaultFetchSize
为驱动的结果集获取数量(fetchSize)设置一个提示值。此参数只可以在查询设置中被覆盖。
- mapUnderscoreToCamelCase
是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。
- autoMappingUnknownColumnBehavior
指定发现自动映射目标未知列(或者未知属性类型)的行为。
    - NONE: 不做任何反应
    - WARNING: 输出提醒日志 ('org.apache.ibatis.session.AutoMappingUnknownColumnBehavior' 的日志等级必须设置为 WARN)
    - FAILING: 映射失败 (抛出 SqlSessionException)

Java configuration

@MapperScan("com.codecraft.dao")
@Configuration
public class MybatisConfig {
}

doc