有时候,我们想要指定数据库的某张表,生成对应的映射文件、实体类、dao接口;这时候我们可以通过mybatis插件完成。
当然,方法有很多,这只是其中之一,仅作为参考,比如Easy Code插件更强大些。
准备工作:
1、jar包:mybatis-generator-core-oracle-1.3.6.jar
2、配置文件:generatorConfig.xml
3、以上两项准备完毕,执行命令:
java -jar mybatis-generator-core-oracle-1.3.6.jar -configfile generatorConfig.xml -overwrite
注意:jar包应该要与配置文件保持同一路径。
其实配置文件内容要修改的只是很少的一部分基本够用了,本人也只是简单运用。
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>
<!-- 驱动包所在位置-->
<classPathEntry
location="E:\\maven_warehouse\\repository\\com\\oracle\\ojdbc6\\11.2.0.3\\ojdbc6-11.2.0.3.jar" />
<context id="sqlserverTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="oracle.jdbc.OracleDriver"
<!--mysql连接:
connectionURL="jdbc:xxxx?nullCatalogMeansCurrent=true"
其中,nullCatalogMeansCurrent=true 可解决不生成按主键操作的sql
-->
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:productdb"
userId="proddev"
password="123123">
</jdbcConnection>
<!--
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下
-->
<!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">-->
<javaModelGenerator targetPackage="com.aspire.ability.common.entity" targetProject="../java">
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--对应的mapper.xml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="./">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 对应的Mapper接口类文件,一般只修改targetPackage、targetProject即可 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aspire.ability.common.dao" targetProject="../java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 列出要生成代码的所有表,这里配置的是不生成Example文件;一般只修改tableName、domainObjectName对应的实体类名 -->
<table tableName="Om_Monitor_Amount_record" domainObjectName="OmMonitorAmountRecord"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>