【项目管理】在IDEA中使用MyBatis_Generator生成Dto、Dao、Mapping
来源:互联网 发布:淘宝关联页 编辑:程序博客网 时间:2024/05/20 05:29
一、添加配置文件
新建一个空的XML配置文件,名称可以随便取,这里以generatorConfig.xml为名,放在resources目录下generator文件中。具体内容如下:
<?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="false"></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="false"></property> <property name="trimStrings" value="true"></property> </javaModelGenerator> <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"> <property name="enableSubPackages" value="false"></property> </sqlMapGenerator> <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <table tableName="sys_user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table></context></generatorConfiguration>
其中需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。
二、在resources下添加配置文件mydb.properties,该文件主要是用于配置数据库连接信息,内容如下:
三、添加gradle.properties配置文件。具体内容如下:
#生成的实体类所在包modelPackage=com.rdpm.sysm.entity#生成的mapper接口类所在包mapperPackage=com.rdpm.sysm.dao#生成的mapper xml文件所在包,默认存储在resources目录下sqlMapperPackage=mappers
四、在build.gradle添加如下信息:
//mybatis的Generator插件入口configurations { mybatisGenerator}
//使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件//给mybatisGenerator插件用的jar包mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2'mybatisGenerator 'mysql:mysql-connector-java:5.1.38'mybatisGenerator 'tk.mybatis:mapper:3.3.1'
def getDbProperties = { def properties = new Properties() file("src/main/resources/mysql-generate.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.user") ant.properties['password'] = properties.getProperty("jdbc.pass") 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'] = this.modelPackage ant.properties['mapperPackage'] = this.mapperPackage ant.properties['sqlMapperPackage'] = this.sqlMapperPackage ant.taskdef( name: 'mbgenerator', classname: 'org.mybatis.generator.ant.GeneratorAntTask', classpath: configurations.mybatisGenerator.asPath ) ant.mbgenerator(overwrite: true, configfile: 'src/main/resources/generator/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的刷新按钮进行刷新,运行没有错就会在other目录下看见MybatisGenerator项,双击该项运行成功后,就会看见自动生成相关的文件。如图:最终效果如下:
红框内容就是mysql-generate自动生成的效果。
六、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="MysqlTable" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!--commentGenerator生成的代码中可以包含注释信息,具体就是生成表或字段的备注信息。-->
<commentGenerator>
<!--**阻止**生成注释,默认为false-->
<property name="suppressAllComments" value="true"></property>
<!--**阻止**生成的注释包含时间戳,默认为false-->
<property name="suppressDate" value="true"></property>
<!--生成的代码编码格式-->
<property name="javaFileEncoding" value="utf-8"/>
</commentGenerator>
<!--指定数据库连接信息-->
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
</jdbcConnection>
<!--指定JDBC类型和Java类型如何转换
该属性可以控制是否强制DECIMAL和NUMERIC类型的字段转换为Java类型的java.math.BigDecimal,
默认值为false.
-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--该元素用来控制生成的实体类,即生成模型的包名和位置-->
<!--targetPackage:生成实体类存放的包名-->
<!--targetProject:指定目标项目路径,使用的是文件系统的绝对路径-->
<javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
<property name="enableSubPackages" value="false"></property>
<!--trimStrings:是否对数据库查询结果进行trim操作-->
<property name="trimStrings" value="true"></property>
</javaModelGenerator>
<!--生成mapper映射文件的包名和位置-->
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<!--XMLMAPPER:所有的方法都在XML中,接口调用依赖XML文件。-->
<javaClientGenerator targetPackage="${mapperPackage}">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--tableName:指定要生成的表名
enableXXX:XXX代表多种SQL方法,该属性用来指定是否生成对应的XXX语句。-->
<table tableName="sys_user" enableCountByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
========================================================================================
更详细的属性解释请查看MyBatis Generator 详解
========================================================================================
- 【项目管理】在IDEA中使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- 使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- QWebview 控件直接拉上去不能通过编译的问题。
- opencv学习_11 (moravec角点检测及缺点)
- JAVA学习总结4
- UE4的MaterialInstance作用
- Git和svn 的差异
- 【项目管理】在IDEA中使用MyBatis_Generator生成Dto、Dao、Mapping
- Ajax实现的联动
- retrofit post请求
- 去除文本中的html代码 编辑器适用
- 安卓-父控件、子控件焦点问题
- Sys内核驱动签名指南
- 精简版android事件分发解释
- 算法的一些读后感
- sqlserver和oracel的使用心得记录(语句技巧和错误解决)