All Projects → kingcos → MyBatisGenerator-Tool

kingcos / MyBatisGenerator-Tool

Licence: MIT license
🔧 A tool which aims to generate code by using MyBatis Generator with Gradle in IntelliJ IDEA.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to MyBatisGenerator-Tool

seedpress-cms
A headless CMS built in Express for PostgresQL using Sequelize. Generally follows the Wordpress post and term schema.
Stars: ✭ 71 (+255%)
Mutual labels:  mit-license
fight-for-artistic-creativity
Twitterをディストピアにしないために、我々ができること。
Stars: ✭ 19 (-5%)
Mutual labels:  mit-license
Willow
The Web Interaction Library that eases the burden of creating AJAX-based web applications
Stars: ✭ 41 (+105%)
Mutual labels:  mit-license
atguigu ssm crud
Atguigu-SSM-CRUD 一个最基本的CRUD系统,采用IDEA+Maven搭建,具备前后端交互功能,前端采用BootStrap+Ajax异步请求DOM渲染,后端采用SpringMVC+MyBatis+Mysql8.0+Servlet+Jsp,符合REST风格URL规范,并加入了Hibernate提供的数据校验功能,支持PageHelper的分页功能,很适合SSM阶段性练习。同时用到了很多前端操作以及BootStrap组件,也有利于学习JS和前端框架。
Stars: ✭ 52 (+160%)
Mutual labels:  mybatis-generator
vr-streaming-overlay
SteamVR overlay for streamers on Linux/Windows
Stars: ✭ 29 (+45%)
Mutual labels:  mit-license
Inventus
Inventus is a spider designed to find subdomains of a specific domain by crawling it and any subdomains it discovers.
Stars: ✭ 80 (+300%)
Mutual labels:  mit-license
mybatis-generator-gui-plus
基于MyBatis-Generator+SQLite+beautyeye_lnf开发的一款图形化代码生成器
Stars: ✭ 16 (-20%)
Mutual labels:  mybatis-generator
ParsecSoda
Parsec Soda is a custom open-source game streaming app that integrates with Parsec API and is focused in Host experience.
Stars: ✭ 135 (+575%)
Mutual labels:  mit-license
powerslaves
Taking PowerSaves as a slave to your will.
Stars: ✭ 28 (+40%)
Mutual labels:  mit-license
web-service-demo
基于Dubbo的微服务基础框架,整合Spring Boot 2与Dubbo对外提供独立服务,集成Redis、Rabbitmq、mybatis。
Stars: ✭ 23 (+15%)
Mutual labels:  mybatis-generator
wikibot
A 🤖 which provides features from Wikipedia like summary, title searches, location API etc.
Stars: ✭ 25 (+25%)
Mutual labels:  mit-license
sqip
SQIP is a tool for SVG-based LQIP image creation written in go
Stars: ✭ 46 (+130%)
Mutual labels:  mit-license
Winter
Winter is a 2D game engine for Pharo Smalltalk
Stars: ✭ 43 (+115%)
Mutual labels:  mit-license
ANODA-Turn-Timer
ANODA Open Source iOS Swift example app
Stars: ✭ 19 (-5%)
Mutual labels:  mit-license
fswatch
File/Directory Watcher for Modern C++
Stars: ✭ 56 (+180%)
Mutual labels:  mit-license
Halma
Halma Game Built With The Awesome libGDX framework
Stars: ✭ 20 (+0%)
Mutual labels:  mit-license
Unity-FPS-Counter
#NVJOB FPS Counter and Graph. Free Unity Asset.
Stars: ✭ 44 (+120%)
Mutual labels:  mit-license
PHP-File-Cache
Light, simple and standalone PHP in-file caching class
Stars: ✭ 34 (+70%)
Mutual labels:  mit-license
lidtk
Language Identification Toolkit
Stars: ✭ 17 (-15%)
Mutual labels:  mit-license
nn-segmentation-for-lar
Neural networks to segment some type of biomedical images
Stars: ✭ 21 (+5%)
Mutual labels:  mit-license

MyBatisGenerator-Tool

在 Intellij IDEA 中结合 Gradle 使用 MyBatis Generator 逆向生成代码

  • Info:
  • JDK 1.8
  • Gradle 2.14
  • Intellij IDEA 2016.3

前言

由于 IDEA 的教程较少,且 MyBatis Generator 不支持 Gradle 直接运行,因此这次是在自己折腾项目过程中,根据一些参考资料加上自己的实践得出的结论,并附上相应的 Demo 可供自己未来参考,也与大家分享。

本文的 Demo 也可以当作工具直接导入 IDEA,加上自己的数据库信息即可生成代码。

创建项目

