SpringBoot+mybatis+gradle的多模块项目整合

最近想写一个个人项目,正好拿SpringBoot+mybatis+mysql+gradle组合练练手

其中SpringBoot整合Mybatis部分参考了官方Git的部分,搭建过程中遇到的各种粗心导致的Bug这里就不一一陈诉了,会在博文结束部分展示部分容易犯错的。

第一步:单间SpringBoot+gradle多模块项目。

具体请参考博文:https://blog.csdn.net/xybclndcn/article/details/78263833

第二步:将SpringBoot与mybatis整合

SpringBoot+Mybatis 官方整合地址:https://github.com/mybatis/spring-boot-starter,其中samples结束的是整合的项目,其它的为一些快捷配置,samples里面放了两个版本一个是基于xml的整合另外一种则是基于注解的整合,我更倾向于xml版,所以整合也是基于xml的。

SpringBoot+mybatis+gradle的多模块项目整合

阅读官方文档 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ 我们发现不同版本的mybatis的jar包所要求的SpringBoot版本号也不同。我搭建的时候去取了巧,直接在Springboot.io上面选中了mysql.mybatis所以就不考虑这些了

SpringBoot+mybatis+gradle的多模块项目整合


目录结构:

SpringBoot+mybatis+gradle的多模块项目整合

SpringBoot+mybatis+gradle的多模块项目整合


首先我们在SpringBoot+gradle+IDEA的基础上搭建出自己的多模块项目,本篇包名和上篇不一致,请勿生搬硬套

在提供API接口服务模块的build.gradle添加如下代码

buildscript {
    repositories {
        mavenLocal()
        maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.5.RELEASE")
    }
}

apply plugin: 'org.springframework.boot'

archivesBaseName = 'xxxSoil'

ext {
    springBootVar = '2.0.0.RELEASE'
}

dependencies {
    compile project(':xxxnCommon')          //要依赖的模块

    // spring boot
    compile "org.springframework.boot:spring-boot-starter-web:$springBootVar"
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
    runtime('mysql:mysql-connector-java')
    testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVar"
}

在resources目录下

1.新建application.properties

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/sss?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = shandian

#不能将这句带有通配符的代码放到mybatis-config中,否则会出现找不到com.xxx.xxx/XxxxMapper.xml的错误
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml                 

mybatis.config-location=classpath:mybatis-config.xml
logging.level.root=WARN
logging.level.sample.mybatis.mapper=TRACE

2.新建mybatis-config.xml

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.xxx.domain"/>
    </typeAliases>
</configuration>

配置完成进行mapper.xml,mapper.class,domain的编写


可能遇到的bug:

SpringBoot+mybatis+gradle的多模块项目整合

在包部分注意是包部分没有加@Mapper

SpringBoot+mybatis+gradle的多模块项目整合

有问题欢迎留言交流


接下来可能会进行的整合:

与shiro整合

与redis整合

与swagger的整合