使用mybatisGenerator插件可以自动生成Mapper,Xml,Pojo,减少工作量。
当然,也可以使用Mybastic Plus。
在Springboot项目中的src/main/resource目录下加入mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局的设置信息-->
<settings>
<!-- 配置用生成的主键值 -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 启用缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 启用延迟加载功能 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 按需要延迟加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启驼峰命名(方便自动映射) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启SQL控制台打印 -->
<setting name="logImpl" value="SLF4J" />
</settings>
<!-- 批量设置别名(可以不配) 作用:就是在mapper.xml文件中直接写类名,也可以不用写全路径名。 -->
<typeAliases>
<!--<package name="cn.e3mall.manager.po" />-->
</typeAliases>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="org.mybatis.extend.interceptor.PaginationInterceptor">
</plugin>
</plugins>
</configuration>
Springboot中使用mybastic,要在gradle的配置文件中 加入依赖。
implementation 'org.springframework.boot:spring-boot-starter'。
在gradle的配置文件中的configurations加入 mybatisGenerator 插件,
mybatisGenerator插件就是用来运行并生成生成Mapper,Xml,Pojo。
完整的配置文件settings.gradle如下:
buildscript {
ext { springBootVersion = '2.2.6.RELEASE' }
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url "https://plugins.gradle.org/m2/" }
//mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "gradle.plugin.com.thinkimi.gradle:mybatis-generator-plugin:2.2"
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "com.thinkimi.gradle.MybatisGenerator"
group = 'com.ligeit'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
mainClassName = 'com.ligeit.business.BusinessApiApplication'
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo1.maven.org/maven2/" }
jcenter() { url "https://maven.aliyun.com/repository/jcenter" }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
implementation 'org.springframework.boot:spring-boot-starter-logging:2.0.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-amqp:2.1.4.RELEASE'
implementation 'org.apache.httpcomponents:httpclient:4.5.10'
implementation 'net.logstash.logback:logstash-logback-encoder:6.0'
implementation 'com.alibaba.boot:nacos-config-spring-boot-starter:0.2.7'
//implementation 'com.xuxueli:xxl-job-core:2.1.2'
//implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:2.2.5.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.apache.shiro:shiro-spring-boot-web-starter:1.5.2'
implementation 'mysql:mysql-connector-java:8.0.11'
compile fileTree(dir: 'libs', include: ['*.jar'])
// compile files('libs/huabo-java-sdk-core-2.0.3.jar')
implementation 'io.jsonwebtoken:jjwt-impl:0.11.1'
implementation 'io.jsonwebtoken:jjwt-api:0.11.1'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.1'
implementation 'com.lmax:disruptor:3.4.2'
testImplementation 'org.testng:testng:6.8.17'
implementation 'org.apache.kafka:kafka-clients'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'com.github.xiaoymin:swagger-bootstrap-ui:1.9.6'
compile project(':tbl-sdk')
}
configurations {
providedRuntime
mybatisGenerator
// remove default logger
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
all*.exclude group: 'ch.qos.logback'
}
mybatisGenerator {
verbose = true
configFile = 'src/test/resources/mybatis/generatorConfig_bus.xml'
}
jar { enabled = true }
/**
* 执行zip打包操作
*/
task zip(type: Zip, dependsOn: ['clean', 'jar']) {
//from 'build/package'
metadataCharset "UTF-8"
extension "zip"
destinationDir file('build/')
baseName 'tbl-business-api'
into(project.name) {
into("lib") {
from(configurations.runtimeClasspath)
from('build/libs/')
}
// into("conf") {
// from("src/main/resources/")
// excludes = ['**/com/**']
// }
// into("bin") {
// from("src/main/scripts")
// fileMode 0744
// }
}
}
mybatisGenerator插件配置好后,就会在IDEA上显示,点击就运行。