springboot程序员

springboot mybatis 整合

2017-08-27  本文已影响62人  小鱼嘻嘻

springboot for mybatis

首先说明一下,我很讨厌使用jdbc的方式(虽然我之前也用过一段时间),等我使用mybatis之后我就彻底抛弃了把SQL写在代码这种做法了,还有我认为mybatis比较灵活可以有 if else choose 等更加灵活,同时代码和SQL也完美的分开了,并没有耦合在一起。

<!-- mysql and druid -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.29</version>
</dependency>

<!-- mybatis springboot -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis.spring.boot.version}</version>
</dependency>

这里肯定有人说我要打脸了不是时候,springboot就一个配置文件吗?这。。。这里我要给自己洗白一下,只是说我们可以只用一个配置文件,如果我们想用其他的配置文件它当然也是支持的,这里我们就展示一下这两种情况

1 添加额外的配置文件

spring:
    datasource:
        name: test
        url: jdbc:mysql://localhost:3306/maoyan?useUnicode=true&connectTimeout=6000&socketTimeout=12000&characterEncoding=UTF-8
        username: root
        password: root
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 16
        initialSize: 4
        maxWait: 3000
        minIdle: 4
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 1
        testWhileIdle: true
        testOnBorrow: false
        dbc_connectionInitSqls: SET NAMES utf8mb4

//这里就添加了一个mybatis的配置文件
mybatis:
    configLocation: classpath:/mybatis-config.xml

那就配置这个文件吧(mybatis-config.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 全局映射器启用缓存 -->
        <setting name="cacheEnabled" value="false"/>
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
        <setting name="aggressiveLazyLoading" value="false"/>
        <!-- 允许JDBC支持生成的键。需要适合的驱动。如果设置为true则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如Derby) -->
        <setting name="useGeneratedKeys" value="false"/>
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <!-- 对于批量更新操作缓存SQL以提高性能 -->
        <setting name="defaultExecutorType" value="REUSE"/>
        <!-- 列名自动映射 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- null值数据库映射 -->
        <setting name="jdbcTypeForNull" value="NULL"/>
        <!-- 取消Session内的缓存,防止同一事务内多次查询拿到同样的对象 -->
        <setting name="localCacheScope" value="STATEMENT"/>
    </settings>


    <mappers>
        <mapper resource="com/yuxi/db/mapper/Account.xml"/>
    </mappers>


</configuration>

2 无需配置的方式
在 application.yml 里把配置文件修改为:

mybatis:
  mapper-locations: classpath:mappers/*.xml

这里需要注意mappers这个文件夹的命令不要是带"."的不然会扫描不到

//这个玩意可是不能没有的,不然会报错的,亲测
@MapperScan("com.yuxi.dao")

好了,代码就不贴了,可以直接下载下来看,每个例子我都跑过的,

当然数据库你需要换成你自己的哦!

源码地址:
http://git.oschina.net/xiaoyuxi/springbootlearning

上一篇 下一篇

猜你喜欢

热点阅读