Mybatis generator 逆向生成代码
简单介绍
生成javaben dao接口
图片
文件里面引入需要的jar的依赖
<span style="color: #008000"><!--</span><span style="color: #008000"> mybatis </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.mybatis<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>mybatis<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>3.4.0<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> mybaits-generator </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.mybatis.generator<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>mybatis-generator-core<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>1.3.5<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> <a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a>驱动 </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>com.oracle<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>ojdbc6<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>0.0.1-SNAPSHOT<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">build</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">finalName</span><span style="color: #0000ff">></span>Mybaits<span style="color: #0000ff"></</span><span style="color: #800000">finalName</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">build</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></
<span style="color: #800000">project<span style="color: #0000ff">>
配置文件
配置文件里面有些地方需要改动,需要的朋友可以根据自己的情况改动
配置文件内容
<span style="color: #0000ff"><</span><span style="color: #800000">context </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="DB2Tables"</span><span style="color: #ff0000"> targetRuntime</span><span style="color: #0000ff">="MyBatis3"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">commentGenerator</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="suppressAllComments"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"></</span><span style="color: #800000">commentGenerator</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> 配置<a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a>连接 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">jdbcConnection </span><span style="color: #ff0000">driverClass</span><span style="color: #0000ff">="oracle.jdbc.OracleDriver"</span><span style="color: #ff0000"> connectionURL</span><span style="color: #0000ff">="jdbc:oracle:thin:@localhost:1521:xe"</span><span style="color: #ff0000"> userId</span><span style="color: #0000ff">="admin007"</span><span style="color: #ff0000"> password</span><span style="color: #0000ff">="admin"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">jdbcConnection</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">javaTypeResolver</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="forceBigDecimals"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="false"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"></</span><span style="color: #800000">javaTypeResolver</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> 指定javaBean<a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>的位置 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">javaModelGenerator </span><span style="color: #ff0000">targetPackage</span><span style="color: #0000ff">="com.bean"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">=".\src\main\java"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="trimStrings"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"></</span><span style="color: #800000">javaModelGenerator</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000">指定<a href="/tag/sql/" target="_blank" class="keywords">sql</a>映射<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>的位置 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000"><a href="/tag/sql/" target="_blank" class="keywords">sql</a>MapGenerator </span><span style="color: #ff0000">targetPackage</span><span style="color: #0000ff">="mapper"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">=".\src\main\resources"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"></</span><span style="color: #800000"><a href="/tag/sql/" target="_blank" class="keywords">sql</a>MapGenerator</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> 指定dao接口<a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>的位置,mapper接口 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">javaClientGenerator </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="XMLMAPPER"</span><span style="color: #ff0000"> targetPackage</span><span style="color: #0000ff">="com.dao"</span><span style="color: #ff0000"> targetProject</span><span style="color: #0000ff">=".\src\main\java"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="enableSubPackages"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"></</span><span style="color: #800000">javaClientGenerator</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> table指定每个表的<a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>策略 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">table </span><span style="color: #ff0000">tableName</span><span style="color: #0000ff">="t_user"</span><span style="color: #ff0000"> domainObjectName</span><span style="color: #0000ff">="User"</span><span style="color: #0000ff">></</span><span style="color: #800000">table</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">context</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></
<span style="color: #800000">generatorConfiguration<span style="color: #0000ff">>
生成的启动java文件
<span style="color: #0000ff">import<span style="color: #000000"> java.io.File;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.ArrayList;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.List;<span style="color: #0000ff">import<span style="color: #000000"> org.mybatis.generator.api.MyBatisGenerator;
<span style="color: #0000ff">import<span style="color: #000000"> org.mybatis.generator.config.Configuration;
<span style="color: #0000ff">import<span style="color: #000000"> org.mybatis.generator.config.xml.ConfigurationParser;
<span style="color: #0000ff">import<span style="color: #000000"> org.mybatis.generator.internal.DefaultShellCallback;<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Test {
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { List</span><String> warnings = <span style="color: #0000ff">new</span> ArrayList<String><span style="color: #000000">(); </span><span style="color: #0000ff">boolean</span> overwrite = <span style="color: #0000ff">true</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000"> <a href="/tag/peizhiwenjian/" target="_blank" class="keywords">配置文件</a>路径切记写对</span> File configFile = <span style="color: #0000ff">new</span> File("src/main/resources/mbg.xml"<span style="color: #000000">); ConfigurationParser cp </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> ConfigurationParser(warnings); Configuration config </span>=<span style="color: #000000"> cp.parseConfiguration(configFile); DefaultShellCallback callback </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> MyBatisGenerator(config,callback,warnings); myBatisGenerator.generate(</span><span style="color: #0000ff">null</span><span style="color: #000000">); }
}
配置文件写完之后直接运行此配置文件,就可以生成代码,下面附上生成之后的效果(运行完之后,刷新项目 就可以看到生成的文件)
文章中涉及到的配置文件,启动类等都可以在mybatis官网找到。
错误,请大神指导。