Mybastic自動生成Mapper,Xml,Pojo。

使用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上顯示,點擊就運行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章