Provides the main classes and interfaces used by clients of MyBatis Generator.
24 | 25 | 26 | -------------------------------------------------------------------------------- /java-generator-core/src/main/java/org/mybatis/generator/codegen/AbstractGenerator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2006-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.generator.codegen; 17 | 18 | import java.util.List; 19 | 20 | import org.mybatis.generator.api.IntrospectedTable; 21 | import org.mybatis.generator.api.ProgressCallback; 22 | import org.mybatis.generator.config.Context; 23 | 24 | /** 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public abstract class AbstractGenerator { 30 | protected Context context; 31 | protected IntrospectedTable introspectedTable; 32 | protected ListThe <except> element is used to specify an exception to a set of columns 29 | that are ignored by a regular expression in a 30 | <ignoreColumnsByRegex> element. 31 | This allows you to specify a very general 32 | regular expression to ignore a large set of columns, then add columns back into 33 | the set of columns used for code generation.
34 |For example, if you specify a regular
35 | expression like ".*
" then MBG will ignore every column in a table.
36 | But if you add exceptions, you can add a few columns back.
37 | This element is an optional child
38 | element of the <ignoreColumnsByRegex> element.
Attribute | 44 |Description | 45 |
---|---|
column | 48 |The column name of the exception column. | 49 |
Attribute | 56 |Description | 57 |
---|---|
delimitedColumnName | 60 |If true then MBG will perform a case-sensitive exact
61 | match when matching against returned columns from the database. If
62 | false (default) then the name is considered case-insensitive. |
63 |
None
68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/configreference/generatorConfiguration.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |The <generatorConfiguration> element is the root element of a 29 | MyBatis Generator configuration file. The file should contain the following DOCTYPE: 30 |
31 |32 | <!DOCTYPE generatorConfiguration PUBLIC 33 | "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 34 | "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 35 |36 | 37 |
None
39 | 40 |None
42 | 43 |The <ignoreColumn> element is used to tell MyBatis Generator (MBG) to ignore a column 29 | in an introspected table. No generated SQL will refer to the column, and no 30 | property will be generated for the column in the model objects. 31 | This element is an optional child 32 | element of the <table> element.
33 | 34 |Attribute | 38 |Description | 39 |
---|---|
column | 42 |The column name of the ignored column. | 43 |
Attribute | 50 |Description | 51 |
---|---|
delimitedColumnName | 54 |If true then MBG will perform a case-sensitive exact
55 | match when matching against returned columns from the database. If
56 | false (default) then the name is considered case-insensitive. |
57 |
None
62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/configreference/ignoreColumnsByRegex.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |The <ignoreColumnsByRegex> element is used to tell MyBatis Generator (MBG) to ignore a 29 | set of columns in an introspected table - the set is determined by matching against a regular 30 | expression specified in this element. No generated SQL will refer to the ignored columns, and no 31 | properties will be generated for the ignored columns in the model objects. 32 | This element is an optional child 33 | element of the <table> element.
34 |You can specify exceptions to the pattern through use of an 35 | <except> element.
36 |Attribute | 40 |Description | 41 |
---|---|
pattern | 44 |The regular expression used to match column names. The regular expression
45 | engine is the standard java.util.regex.Pattern engine. |
46 |
None
51 | 52 |This example tells MyBatis to ignore every column in the Foo table that begins with 59 | the characters "col" (case-insensitive) except for "col01" and "col13".
60 |61 | <table tableName="Foo"> 62 | <ignoreColumnsByRegex pattern="(?i)col.*"> 63 | <except column="col01"/> 64 | <except column="col13"/> 65 | </ignoreColumnsByRegex> 66 | </table> 67 |68 | 69 | 70 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/configreference/plugin.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |
The <plugin> element is used to define a plugin. Plugins can be used 29 | to extend or modify the code generated by MyBatis Generator (MBG). This element is a child element of the 30 | <context> element. Any number of 31 | plugins may be specified in a context. The plugins will be called in 32 | the order that are listed in the configuration.
33 | 34 |For more information about implementing plugins, see the 35 | Implementing Plugins reference 36 | page.
37 | 38 |For more information about plugins supplied with MyBatis Generator, see the 39 | Supplied Plugins reference 40 | page.
41 | 42 |Attribute | 46 |Description | 47 |
---|---|
type | 50 |The fully qualified name of the class that implements the plugin.
51 | The class must implement the interface
52 | org.mybatis.generator.api.Plugin ,
53 | and must have a public default constructor. Note that it is far
54 | easier to extend the adapter class
55 | org.mybatis.generator.api.PluginAdapter
56 | than to implement the entire interface. |
57 |
None
62 | 63 |The <property> element is used to specify properties for many of the 29 | other elements. The documentation page for each element that supports the 30 | property element lists the different values that are valid. This element 31 | can also be used to pass properties to any custom code generators that 32 | you implement.
33 | 34 |Attribute | 38 |Description | 39 |
---|---|
name | 42 |The name of the property (case sensitive). | 43 |
value | 46 |The value of the property (typically case insensitive). | 47 |
None
52 | 53 |None
55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/generatedobjects/results.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |MyBatis Generator (MBG) generates these types of objects:
29 |The individual pages describe these objects, and their usage.
43 | 44 | 45 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/license.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |Licensed under the Apache License, Version 2.0 (the "License"); 29 | you may not use this product except in compliance with the License. 30 | You may obtain a copy of the License at 31 |
32 |33 | http://www.apache.org/licenses/LICENSE-2.0
34 |35 | Unless required by applicable law or agreed to in writing, software 36 | distributed under the License is distributed on an "AS IS" BASIS, 37 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 38 | See the License for the specific language governing permissions and 39 | limitations under the License. 40 |
41 | 42 |This product includes software developed by the Apache Software 43 | Foundation (http://www.apache.org/).
44 | 45 |This product includes the EqualsUtil
and HashCodeUtil
classes
46 | from http://www.javapractices.com.
This section collects useful information related to technical topics 29 | with MyBatis Generator.
30 | 31 |By default MyBatis Generator will map LONG VARCHAR fields to the java.lang.String
data type,
30 | and will mark these fields as jdbcType="LONGVARCHAR"
. This
31 | mapping will cause errors when retrieving data from DB2. DB2 LONG VARCHAR fields should be mapped
32 | to the java.lang.String
data type with jdbcType="VARCHAR"
. To overcome
33 | this problem, use a <columnOverride>
as follows:
35 | <table schema="DB2ADMIN" tableName="ALLTYPES" > 36 | <columnOverride column="LONG_VARCHAR_FIELD" javaType="java.lang.String" jdbcType="VARCHAR" /> 37 | </table> 38 |39 | 40 | 41 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/usage/intro.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |
This section collects usage information for different databases.
29 | 30 |Please let us know about any quirks you discover for the database you are using - we will 37 | be happy to add the information 38 | to this section for future reference.
39 | 40 | 41 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/usage/mysql.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |MySql supports both signed, and unsigned, numeric fields. These are not JDBC types, so MyBatis
30 | generator cannot automatically account for these types of fields. The Java data types are always signed.
31 | This can
32 | lead to a loss of precision when using unsigned fields. The solution is to provide a
33 | <columnOverride>
for any unsigned numeric field in MySql. Here is an example
34 | of how to deal with an unsigned bigint field in MySql:
36 | <table tableName="ALLTYPES" > 37 | <columnOverride column="UNSIGNED_BIGINT_FIELD" javaType="java.lang.Object" jdbcType="LONG" /> 38 | </table> 39 |40 |
You will have to cast the returned value to the appropriate type yourself (in this case,
41 | java.math.BigInteger
).
MySql does not properly support SQL catalogs and schema. If you run the create schema
44 | command in Mysql, it actually creates a database - and the JDBC driver reports it back as a catalog.
45 | But MySql syntax does not support the standard catalog..table
SQL syntax.
For this reason, it is best to not specify either catalog or schema in generator configurations. 47 | Just specify table names and specify the database in the JDBC URL.
48 | 49 | 50 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/usage/oracle.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |If you want to generate objects for a table that has a public synonym, you 30 | should actually generate the objects against the real table - and then change the 31 | table name at runtime. MyBatis Generator supports this automatically.
32 |For example, assume there is a public alias "FRED" that points to the 33 | table "HR.EMPLOYEES". The following table configuration will generate the objects 34 | based on HR.EMPLOYEES, but the runtime SQL will only refer to FRED:
35 |36 | <table schema="HR" tableName="EMPLOYEES"> 37 | <property name="ignoreQualifiersAtRuntime" value="true" /> 38 | <property name="runtimeTableName" value="FRED" /> 39 | </table> 40 |41 |
The Oracle JDBC driver will report that a LONG column is JDBC type LONGVARCHAR. 43 | MyBatis will map LONGVARCHAR to CLOB which is unsupported by the Oracle driver. 44 | So, when using LONG datatype, you should do a column override in the generator 45 | to map it to VARCHAR JDBC type.
46 | 47 | 48 | -------------------------------------------------------------------------------- /java-generator-core/src/site/xhtml/usage/postgresql.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 |PostgreSQL is case sensitive with regards to all database identifiers 30 | (table names, schema names, column names, etc.) In addition, PostgreSQL 31 | has a distinct preference for all identifiers being in all lower case letters. 32 | If you use all lower case identifiers for PostgreSQL, then MyBatis Generator will find tables 33 | and write correct SQL with no additional consideration. If you used mixed cased, 34 | or upper case, identifiers you will 35 | need to configure MyBatis Generator appropriately:
36 |delimitIdentifiers
option if your table or schema
38 | is mixed case or all upper case.<columnOverride>
element to specifically delimit the column,
41 | or you could specify the delimitAllColumns
attribute
42 | to delimit all column names.Examples:
45 |46 | <table schema="HR" tableName="Employees" 47 | delimitIdentifiers="true" delimitAllColumns="true"/> 48 |49 |
Or...
50 |51 | <table schema="HR" tableName="Employees" delimitIdentifiers="true" > 52 | <columnOverride column="EmployeeId" delimitedColumnName="true" /> 53 | <columnOverride column="EmployeeName" delimitedColumnName="true" /> 54 | </table> 55 |56 | 57 | 58 | -------------------------------------------------------------------------------- /java-generator-core/src/test/java/org/mybatis/generator/api/GeneratedJavaFileTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2006-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.generator.api; 17 | 18 | import static org.junit.Assert.*; 19 | import org.junit.Test; 20 | import org.mybatis.generator.api.dom.DefaultJavaFormatter; 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | 24 | public class GeneratedJavaFileTest { 25 | 26 | @Test 27 | public void testReqularInterface() { 28 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("org.mybatis.test.TestInterface"); 29 | Interface ifc = new Interface(fqjt); 30 | JavaFormatter jf = new DefaultJavaFormatter(); 31 | GeneratedJavaFile gjf = new GeneratedJavaFile(ifc, "src", jf); 32 | 33 | assertEquals("TestInterface.java", gjf.getFileName()); 34 | assertEquals("org.mybatis.test", gjf.getTargetPackage()); 35 | } 36 | 37 | @Test 38 | public void testGenericInterface() { 39 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("org.mybatis.test.TestInterface"); 40 | fqjt.addTypeArgument(new FullyQualifiedJavaType("T")); 41 | Interface ifc = new Interface(fqjt); 42 | JavaFormatter jf = new DefaultJavaFormatter(); 43 | GeneratedJavaFile gjf = new GeneratedJavaFile(ifc, "src", jf); 44 | 45 | assertEquals("TestInterface.java", gjf.getFileName()); 46 | assertEquals("org.mybatis.test", gjf.getTargetPackage()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /java-generator-core/src/test/java/org/mybatis/generator/internal/util/StringUtilityTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2006-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.generator.internal.util; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import org.junit.Test; 21 | 22 | public class StringUtilityTest { 23 | 24 | @Test 25 | public void testNoCatalog() { 26 | String answer = StringUtility.composeFullyQualifiedTableName(null, "schema", "table", '.'); 27 | assertEquals("schema.table", answer); 28 | } 29 | 30 | @Test 31 | public void testNoSchema() { 32 | String answer = StringUtility.composeFullyQualifiedTableName("catalog", null, "table", '.'); 33 | assertEquals("catalog..table", answer); 34 | } 35 | 36 | @Test 37 | public void testAllPresent() { 38 | String answer = StringUtility.composeFullyQualifiedTableName("catalog", "schema", "table", '.'); 39 | assertEquals("catalog.schema.table", answer); 40 | } 41 | 42 | @Test 43 | public void testTableOnly() { 44 | String answer = StringUtility.composeFullyQualifiedTableName(null, null, "table", '.'); 45 | assertEquals("table", answer); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /java-generator-core/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2006-2016 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # log4j.properties for 18 | 19 | # Set root category priority to ERROR and its only appender to LOGFILE. 20 | log4j.rootCategory=ERROR, CONSOLE 21 | 22 | #Spring Framework 23 | #log4j.logger.org.springframework=DEBUG 24 | #log4j.logger.org.springframework.orm=DEBUG 25 | #log4j.logger.org.springframework.transaction=DEBUG 26 | 27 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 28 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 29 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 30 | log4j.appender.CONSOLE.layout.ConversionPattern=%-d{HH:mm:ss} %-5p %60.60c %-25.25M %m%n 31 | -------------------------------------------------------------------------------- /java-generator-core/src/test/resources/scripts/database.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2006-2016 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | database.url=jdbc:hsqldb:mem:aname 18 | -------------------------------------------------------------------------------- /java-generator-maven-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |