我是靠谱客的博主 背后星星,这篇文章主要介绍gradle 中使用 mybatis-generator,现在分享给大家,希望可以做个参考。

  1. 添加依赖
    在build.gradle中添加
复制代码
1
2
3
configurations { mybatisGenerator }

注意把前面的compile group改成mybatisGenerator

复制代码
1
2
3
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.6' mybatisGenerator 'mysql:mysql-connector-java:5.1.45' mybatisGenerator 'tk.mybatis:mapper:3.5.2'
  1. 设置数据库信息
复制代码
1
在 resources 下,新建 mybatis 文件夹,并新建 config.properties 和 generatorConfig.xml,文件结构如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
# JDBC 驱动类名 jdbc.driverClassName=com.mysql.jdbc.Driver # JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名 jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 # JDBC 用户名及密码 jdbc.username=root jdbc.password=123456 # 生成实体类所在的包 package.model=com.yy.boot.domain # 生成 mapper 类所在的包 package.mapper=com.yy.boot.dao # 生成 mapper xml 文件所在的包,默认存储在 resources 目录下 package.xml=mybatis/mapper

generator.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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> <!--MyBatis3Simple或者MyBatis3,生成的xml文件略有不同--> <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> <!-- type=ANNOTATEDMAPPER表示不生成xml文件 这里我用XMLMAPPER --> <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- sql占位符,表示所有的表 这里只用city表 --> <!--<table tableName="%">--> <!--<generatedKey column="id" sqlStatement="Mysql" identity="true" />--> <!--</table>--> <table tableName="city" domainObjectName="City" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>

在build.gradle中添加

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def getDbProperties = { def properties = new Properties() file("src/main/resources/mybatis/config.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 中使用 mybatis-generator的全部内容,更多相关gradle内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(115)

评论列表共有 0 条评论

立即
投稿
返回
顶部