详细的创建步骤可以参考使用 Gradle 构建 Struts 2 Web 应用中「新建 Gradle Web 项目」一节即可。当创建完毕,需要等待 Gradle 联网构建,由于国内网络因素,可能需要稍作等待。当构建完成,目录结构应如下图一致:

Gradle 构建完成

配置依赖

这里需要使用 MyBatis Generator,MySQL 驱动,以及 MyBatis Mapper。由于代码生成单独运行即可,不需要参与到整个项目的编译,因此在 build.gradle 中添加配置:

configurations {
    mybatisGenerator
}

Maven Repository (http://mvnrepository.com/) 分别搜索依赖:

org.mybatis.generator

mysql-connector-java

tk.mybatis:mapper

依赖的版本并不是局限于某个特定版本,可以选择适时相应的更新版本。添加到 build.gradle 的 dependencies 中(注意前面需将「compile group:」改为 「mybatisGenerator」):

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    mybatisGenerator 'mysql:mysql-connector-java:5.1.40'
    mybatisGenerator 'tk.mybatis:mapper:3.3.9'
}

设置数据库信息

在 resources 下,新建 mybatis 文件夹,并新建 config.properties 和 generatorConfig.xml,文件结构如下:

配置文件目录

在 config.properties 中配置数据库和要生成的 Java 类的包:

# JDBC 驱动类名
jdbc.driverClassName=com.mysql.jdbc.Driver
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
jdbc.url=jdbc:mysql://
# JDBC 用户名及密码
jdbc.username=
jdbc.password=

# 生成实体类所在的包
package.model=com.maimieng.model
# 生成 mapper 类所在的包
package.mapper=com.maimieng.mapper
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mybatis

设置生成代码的配置文件

在 generatorConfig.xml 中配置数据库表信息,可以参考官方的文档(附在文末)来进行配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <commentGenerator>
            <property name="suppressAllComments" value="true"></property>
            <property name="suppressDate" value="true"></property>
            <property name="javaFileEncoding" value="utf-8"/>
        </commentGenerator>

        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
            <property name="enableSubPackages" value="true"></property>
            <property name="trimStrings" value="true"></property>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
            <property name="enableSubPackages" value="true"></property>
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="ANNOTATEDMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- sql占位符,表示所有的表 -->
        <table tableName="%">
            <generatedKey column="epa_id" sqlStatement="Mysql" identity="true" />
        </table>
    </context>
</generatorConfiguration>

Gradle 设置 Ant Task

由于 MyBatis Generator 尚不支持 Gradle,所以只能使用 Gradle 来执行 Ant Task,达到相同的效果。

build.gradle:

def getDbProperties = {
    def properties = new Properties()
    file("src/main/resources/mybatis/jdbc.properties").withInputStream { inputStream ->
        properties.load(inputStream)
    }
    properties
}

task mybatisGenerate << {
    def properties = getDbProperties()
    ant.properties['targetProject'] = projectDir.path
    ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName")
    ant.properties['connectionURL'] = properties.getProperty("jdbc.url")
    ant.properties['userId'] = properties.getProperty("jdbc.username")
    ant.properties['password'] = properties.getProperty("jdbc.password")
    ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
    ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path
    ant.properties['modelPackage'] = properties.getProperty("package.model")
    ant.properties['mapperPackage'] = properties.getProperty("package.mapper")
    ant.properties['sqlMapperPackage'] = properties.getProperty("package.xml")
    ant.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )
    ant.mbgenerator(overwrite: true,
            configfile: 'src/main/resources/mybatis/generatorConfig.xml', verbose: true) {
        propertyset {
            propertyref(name: 'targetProject')
            propertyref(name: 'userId')
            propertyref(name: 'driverClass')
            propertyref(name: 'connectionURL')
            propertyref(name: 'password')
            propertyref(name: 'src_main_java')
            propertyref(name: 'src_main_resources')
            propertyref(name: 'modelPackage')
            propertyref(name: 'mapperPackage')
            propertyref(name: 'sqlMapperPackage')
        }
    }
}

配置好 build.gradle,就可以在 IDEA 的 Gradle 菜单中找到新的 Task:

mybatisGenerate

如果这里没有,可能是 Gradle 没有 build,重新刷新即可:

Refresh all Gradle projects

双击「mybatisGenerate」即可运行,如果配置正确即可运行成功:

成功生成代码

注:这里我在 generatorConfig.xml 配置选择的是按注解映射,因此没有 xml 文件生成。

总结

这次的 Demo 您可以在 https://github.com/kingcos/MyBatisGenerator-Tool 下载查看。Demo 本身也可作为工具来生成代码。由于目前正在完善一个 Android + Java Web 的小系统,因此未来可能还会更新 Spring Boot、Druid、Swagger 的配置脚手架。

参考资料

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].