自动生成框架的意义

主要为了解决人为添加mapper,模型等工作,减少错误,提交效率!

添加引用build.gradle

configurations {
mybatisGenerator
}
   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'

添加配置文件

sources/mybaits目录

config.properties

# JDBC 驱动类名
jdbc.driverClassName=com.mysql.jdbc.Driver
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + :端口号 + /数据库名
jdbc.url=jdbc:mysql://***:3306/test
# JDBC 用户名及密码
jdbc.username=a
jdbc.password=a123 # 生成实体类所在的包
package.model=cn.customer.entity
# 生成 mapper 类所在的包
package.mapper=cn.customer.mapper
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapper

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">
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="cn.management.mapper"/>
<!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
<property name="caseSensitive" value="true"/>
</plugin>
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
</jdbcConnection>
<javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"/>
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"/>
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"/>
<!-- sql占位符,表示所有的表 -->
<table tableName="%">
<generatedKey column="epa_id" sqlStatement="Mysql" identity="true" />
</table>
</context>
</generatorConfiguration>

在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')
}
}
}

在左侧gradle工具栏里可以找到mybatisGenerate,然后点击发布即可。

gradle下mybatis自动生成框架的使用的更多相关文章

  1. dos下mybatis自动生成代码

    今天来介绍下怎么用mybatis-gennerator插件自动生成mybatis所需要的dao.bean.mapper xml文件,这样我们可以节省一部分精力,把精力放在业务逻辑上. 之前看过很多文章 ...

  2. mybatis自动生成java代码

    SSM框架没有DB+Record模式,写起来特别费劲,只能用下面的方法勉强凑合. 上图中,*.jar为下载的,src为新建的空白目录,.xml配置如下. <?xml version=" ...

  3. 【MyBatis】MyBatis自动生成代码查询之爬坑记

    前言 项目使用SSM框架搭建Web后台服务,前台后使用restful api,后台使用MyBatisGenerator自动生成代码,在前台使用关键字进行查询时,遇到了一些很宝贵的坑,现记录如下.为展示 ...

  4. 使用mybatis-generator插件结合tk.mybatis自动生成mapper二三事

    本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑.     ...

  5. mybatis自动生成代码工具(逆向工程)

    MyBatis自动生成实体类(逆向工程) MyBatis属于一种半自动的ORM框架,它需要我们自己编写sql语句和映射文件,但是编写映射文件和sql语句很容易出错,所以mybatis官方提供了Gene ...

  6. 使用mybatis-generator插件结合tk.mybatis自动生成mapper

    本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑.     ...

  7. Mybatis自动生成实体类

    Maven自动生成实体类需要的jar包 一.pom.xml中 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  8. 谨慎使用MyBatis自动生成Where语句

    最近监控到类似这样一个慢查询: select XX_time from XXOrderInfo WHERE ( OrderId is not null and OrderId = N'xxxx') x ...

  9. mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

    mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置 ============================== 蕃薯耀 2018年3月14 ...

随机推荐

  1. dicom 影像通信(scu、scp)的c-echo、c-store、c-find、c-move

    本文主要描述,dicom通信的scu,scp的c-echo.c-store.c-find.c-move的使用. DicomServiceIDicomServiceProviderIDicomCStor ...

  2. Python-TCP客户端程序开发

    TCP客户端,需要与服务端建立连接,连接建立成功后才可以进行数据的传输. # 1.导入模块 import socket if __name__ == '__main__': # 2.创建套接字对象 t ...

  3. docker tomcat镜像部署springbootwar包

    springboot打war包 1.在pom文件中增加插件 <build> <finalName>xx</finalName> <plugins> &l ...

  4. 19.JAVA-从文件中解析json、并写入Json文件(详解)

    1.json介绍 json与xml相比, 对数据的描述性比XML较差,但是数据体积小,传递速度更快. json数据的书写格式是"名称:值对",比如: "Name" ...

  5. OA项目之mybatis动态查询

    类似于三个条件,可以全部选择,也可以选择几个条件进行查询 Mapper.xml文件: <resultMap type="Employee" id="selAll&q ...

  6. Docker学习-jenkins+github实现持续集成和部署

    上一篇介绍了docker环境搭建,本篇继续深入,结合jenkins利用docker-compose容器编排简单介绍下如何实现个人学习的持续集成/部署. 本篇学习曲线: 1.安装/运行jenkins容器 ...

  7. 这些C++常用内置函数你会几个??

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:Regina520  新手注意:如果你C++学的不好,可以去拿我的C+ ...

  8. iSensor APP 之 摄像头调试 OV9655

    iSensor APP 之 摄像头调试  OV9655 iSensor app 非常适合调试各种摄像头,已测试通过的sensor有: l  OV7670.OV7725.OV9650.OV9655.OV ...

  9. 微信团队分享:极致优化,iOS版微信编译速度3倍提升的实践总结

    1.引言 岁月真是个养猪场,这几年,人胖了,微信代码也翻了. 记得 14 年转岗来微信时,用自己笔记本编译微信工程才十来分钟.如今用公司配的 17 年款 27-inch iMac 编译要接近半小时:偶 ...

  10. NodeJS7-1本地构建_gulp入门学习

    NodeJS在前端最常用的两种方式: 1.做成webserver 2.做成前端开发的相关工具 本地构建:前端发布代码都会经过压缩(谁来处理) ,前端技术的日新月异,利用新特性代码变得易读,清晰,可是老 ...