mybatis是目前很流行的持久层框架,其逆向工程更是大大缩减了我们的开发时间。有兴趣的可以看文档。
文档地址: http://www.mybatis.org/generator/index.html
废话不多讲,下面我们总结一下他的几种配置使用的方式(以Eclipse为例):
一、Eclipse插件安装的方式
这里的安装分为在线安装和手动安装。我们先来说手安装。
1、手动安装
下载地址:https://dl.bintray.com/mybatis/mybatis-generator
2、在线安装
eclipse在线安装mybatis generator,打开eclipse,找到help->Eclispe Mapketplace
在搜索框输入mybatis generator,然后点击go。
找到mybatis generator对应的版本,下载后然后重启eclipse即可
1 xml version="1.0" encoding="UTF-8"?> 2 DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id="testTables" targetRuntime="MyBatis3"> 8 <commentGenerator> 9 10 <property name="suppressAllComments" value="true" /> 11 commentGenerator> 12 13 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 14 connectionURL="jdbc:mysql:///db_demo" userId="root" 15 password="123"> 16 jdbcConnection> 17 19 <javaTypeResolver> 20 <property name="forceBigDecimals" value="false" /> 21 javaTypeResolver> 22 23 24 <javaModelGenerator targetPackage="com.demo.pojo" 25 targetProject=".\src"> 26 27 <property name="enableSubPackages" value="false" /> 28 29 <property name="trimStrings" value="true" /> 30 javaModelGenerator> 31 32 <sqlMapGenerator targetPackage="com.demo.mapper" 33 targetProject=".\src"> 34 35 <property name="enableSubPackages" value="false" /> 36 sqlMapGenerator> 37 38 <javaClientGenerator type="XMLMAPPER" 39 targetPackage="com.demo.mapper" 40 targetProject=".\src"> 41 42 <property name="enableSubPackages" value="false" /> 43 javaClientGenerator> 44 45 <table schema="" tableName="tb_user">table> 46 <table schema="" tableName="tb_area">table> 47 48 context> 49 generatorConfiguration> 50 51 作者:贪挽懒月 52 链接:https://www.jianshu.com/p/74ab984b4fee 53 来源:简书 54 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
在插件安装完之后,创建逆向工程文件 generatorConfig.xml
1 xml version="1.0" encoding="UTF-8"?> 2 DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 8 <context id="testTables"> 9 <commentGenerator> 10 11 <property name="suppressAllComments" value="true" /> 12 commentGenerator> 13 14 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 15 connectionURL="jdbc:mysql:///db_demo" userId="root" 16 password="123"> 17 jdbcConnection> 18 20 <javaTypeResolver> 21 <property name="forceBigDecimals" value="false" /> 22 javaTypeResolver> 23 24 25 <javaModelGenerator targetPackage="com.demo.pojo" 26 targetProject="工程名\src"> 27 28 <property name="enableSubPackages" value="false" /> 29 30 <property name="trimStrings" value="true" /> 31 javaModelGenerator> 32 33 <sqlMapGenerator targetPackage="com.demo.mapper" 34 targetProject="工程名/src/com/demo/mapper"> 35 36 <property name="enableSubPackages" value="false" /> 37 sqlMapGenerator> 38 39 <javaClientGenerator type="XMLMAPPER" 40 targetPackage="com.demo.mapper" 41 targetProject="工程名/src/com/demo/mapper"> 42 43 <property name="enableSubPackages" value="false" /> 44 javaClientGenerator> 45 46 47 <table schema="" tableName="tb_user">table> 48 <table schema="" tableName="tb_area">table> 49 50 context> 51 generatorConfiguration>
1 import java.io.File; 2 import java.io.IOException; 3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.mybatis.generator.api.MyBatisGenerator; 8 import org.mybatis.generator.config.Configuration; 9 import org.mybatis.generator.config.xml.ConfigurationParser; 10 import org.mybatis.generator.exception.InvalidConfigurationException; 11 import org.mybatis.generator.exception.XMLParserException; 12 import org.mybatis.generator.internal.DefaultShellCallback; 13 14 public class MBGTest { 15 public static void main(String args[]) 16 throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { 17 Listwarnings = new ArrayList (); 18 boolean overwrite = true; 19 File configFile = new File("generatorConfig.xml"); 20 ConfigurationParser cp = new ConfigurationParser(warnings); 21 Configuration config = cp.parseConfiguration(configFile); 22 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 23 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 24 myBatisGenerator.generate(null); 25 } 26 27 }
以Java Application方式执行即可
三、以Maven方式
mybatis generator在eclipse上的配置主要有在以下几个文件上需要进行修改内容:pom.xml,同样的需要配置文件generatorConfig.xml的创建与编写。
pom.xml需要添加的配置
1 <build> 2 <finalName>taotaofinalName> 3 <pluginManagement> 4 <plugins> 5 <plugin> 6 <groupId>org.mybatis.generatorgroupId> 7 <artifactId>mybatis-generator-maven-pluginartifactId> 8 <version>1.3.2version> 9 <configuration> 10 <configurationFile>src/main/resources/generator.xmlconfigurationFile> 11 <verbose>trueverbose> 12 <overwrite>trueoverwrite> 13 configuration> 14 <executions> 15 <execution> 16 <id>Generate MyBatis Artifactsid> 17 <goals> 18 <goal>generategoal> 19 goals> 20 execution> 21 executions> 22 <dependencies> 23 <dependency> 24 <groupId>org.mybatis.generatorgroupId> 25 <artifactId>mybatis-generator-coreartifactId> 26 <version>1.3.2version> 27 dependency> 28 dependencies> 29 plugin> 30 <plugin> 31 <groupId>org.apache.maven.pluginsgroupId> 32 <artifactId>maven-surefire-pluginartifactId> 33 <version>2.19.1version> 34 <configuration> 35 <skipTests>trueskipTests> 36 configuration> 37 plugin> 38 39 <plugin> 40 <groupId>org.apache.maven.pluginsgroupId> 41 <artifactId>maven-resources-pluginartifactId> 42 <version>3.0.1version> 43 <configuration> 44 <encoding>UTF-8encoding> 45 configuration> 46 plugin> 47 plugins> 48 pluginManagement> 49 build>
添加各种依赖,例如数据库依赖
1 <dependency> 2 <groupId>mysqlgroupId> 3 <artifactId>mysql-connector-javaartifactId> 4 <version>5.1.35version> 5 <scope>runtimescope> 6 dependency>
在maven面板中可以看到该插件:
同样的需要配置文件generatorConfig.xml的创建与编写。
(这里就略)
运用maven指令生成逆向工程
项目右键->run as->maven build...,Goals:中输入mybatis-generator:generate(这一步可以添加Maven)
最后在说下我在使用命令时遇到以下几个问题的解决方案
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique:
依赖必须是唯一的 原因是我在pom文件中加入了两个相同的依赖 删掉一个就行了
Non-resolvable parent POM for com.siyuan:siyuan-web:0.0.1-SNAPSHOT: Could not find artifa
原来是父工程没有注册,右键parent 项目 -run as - maven install 即可解决
四、以命令行模式
在做好相关的配置(添加完相关依赖或是jar,编写好文件)
1、打开命令提示符,进入lib目录下(也就是进入generatorConfig.xml所在文件夹,并且修改配置好)
2、输入命令:java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
3、出现Mybatis Generator finished successfully 代码生成完成
然后只需把文件复制到项目中,好久就是我们不用为了这些生成的文件来新建一个项目。
最后,在这里分享一份generatorConfig.xml配置文件的资料,觉得挺详细的。
1 xml version="1.0" encoding="UTF-8"?> 2 DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 15 16 20 21 33 <context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple" > 34 35 38 <property name="autoDelimitKeywords" value="false"/> 39 40 <property name="javaFileEncoding" value="UTF-8"/> 41 42 <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> 43 44 <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> 45 46 47 <property name="beginningDelimiter" value="`"/> 48 <property name="endingDelimiter" value="`"/> 49 50 53 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///pss" userId="root" password="admin"> 54 55 jdbcConnection> 56 57 61 <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> 62 70 <property name="forceBigDecimals" value="false"/> 71 javaTypeResolver> 72 73 74 79 <javaModelGenerator targetPackage="com._520it.mybatis.domain" targetProject="src/main/java"> 80 83 <property name="constructorBased" value="false"/> 84 85 86 <property name="enableSubPackages" value="true"/> 87 88 92 <property name="immutable" value="false"/> 93 94 99 <property name="rootClass" value="com._520it.mybatis.domain.BaseDomain"/> 100 101 102 <property name="trimStrings" value="true"/> 103 javaModelGenerator> 104 105 106 111 <sqlMapGenerator targetPackage="com._520it.mybatis.mapper" targetProject="src/main/resources"> 112 113 <property name="enableSubPackages" value="true"/> 114 sqlMapGenerator> 115 116 117 125 <javaClientGenerator targetPackage="com._520it.mybatis.mapper" type="ANNOTATEDMAPPER" targetProject="src/main/java"> 126 127 <property name="enableSubPackages" value="true"/> 128 129 132 javaClientGenerator> 133 134 172 <table tableName="userinfo" > 173 174 175 <property name="constructorBased" value="false"/> 176 177 178 <property name="ignoreQualifiersAtRuntime" value="false"/> 179 180 181 <property name="immutable" value="false"/> 182 183 184 <property name="modelOnly" value="false"/> 185 186 189 190 193 194 197 198 201 202 205 206 209 <property name="selectAllOrderByClause" value="age desc,username asc"/> 210 211 212 <property name="useActualColumnNames" value="false"/> 213 214 215 231 232 243 244 245 249 <columnOverride column="username"> 250 251 <property name="property" value="userName"/> 252 253 256 257 260 261 266 267 270 columnOverride> 271 272 279 table> 280 281 context> 282 283 generatorConfiguration> 284 285 作者:叩丁狼教育 286 链接:https://www.jianshu.com/p/e09d2370b796 287 来源:简书