mybatis 代码生成器
依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency>
|
主类
MybatisGen.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package cn.zl;
import org.mybatis.generator.api.ShellRunner;
import java.io.File;
public class MybatisGen { public static void main(String[] args) { String type = args[0]; boolean isDelete = false;
if (args.length > 1 && "true".equals(args[1])) { isDelete = true; } gen(type, isDelete); }
public static void gen(String type, Boolean isDelete) { String sepa = File.separator; File xmlPath = new File("." + sepa + "src" + sepa + "main" + sepa + "resources" + sepa + "cn" + sepa + "zl" + sepa + "sqlmap"); System.out.println(xmlPath.getAbsolutePath()); if (xmlPath.exists() && isDelete) { System.out.println("开始删除xml文件"); boolean b = deleteDir(xmlPath); if (!b) { return; } } String config = MybatisGen.class.getClassLoader().getResource(type + "/generatorConfig.xml").getFile(); String[] arg = {"-configfile", config, "-overwrite"}; ShellRunner.main(arg); }
private static boolean deleteDir(File dir) {
if (dir.isDirectory()) { String[] children = dir.list(); if (children == null) { return true; } for (String aChildren : children) { boolean success = deleteDir(new File(dir, aChildren)); if (!success) { return false; } } } System.out.println("删除xml:" + dir.getName()); return dir.delete(); }
}
|
配置文件
- generatorConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| <?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>
<properties resource="MybatisConfig.properties"/>
<classPathEntry location="${jdbc.path}"/>
<context id="MySQLTables" targetRuntime="MyBatis3" defaultModelType="conditional">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin"> <property name="fileName" value="sqlMapperConfig.xml"/> <property name="targetPackage" value="mybatis"/> <property name="targetProject" value="src/main/resources"/> </plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
<commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator>
<jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection>
<javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver>
<javaModelGenerator targetPackage="${mdb.javaModelGenerator.targetPackage}" targetProject="${mdb.javaModelGenerator.targetProject}">
<property name="constructorBased" value="false"/>
<property name="enableSubPackages" value="false"/>
<property name="immutable" value="false"/>
<property name="trimStrings" value="true"/> </javaModelGenerator>
<sqlMapGenerator targetPackage="${mdb.sqlMapGenerator.targetPackage}" targetProject="${mdb.sqlMapGenerator.targetProject}"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="${mdb.javaClientGenerator.targetPackage}" targetProject="${mdb.javaClientGenerator.targetProject}"> <property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table schema="${jdbc.username}" tableName="pay_order" domainObjectName="PayOrder"> <property name="constructorBased" value="false"/>
<property name="ignoreQualifiersAtRuntime" value="false"/>
<property name="immutable" value="false"/>
<property name="modelOnly" value="false"/>
<property name="useActualColumnNames" value="false"/>
</table>
</context> </generatorConfiguration>
|
- MybatisConfig.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| jdbc.path=D:/tools/apache-maven-3.2.5/repo/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.174.128:3306/dongwanpoc?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root jdbc.password=123456
mdb.javaModelGenerator.targetPackage=cn.zl.model mdb.javaModelGenerator.targetProject=src/main/java mdb.sqlMapGenerator.targetPackage=cn.zl.mapper mdb.sqlMapGenerator.targetProject=src/main/resources mdb.javaClientGenerator.type=XMLMAPPER mdb.javaClientGenerator.targetPackage=cn.zl.mapper mdb.javaClientGenerator.targetProject=src/main/java
|
oracle 注意点
- 类型对照表
Mybatis生成java文件时,数据库表字段类型与java类型对照表
NUMBER 长度 |
JAVA 类型 |
1-4 |
short |
5-9 |
integer |
10-18 |
long |
18+ |
bigDecimal |
生成器 类型转换 规则
详见 类
org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl