1.我用的是idea开发工具,做的是一个教育系统SSM结构,需要实现一些简单地增删改查的接口,我同事搭建项目,就是用了mybatis-generator生成器,因为用起来很方便所以推荐给大家。
第一步:pom.xml配置 添加依赖**
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
**第二部:generatorConfig.xml文件****在Resources目录下写
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?><properties resource="config/config.properties"/>
<context id="DB2Tables" defaultModelType="flat" targetRuntime="MyBatis3">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 生成的bean带toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!--去掉生成的注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.jdbcUrl}"
userId="${jdbc.user}"
password="${jdbc.password}"/>
<!-- java类型处理器
用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl;
注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型;
-->
<javaTypeResolver>
<!--
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>
<!--实体类-->
<javaModelGenerator targetPackage="com.jxjy.model" targetProject=".\src\main\java">
<!--在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false-->
<property name="enableSubPackages" value="true"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--mapper.xml文件-->
<sqlMapGenerator targetPackage="mapper" 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,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.jxjy.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--<table tableName="danmu">-->
<!--<columnOverride column="is_deleted">-->
<!--<property name="property" value="deleted"/>-->
<!--</columnOverride>-->
<!--</table>-->
<!-- <table tableName="sys_permission"/>
<table tableName="sys_role_permission"/>-->
<table tableName="sys_njb"/>
<table tableName="sys_bjb"/>
<table tableName="sys_jsb"/>
<table tableName="sys_kclb" schema="" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<!--<generatedKey column="userId" sqlStatement="Mysql" identity="true"/>-->
</table>
<!--<table schema="seckill" tableName="ALLTYPES" domainObjectName="Customer" >-->
<!--<property name="useActualColumnNames" value="true"/>-->
<!--<generatedKey column="ID" sqlStatement="DB2" identity="true" />-->
<!--<columnOverride column="DATE_FIELD" property="startDate" />-->
<!--<ignoreColumn column="FRED" />-->
<!--<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
<!--</table>-->
</context>
注: 这种生成的,包含实体对象,也包含实体对象Example对象
mybatisGenatator.java
package com.jxjy.common;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/
- Mybatis-Genetator,在generator-config中配置好要生成文件的数据表然后执行这里
- Copyright© 2019-08-15
- Author: mzh
- Date: 2019/08/15 11:04
/
public class MybatisGenerator {
/*-
mybatis-generator自动生成
-
@param args
-
@throws IOException
-
@throws XMLParserException
-
@throws InvalidConfigurationException
-
@throws SQLException
-
@throws InterruptedException
*/
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
///H:/java_world/seckill/target/classes/
System.out.println(MybatisGenerator.class.getClass().getResource("/").getPath());
generator();
}
private static void generator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List warnings = new ArrayList();
boolean overwrite = true;File configFile = new File(MybatisGenerator.class.getClass().getResource("/").getPath()+ “/config/generator-config.xml”);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
第四部:直接启动mybatisGenerator类,就可以生成了,
** 学没学会**
刚开始用的时候还出现了一个BUG,加载xml文件的时候就跑到c盘去了,当时也是求助了一位大神帮忙解决。
结束语:当然配置mybatis-generator方法有很多种,今天的这一种也比较简单,希望对你有帮助,下期有时间在写一些在做项目中遇到的问题。
–李某某
2019.08.22晚23点。
-