美文网首页
springboot整合mybatis

springboot整合mybatis

作者: 泥煤的嘎嘎 | 来源:发表于2019-03-31 19:01 被阅读0次

    SpringBoot2.x持久化数据方式介绍

    1. 原始java访问数据库
    # 开发流程麻烦
    1、注册驱动/加载驱动
        Class.forName("com.mysql.jdbc.Driver")
    2、建立连接
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root","root");
    3、创建Statement
    
    4、执行SQL语句
    
    5、处理结果集
    
    6、关闭连接,释放资源
    
    1. apache dbutils框架 官网
    2. jpa框架(spring-data-jpa) jpa在==复杂查询的时候性能不是很好==
    3. Hiberante(==ORM:对象关系映射Object Relational Mapping==) 企业大都喜欢使用hibernate
    4. Mybatis框架 互联网行业通常使用mybatis,不提供对象和关系模型的直接映射,半ORM

    springboot整合mybatis

    • 引入依赖
        <!-- 引入mybatis starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
    
        <!-- MySQL的JDBC驱动包  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    
        <!-- 数据源 druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
    
    • 添加配置文件(druid.properties)
    # 新建druid.properties
    spring.datasource.druid.driverClassName=com.mysql.jdbc.Driver
    # ?后面解决异常 根据自己情况添加
    spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
    spring.datasource.druid.username=root
    spring.datasource.druid.password=*******
    
    spring.datasource.druid.initial-size=1
    spring.datasource.druid.max-active=20
    spring.datasource.druid.min-idle=3
    spring.datasource.druid.max-wait=60000
    spring.datasource.druid.time-between-eviction-runs-millis=60000
    spring.datasource.druid.min-evictable-idle-time-millis=30000
    spring.datasource.druid.validation-query=select 'x'
    spring.datasource.druid.test-while-idle=true
    spring.datasource.druid.test-on-borrow=false
    spring.datasource.druid.test-on-return=false
    spring.datasource.druid.pool-prepared-statements=true
    spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
    spring.datasource.druid.filters=stat,wall,slf4j
    spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
    
    spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
    spring.datasource.druid.web-stat-filter.profile-enable=true
    spring.datasource.druid.web-stat-filter.session-stat-enable=true
    spring.datasource.druid.web-stat-filter.session-stat-max-count=100000
    
    #druid 监控
    spring.datasource.druid.stat-view-servlet.enabled=true
    spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
    spring.datasource.druid.stat-view-servlet.reset-enable=true
    spring.datasource.druid.stat-view-servlet.login-username=admin
    spring.datasource.druid.stat-view-servlet.login-password=123456
    
    spring.datasource.druid.filter.stat.log-slow-sql= true
    spring.datasource.druid.filter.stat.slow-sql-millis=1000
    spring.datasource.druid.filter.stat.merge-sql=true
    spring.datasource.druid.filter.stat.db-type=mysql
    spring.datasource.druid.filter.stat.enabled=true
    
    spring.datasource.druid.filter.slf4j.enabled=true
    spring.datasource.druid.filter.slf4j.connection-log-enabled=true
    spring.datasource.druid.filter.slf4j.connection-close-after-log-enabled=true
    spring.datasource.druid.filter.slf4j.connection-commit-after-log-enabled=true
    spring.datasource.druid.filter.slf4j.connection-connect-after-log-enabled=true
    spring.datasource.druid.filter.slf4j.connection-connect-before-log-enabled=true
    spring.datasource.druid.filter.slf4j.connection-log-error-enabled=true
    spring.datasource.druid.filter.slf4j.data-source-log-enabled=true
    spring.datasource.druid.filter.slf4j.result-set-log-enabled=true
    spring.datasource.druid.filter.slf4j.statement-log-enabled=true
    
    
    # 启动类注解上增加注解
    @MapperScan("com.leiwu.common.database.mapper")
    
    # 配置文件application.properties中增加mapper xml文件的位置
    # mybatis
    mybatis.type-aliases-package=com.leiwu.common.database.po
    mybatis.mapper-locations=classpath:/mapper/*.xml
    
    • 将druid.properties配置文件注入spring容器
    import lombok.Data;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    /**
     * 系统配置
     *
     * @author 伍磊
     */
    @Data
    @PropertySource(value = {"classpath:/config/${spring.profiles.active}/thymeleaf.properties",
            "classpath:/config/${spring.profiles.active}/druid.properties",
            "classpath:/config/${spring.profiles.active}/redis.properties"},
            encoding = "UTF-8", ignoreResourceNotFound = true)
    @Configuration
    public class SystemConfig {
    }
    

    相关文章

      网友评论

          本文标题:springboot整合mybatis

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