经常要用到MybatisGenerator反向生成ORM对象,故在springboot基础上几步快速搭建:
1、增加依赖
compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version:'1.3.2'
compile('com.jslsolucoes:ojdbc6:11.2.0.1.0')
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.6'
2、书写一个xml文件,放到src\main\resources下
如我新建了一个dbConfig.xml,放在这个目录下的dbtools,内容如下:
<?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="oracle" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@115.233.221.90:9521:orcl"
userId="smartcity"
password="smartcity">
</jdbcConnection>
<!-- java类型处理器
用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl;
注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型;
-->
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<!--
true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
false:默认,
scale>0;length>18:使用BigDecimal;
scale=0;length[10,18]:使用Long;
scale=0;length[5,9]:使用Integer;
scale=0;length<5:使用Short;
-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- java模型创建器,是必须要的元素-->
<javaModelGenerator targetPackage="com.example.test.dao" targetProject="D:/workspace-sts-3.9.2.RELEASE/spring-mybatisGenerator/src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="constructorBased" value="true"/>
<property name="trimStrings" value="true"/>
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 生成SQL map的XML文件生成器,
注意,在Mybatis3之后,我们可以使用mapper.xml文件+Mapper接口(或者不用mapper接口),
或者只使用Mapper接口+Annotation,
所以,如果 javaClientGenerator配置中配置了需要生成XML的话,这个元素就必须配置
targetPackage/targetProject:同javaModelGenerator
-->
<!-- <sqlMapGenerator targetPackage="com.bettershine.smartcity.dao" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
-->
<!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口
targetPackage/targetProject:同javaModelGenerator
type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
2,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
-->
<javaClientGenerator targetPackage="com.example.test.dao" type="ANNOTATEDMAPPER" targetProject="D:/workspace-sts-3.9.2.RELEASE/spring-mybatisGenerator/src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 选择一个table来生成相关文件
tableName(必要):要生成对象的表名;
domainObjectName 给表对应的 model 起名字 注意:大小写敏感问题。 -->
<table schema="smartcity" tableName="T_REPORT_LOG" domainObjectName="ReportLog"></table>
<!-- <table schema="smartcity" tableName="T_AIR" domainObjectName="Air"></table> -->
<!-- <table tableName="T_test" domainObjectName="Tole"></table> -->
<!--用来修改表中某个列的属性,一个table元素中可以有多个columnOverride元素哈.
property属性来指定列要生成的属性名称. -->
<!--<columnOverride column="username" property="userName" />-->
</context>
</generatorConfiguration>
3、在src/main/java目录下新建一个java文件,如Mybatisgenerator.java,内容如下:
package com.example.test;
import org.mybatis.generator.api.ShellRunner;
public class MybatisGenerator {
public static void main(String[] args) {
// TODO Auto-generated method stub
args = new String[] { "-configfile", "src\\main\\resources\\dbtools\\dbConfig.xml", "-overwrite" };
ShellRunner.main(args);
}
}
4、点击java文件,采用Java Application运行该项目,大功告成,其中路径等在dbConfig.xml中进行配置就可以
网友评论