[java]54、SpringBoot整合MyBatis

1、SpringBoot整合MyBatis

1.1、在pom.xml中添加依赖

    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.3


    mysql
    mysql-connector-java


    com.alibaba
    druid-spring-boot-starter
    1.2.1


    org.springframework.boot
    spring-boot-starter-thymeleaf

1.2、在application.yml配置mybatis的数据源
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: shijikl126
    url: jdbc:mysql://localhost:3306/test_mbatis

mybatis的配置

mybatis:
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.sj.domain
1.3、在主函数中添加扫描dao的注解
@SpringBootApplication
@MapperScan("com.sj.dao")
public class Application4 {
    public static void main(String[] args) {
        SpringApplication.run(Application4.class, args);
    }
}
1.4、MyBatis的配置三个方法如下

1、指定配置文件mybatis-config.xml

mybatis:
  config-location: classpath:mybatis-config.xml

    
        
    

2、直接在application.yml中指定

mybatis:
  configuration:
    map-underscore-to-camel-case: true

3、或者在java中实现ConfigurationCustomizer对象设置

@Configuration
public class MyBatisConfig {
    @Bean
    ConfigurationCustomizer customizer() {
        return (cfg)-> {
            cfg.setMapUnderscoreToCamelCase(true);
        };
    }
}

2、starter的命名规范

SpringBoot官方提供的starter: spring-boot-starter-*
自定义的starter(非SpringBoot官方):*-spring-boot-starter

你可能感兴趣的:([java]54、SpringBoot整合MyBatis)