Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -427,7 +428,7 @@ private void writeFile(File file, String content, String fileEncoding) throws IO
if (fileEncoding == null) {
osw = new OutputStreamWriter(fos);
} else {
osw = new OutputStreamWriter(fos, fileEncoding);
osw = new OutputStreamWriter(fos, Charset.forName(fileEncoding));
}

try (BufferedWriter bw = new BufferedWriter(osw)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ private PropertyRegistry() {}
public static final String COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS = "suppressAllComments"; //$NON-NLS-1$
public static final String COMMENT_GENERATOR_ADD_REMARK_COMMENTS = "addRemarkComments"; //$NON-NLS-1$
public static final String COMMENT_GENERATOR_DATE_FORMAT = "dateFormat"; //$NON-NLS-1$

public static final String COLUMN_OVERRIDE_FORCE_JAVA_TYPE = "forceJavaTypeIntoMapping"; //$NON-NLS-1$
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.JavaBeansUtil;
import org.mybatis.generator.internal.util.StringUtility;
import org.mybatis.generator.internal.util.messages.Messages;
Expand Down Expand Up @@ -114,7 +115,15 @@ private Field calculateTableDefinition(TopLevelClass topLevelClass) {
private void handleColumn(TopLevelClass topLevelClass, InnerClass innerClass,
IntrospectedColumn column, String tableFieldName) {
topLevelClass.addImportedType(column.getFullyQualifiedJavaType());
FullyQualifiedJavaType fieldType = calculateFieldType(column);

FullyQualifiedJavaType javaType;
if (column.getFullyQualifiedJavaType().isPrimitive()) {
javaType = column.getFullyQualifiedJavaType().getPrimitiveTypeWrapper();
} else {
javaType = column.getFullyQualifiedJavaType();
}

FullyQualifiedJavaType fieldType = calculateFieldType(javaType);
String fieldName = column.getJavaProperty();

if (fieldName.equals(tableFieldName)) {
Expand All @@ -137,21 +146,15 @@ private void handleColumn(TopLevelClass topLevelClass, InnerClass innerClass,
Field field = new Field(fieldName, fieldType);
field.setVisibility(JavaVisibility.PUBLIC);
field.setFinal(true);
field.setInitializationString(calculateInnerInitializationString(column));
field.setInitializationString(calculateInnerInitializationString(column, javaType));
innerClass.addField(field);
}

private FullyQualifiedJavaType calculateFieldType(IntrospectedColumn column) {
FullyQualifiedJavaType typeParameter;
if (column.getFullyQualifiedJavaType().isPrimitive()) {
typeParameter = column.getFullyQualifiedJavaType().getPrimitiveTypeWrapper();
} else {
typeParameter = column.getFullyQualifiedJavaType();
}
return new FullyQualifiedJavaType(String.format("SqlColumn<%s>", typeParameter.getShortName())); //$NON-NLS-1$
private FullyQualifiedJavaType calculateFieldType(FullyQualifiedJavaType javaType) {
return new FullyQualifiedJavaType(String.format("SqlColumn<%s>", javaType.getShortName())); //$NON-NLS-1$
}

private String calculateInnerInitializationString(IntrospectedColumn column) {
private String calculateInnerInitializationString(IntrospectedColumn column, FullyQualifiedJavaType javaType) {
StringBuilder initializationString = new StringBuilder();

initializationString.append(String.format("column(\"%s\", JDBCType.%s", //$NON-NLS-1$ //$NON-NLS-2$
Expand All @@ -161,7 +164,14 @@ private String calculateInnerInitializationString(IntrospectedColumn column) {
if (StringUtility.stringHasValue(column.getTypeHandler())) {
initializationString.append(String.format(", \"%s\")", column.getTypeHandler())); //$NON-NLS-1$
} else {
initializationString.append(')');
initializationString.append(')'); //$NON-NLS-1$
}


if (StringUtility.isTrue(column.getProperties().getProperty(PropertyRegistry.COLUMN_OVERRIDE_FORCE_JAVA_TYPE))) {
initializationString.append(".withJavaType("); //$NON-NLS-1$
initializationString.append(javaType.getShortName());
initializationString.append(".class)"); //$NON-NLS-1$
}

return initializationString.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.mybatis.generator.api.dom.kotlin.KotlinProperty;
import org.mybatis.generator.api.dom.kotlin.KotlinType;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.JavaBeansUtil;
import org.mybatis.generator.internal.util.StringUtility;
import org.mybatis.generator.internal.util.messages.Messages;
Expand Down Expand Up @@ -181,11 +182,17 @@ private String calculateInnerInitializationString(IntrospectedColumn column, Ful

if (StringUtility.stringHasValue(column.getTypeHandler())) {
initializationString.append(
String.format(", typeHandler = \"%s\")", column.getTypeHandler())); //$NON-NLS-1$
} else {
initializationString.append(')');
String.format(", typeHandler = \"%s\"", column.getTypeHandler())); //$NON-NLS-1$
}

if (StringUtility.isTrue(
column.getProperties().getProperty(PropertyRegistry.COLUMN_OVERRIDE_FORCE_JAVA_TYPE))) {
initializationString.append(
String.format(", javaType = %s::class", kt.getShortNameWithoutTypeArguments())); //$NON-NLS-1$
}

initializationString.append(')'); //$NON-NLS-1$

return initializationString.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2006-2018 the original author or authors.
Copyright 2006-2022 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -132,21 +132,32 @@ specified with the <a href="property.html">&lt;property&gt;</a> child element:</
<th>Property Values</th>
</tr>
<tr>
<td valign="top">trimStrings</td>
<td valign="top">forceJavaTypeIntoMapping</td>
<td>
This property is used to select whether MyBatis Generator adds code to trim
the white space from character fields returned from the database.
This can be useful if your database stores data in CHAR fields rather than
VARCHAR fields. When true for a character field/column, MyBatis Generator will
insert code to trim leading and trailing whitespace.
This property value overrides the property if specified at the
<a href="table.html">&lt;javaModelGenerator&gt;</a> and/or
<a href="javaModelGenerator.html">&lt;table&gt;</a> level.

<p><i>The default value is inherited from the
<a href="table.html">&lt;javaModelGenerator&gt;</a> and/or
<a href="javaModelGenerator.html">&lt;javaModelGenerator&gt;</a>, otherwise false.</i></p></td>
When true, this property will add the Java type to the generated mappings. This is normally not necessary.
However some functions will need it such as when you use MyBatis' EnumOrdinalTypeHandler.

<p>This property is only recognized, and only needed, by the MyBatis3DynamicSQL and MyBatis3Kotlin runtimes.</p>
<p><i>The default value is false</i></p>
<p>Since version 1.4.1</p>
</td>
</tr>
<tr>
<td valign="top">trimStrings</td>
<td>
This property is used to select whether MyBatis Generator adds code to trim
the white space from character fields returned from the database.
This can be useful if your database stores data in CHAR fields rather than
VARCHAR fields. When true for a character field/column, MyBatis Generator will
insert code to trim leading and trailing whitespace.
This property value overrides the property if specified at the
<a href="table.html">&lt;javaModelGenerator&gt;</a> and/or
<a href="javaModelGenerator.html">&lt;table&gt;</a> level.

<p><i>The default value is inherited from the
<a href="table.html">&lt;javaModelGenerator&gt;</a> and/or
<a href="javaModelGenerator.html">&lt;javaModelGenerator&gt;</a>, otherwise false.</i></p></td>
</tr>
</table>
</body>
</html>
32 changes: 19 additions & 13 deletions core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
<h1>What's New in MyBatis Generator</h1>
<h2>Version 1.4.1</h2>
<p>This release is primarily focused on updating the runtimes for MyBatis Dynamic SQL ("MyBatis3DynamicSQL" and
"MyBatis3Kotlin"). The generated Java code is now dependent on MyBatis Dynamic SQL version 1.3.1 or later. The
generated Kotlin code is now dependent on MyBatis Dynamic SQL version 1.4.0 or later. See below for details about these
changes. See the GitHub page for milestone 1.4.1 for details other changes in this release:
"MyBatis3Kotlin"). The generated code for "MyBatis3DynamicSQL" is now dependent on MyBatis Dynamic SQL version 1.3.1 or
later. The generated code for "MyBatis3Kotlin" is now dependent on MyBatis Dynamic SQL version 1.4.0 or later. See
below for details about these changes. See the GitHub page for milestone 1.4.1 for details other changes in this
release:
<a target="_blank" href="https://github.com/mybatis/generator/issues?q=milestone%3A1.4.1">Milestone 1.4.1</a>.</p>

<h3>Updated MyBatis Dynamic SQL Runtimes</h3>
Expand Down Expand Up @@ -83,15 +84,16 @@ reference. For example, if you have a table named "Bar", you may see code like t
val bar = Bar(1, 2) // remove "Record"
</pre>

<p>Additionally, generated support classes now declare an instance of the <code>SqlTable</code> object, and all fields,
as values in the top level object. If you wrote custom code that used these generated objects, two changes will be
required:</p>
<p>Additionally, generated support classes now declare an instance of the <code>AliasableSqlTable</code> object,
and all fields, as values in the top level object. If you wrote custom code that used these generated objects, two
changes will be required:</p>

<ul>
<li>The declared <code>SqlTable</code> object is now an instance of an inner Class rather than an inner Object.
It should be referenced with the new name (typically the same name as previously, with the first letter now
lowercase)</li>
<li>References to any <code>SqlColumn</code> should reference the top level value rather than a nested value</li>
<li>The declared <code>AliasableSqlTable</code> object is now an instance of an inner Class rather than an inner
Object. It should be referenced with the new name (typically the same name as previously, with the first letter
now lowercase)</li>
<li>References to any <code>AliasableSqlColumn</code> should reference the top level value rather than a nested
value</li>
</ul>

<p>For example, previously code might have looked like this:</p>
Expand All @@ -116,9 +118,9 @@ required:</p>
}
</pre>

<p>Lastly, the generated code uses upgraded DSL features that should be mostly transparent. But you will notice that any
"where" clauses you write will likely have deprecation warnings. This is due to a change in MyBatis Dynamic SQL. You
can read about remediation of these warnings in the documentation for that project.
<p>Lastly, the generated Kotlin code uses upgraded DSL features that should be mostly transparent. But you will notice
that any "where" clauses you write will likely have deprecation warnings. This is due to a change in MyBatis
Dynamic SQL. You can read about remediation of these warnings in the documentation for that project.
</p>

<h3>Other Enhancements</h3>
Expand All @@ -129,6 +131,10 @@ required:</p>
<li>Added a plugin to generate @CacheNamespace annotations</li>
<li>Added the ability to specify a name for the inner class generated in the MyBatis Dynamic SQL support class</li>
<li>Added plugin methods that allow generation of any arbitrary file type</li>
<li>Added a property to &lt;columnOverride&gt; that forces the Java type into generated mappings for the
MyBatis Dynamic SQL based runtimes. This is useful in certain cases such as when you use MyBatis'
EnumOrdinalTypeHandler.
</li>
</ul>

<h3>Removed Items</h3>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2006-2018 the original author or authors.
-- Copyright 2006-2022 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@ drop table mbgtest.Translations if exists;
drop table CompoundKey if exists;
drop schema mbgtest if exists;
drop table EnumTest if exists;
drop table EnumOrdinalTest if exists;
drop table GeneratedAlwaysTest if exists;
drop table GeneratedAlwaysTestNoUpdates if exists;
drop table IgnoreManyColumns if exists;
Expand Down Expand Up @@ -155,6 +156,12 @@ create table EnumTest (
primary key(id)
);

create table EnumOrdinalTest (
id int not null,
name int not null,
primary key(id)
);

create table GeneratedAlwaysTest (
id int not null,
name varchar(20) not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
<table tableName="EnumTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum"/>
</table>
<table tableName="EnumOrdinalTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum" jdbcType="INTEGER"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" >
<property name="forceJavaTypeIntoMapping" value="true" />
</columnOverride>
</table>
<table tableName="GeneratedAlwaysTest" >
<columnOverride column="ID_PLUS1" isGeneratedAlways="true" />
<columnOverride column="ID_PLUS2" isGeneratedAlways="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2006-2020 the original author or authors.
Copyright 2006-2022 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -230,6 +230,10 @@
<table tableName="EnumTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum"/>
</table>
<table tableName="EnumOrdinalTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum" jdbcType="INTEGER"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" />
</table>
<table tableName="GeneratedAlwaysTest" >
<columnOverride column="ID_PLUS1" isGeneratedAlways="true" />
<columnOverride column="ID_PLUS2" isGeneratedAlways="true" />
Expand Down Expand Up @@ -546,6 +550,10 @@
<table tableName="EnumTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum"/>
</table>
<table tableName="EnumOrdinalTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum" jdbcType="INTEGER"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" />
</table>
<table tableName="GeneratedAlwaysTest" >
<columnOverride column="ID_PLUS1" isGeneratedAlways="true" />
<columnOverride column="ID_PLUS2" isGeneratedAlways="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<table tableName="EnumTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum"/>
</table>
<table tableName="EnumOrdinalTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum" jdbcType="INTEGER"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" >
<property name="forceJavaTypeIntoMapping" value="true" />
</columnOverride>
</table>
<table tableName="GeneratedAlwaysTest" >
<columnOverride column="ID_PLUS1" isGeneratedAlways="true" />
<columnOverride column="ID_PLUS2" isGeneratedAlways="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2006-2018 the original author or authors.
-- Copyright 2006-2022 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@ drop table mbgtest.Translations if exists;
drop table CompoundKey if exists;
drop schema mbgtest if exists;
drop table EnumTest if exists;
drop table EnumOrdinalTest if exists;
drop table GeneratedAlwaysTest if exists;
drop table GeneratedAlwaysTestNoUpdates if exists;
drop table IgnoreManyColumns if exists;
Expand Down Expand Up @@ -155,6 +156,12 @@ create table EnumTest (
primary key(id)
);

create table EnumOrdinalTest (
id int not null,
name int not null,
primary key(id)
);

create table GeneratedAlwaysTest (
id int not null,
name varchar(20) not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
<table tableName="EnumTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum"/>
</table>
<table tableName="EnumOrdinalTest">
<columnOverride column="name" javaType="mbg.test.mb3.common.TestEnum" jdbcType="INTEGER"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" >
<property name="forceJavaTypeIntoMapping" value="true" />
</columnOverride>
</table>
<table tableName="GeneratedAlwaysTest" >
<columnOverride column="ID_PLUS1" isGeneratedAlways="true" />
<columnOverride column="ID_PLUS2" isGeneratedAlways="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,7 @@ abstract class AbstractAnnotatedMiscellaneousTest {
val environment = Environment("test", JdbcTransactionFactory(), ds)
val config = Configuration(environment)
config.addMapper(EnumtestMapper::class.java)
config.addMapper(EnumordinaltestMapper::class.java)
config.addMapper(GeneratedalwaystestMapper::class.java)
config.addMapper(GeneratedalwaystestnoupdatesMapper::class.java)
config.addMapper(MyObjectMapper::class.java)
Expand Down
Loading