本文主要是介绍Springboot+gradle+Mybatis-Generator 代码自动生成器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天看了mybatis-Generator的官方文档,研究了一下,决定自己也搭一个,但是官方文档和大部分资料都是基于maven项目的,查了gradle的官方文档,终于找到了官方配置,很激动(^-^)V,记录下来,方便记忆也同时分享给大家~
mybatis-Generator官方文档:http://www.mybatis.org/genera...
Gradle官方文档:https://plugins.gradle.org/pl...
gradle
buildscript {ext {springBootVersion = '1.5.10.RELEASE'}repositories {mavenCentral()//添加maven仓库maven {url "https://plugins.gradle.org/m2/"}}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")// mybatis-generator 插件路径classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
//引入 mybatis-generator 插件
apply plugin: "com.arenagod.gradle.MybatisGenerator"group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}dependencies {compile('org.springframework.boot:spring-boot-starter-web')testCompile('org.springframework.boot:spring-boot-starter-test')//数据源compile 'com.alibaba:druid-spring-boot-starter:1.1.2'compile 'mysql:mysql-connector-java:6.0.6'//配置mybatiscompile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"//mybatis-generator core 包compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version:'1.3.2'
}configurations {mybatisGenerator
}
// mybatis-generator.xml 配置路径
//这里会遇到个问题:MyBatis Generator 通过xml生成,有日志但是没有生成文件成功的问题,
//原因:mac下是找不到 ./src 路径的,需要全路径,如下配置。windows则为src/main/resources/generator.xml
mybatisGenerator {verbose = trueconfigFile = '/Users/murasakiseifu/mybatisGenerator/src/main/resources/generator.xml'
}
generator.xml
按照刚才的配置路径,在 resources 文件夹下建立 generator.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><context id="my" targetRuntime="MyBatis3"><!--自动实现Serializable接口--><plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin><!-- 去除自动生成的注释 --><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><!--数据库基本信息--><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/takin_write"userId="murasakiseifu"password="123456"></jdbcConnection><!--生成实体类的位置以及包的名字--><!--同样Mac用户:targetProject 为全路径--><javaModelGenerator targetPackage="com.example.entity" targetProject="/Users/murasakiseifu/mybatisGenerator/src/main/java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="true" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator><!--生成映射文件存放位置--><!--同样Mac用户:targetProject 为全路径--><sqlMapGenerator targetPackage="mapper" targetProject="/Users/murasakiseifu/mybatisGenerator/src/main/resources"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false"/></sqlMapGenerator><!--生成Dao类存放位置,mapper接口生成的位置--><!--同样Mac用户:targetProject 为全路径--><javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="/Users/murasakiseifu/mybatisGenerator/src/main/java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false"/></javaClientGenerator><!-- 配置表信息 --><!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample是否生成 example类 --><table schema="takin_write" tableName="c_student"domainObjectName="Student" enableCountByExample="false"enableDeleteByExample="false" enableSelectByExample="false"enableUpdateByExample="false"></table></context>
</generatorConfiguration>
生成代码
双击 mbGenerator
这篇关于Springboot+gradle+Mybatis-Generator 代码自动生成器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!