美文网首页
Mybatis-spring-boot整合

Mybatis-spring-boot整合

作者: 左洁 | 来源:发表于2019-10-21 17:01 被阅读0次

    1.环境

    • jdk8
    • mybatis-spring-boot-starter 2.1.0
    • springboot 2.2.1.BUILD-SNAPSHOT

    2.核心类分析

    分析之前,建议查看下面链接 ,有利于理解本文内容

    MybatisAutoConfiguration:springboot启动自动加载配置,配置中初始化SqlSessionFactory

    SqlSessionTemplateMapperFactoryBean

    下面是流程图

    mybatis-spring-boot-1.png

    3.示例代码

    3.1.工程结构图

    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── zzm
    │   │   │           ├── ArtsTenMybatisSpringBootApplication.java
    │   │   │           ├── config
    │   │   │           │   └── MybatisInterceptorConfiguration.java
    │   │   │           ├── dao
    │   │   │           │   └── BlogMapper.java
    │   │   │           ├── entity
    │   │   │           │   ├── Blog.java
    │   │   │           │   ├── CompanyDto.java
    │   │   │           │   └── UserTest.java
    │   │   │           └── intercept
    │   │   │               └── ExamplePlugin.java
    │   │   └── resources
    │   │       ├── application.yml
    │   │       ├── mapper
    │   │       │   └── BlogMapper.xml
    │   │       ├── static
    │   │       └── templates
    

    3.2.pom依赖

    ……………………
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
       <!-- mybatis springboot 集成依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
         <!--mysql依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
      …………………………
    

    3.3. MybatisInterceptorConfiguration自定配置mybatis属性

    • Interceptor插件
    • TypeHandler类型转换器
    • LanguageDriver
    • DatabaseIdProvider 数据库提供商配置
    @Configuration
    @EnableAutoConfiguration
    public class MybatisInterceptorConfiguration {
    
        @Bean
        public ExamplePlugin myInterceptor() {
            return new ExamplePlugin();
        }
    
    }
    

    3.4.application.yml配置

    server:
      port: 8082
    
    management:
      endpoints:
        web:
          exposure:
            exclude: '*'
    
    spring:
      datasource:
        password: 123456
        username: root
        url: jdbc:mysql://localhost:3306/test
    <!-- sql打印日志级别--->
    logging:
      level:
        root: info
        com:
          zzm:
            dao: trace
    <!-- mybatis扫描xml配置路径-->
    mybatis:
      mapper-locations: classpath*:/mapper/*.xml
    

    4. Configuration

    它是mybatis-springboot配置mybatis中属性,例如 mapper-locationstype-handlers-package

    代码地址

    参考链接

    相关文章

      网友评论

          本文标题:Mybatis-spring-boot整合

          本文链接:https://www.haomeiwen.com/subject/ajatvctx.html