├── .gitignore ├── NOTICE ├── src ├── main │ ├── java │ │ └── org │ │ │ └── mybatis │ │ │ └── generator │ │ │ ├── api │ │ │ ├── package.html │ │ │ ├── dom │ │ │ │ ├── xml │ │ │ │ │ ├── Element.java │ │ │ │ │ ├── TextElement.java │ │ │ │ │ ├── Attribute.java │ │ │ │ │ └── Document.java │ │ │ │ └── java │ │ │ │ │ ├── JavaVisibility.java │ │ │ │ │ ├── CompilationUnit.java │ │ │ │ │ └── Parameter.java │ │ │ ├── VerboseProgressCallback.java │ │ │ ├── GeneratedXmlFile.java │ │ │ ├── GeneratedJavaFile.java │ │ │ └── GeneratedFile.java │ │ │ ├── update │ │ │ └── update.txt │ │ │ ├── logging │ │ │ ├── AbstractLogFactory.java │ │ │ ├── Log.java │ │ │ ├── Log4jImpl.java │ │ │ ├── JdkLoggingImpl.java │ │ │ └── LogFactory.java │ │ │ ├── codegen │ │ │ ├── AbstractXmlGenerator.java │ │ │ ├── ibatis2 │ │ │ │ ├── IntrospectedTableIbatis2Java5Impl.java │ │ │ │ ├── sqlmap │ │ │ │ │ └── elements │ │ │ │ │ │ ├── DeleteByExampleElementGenerator.java │ │ │ │ │ │ ├── BaseColumnListElementGenerator.java │ │ │ │ │ │ ├── BlobColumnListElementGenerator.java │ │ │ │ │ │ └── CountByExampleElementGenerator.java │ │ │ │ └── dao │ │ │ │ │ └── templates │ │ │ │ │ ├── SpringDAOTemplate.java │ │ │ │ │ └── IbatisDAOTemplate.java │ │ │ ├── mybatis3 │ │ │ │ ├── javamapper │ │ │ │ │ └── elements │ │ │ │ │ │ ├── sqlprovider │ │ │ │ │ │ ├── AbstractJavaProviderMethodGenerator.java │ │ │ │ │ │ ├── ProviderSelectByExampleWithBLOBsMethodGenerator.java │ │ │ │ │ │ └── ProviderUpdateByExampleWithBLOBsMethodGenerator.java │ │ │ │ │ │ ├── annotated │ │ │ │ │ │ ├── AnnotatedCountByExampleMethodGenerator.java │ │ │ │ │ │ ├── AnnotatedDeleteByExampleMethodGenerator.java │ │ │ │ │ │ ├── AnnotatedUpdateByExampleSelectiveMethodGenerator.java │ │ │ │ │ │ ├── AnnotatedUpdateByExampleWithBLOBsMethodGenerator.java │ │ │ │ │ │ ├── AnnotatedUpdateByExampleWithoutBLOBsMethodGenerator.java │ │ │ │ │ │ ├── AnnotatedUpdateByPrimaryKeySelectiveMethodGenerator.java │ │ │ │ │ │ └── AnnotatedInsertSelectiveMethodGenerator.java │ │ │ │ │ │ ├── InsertMethodGenerator.java │ │ │ │ │ │ ├── CountByExampleMethodGenerator.java │ │ │ │ │ │ ├── DeleteByExampleMethodGenerator.java │ │ │ │ │ │ ├── InsertSelectiveMethodGenerator.java │ │ │ │ │ │ ├── UpdateByPrimaryKeyWithoutBLOBsMethodGenerator.java │ │ │ │ │ │ ├── UpdateByPrimaryKeyWithBLOBsMethodGenerator.java │ │ │ │ │ │ ├── UpdateByPrimaryKeySelectiveMethodGenerator.java │ │ │ │ │ │ └── UpdateByExampleSelectiveMethodGenerator.java │ │ │ │ └── xmlmapper │ │ │ │ │ ├── MixedMapperGenerator.java │ │ │ │ │ └── elements │ │ │ │ │ ├── DeleteByExampleElementGenerator.java │ │ │ │ │ ├── CountByExampleElementGenerator.java │ │ │ │ │ ├── BaseColumnListElementGenerator.java │ │ │ │ │ └── BlobColumnListElementGenerator.java │ │ │ ├── AbstractJavaClientGenerator.java │ │ │ ├── AbstractGenerator.java │ │ │ └── XmlConstants.java │ │ │ ├── exception │ │ │ ├── InvalidConfigurationException.java │ │ │ ├── XMLParserException.java │ │ │ └── ShellException.java │ │ │ ├── config │ │ │ ├── MergeConstants.java │ │ │ ├── JavaTypeResolverConfiguration.java │ │ │ ├── CommentGeneratorConfiguration.java │ │ │ ├── TypedPropertyHolder.java │ │ │ ├── ModelType.java │ │ │ ├── PluginConfiguration.java │ │ │ ├── PropertyHolder.java │ │ │ ├── xml │ │ │ │ ├── ParserEntityResolver.java │ │ │ │ └── ParserErrorHandler.java │ │ │ ├── SqlMapGeneratorConfiguration.java │ │ │ └── JavaModelGeneratorConfiguration.java │ │ │ ├── ant │ │ │ └── AntProgressCallback.java │ │ │ ├── internal │ │ │ ├── NullProgressCallback.java │ │ │ ├── rules │ │ │ │ ├── FlatModelRules.java │ │ │ │ ├── HierarchicalModelRules.java │ │ │ │ └── ConditionalModelRules.java │ │ │ ├── db │ │ │ │ └── ActualTableName.java │ │ │ └── util │ │ │ │ ├── EqualsUtil.java │ │ │ │ ├── ClassloaderUtility.java │ │ │ │ └── messages │ │ │ │ └── Messages.java │ │ │ └── plugins │ │ │ └── AnnotationPlugin.java │ ├── resources │ │ ├── log4j.properties │ │ ├── generatorConfigIbatis2.xml │ │ ├── grid.xml │ │ ├── mysql_bank.xml │ │ ├── ConfigIbatis_bank.xml │ │ └── ConfigIbatis_wht.xml │ └── assembly │ │ └── src.xml └── test │ ├── resources │ ├── log4j.properties │ ├── generatorConfigIbatis2.xml │ ├── grid.xml │ ├── grid1.xml │ ├── authority_mysql.xml │ └── datacheck_mysql.xml │ └── java │ ├── test │ └── _Ibator_Run_Test.java │ └── org │ └── mybatis │ └── generator │ └── MyBatisGeneratorTest.java ├── doc ├── html │ ├── index.html │ ├── reference │ │ ├── intro.html │ │ └── building.html │ ├── usage │ │ ├── intro.html │ │ ├── db2.html │ │ ├── oracle.html │ │ ├── mysql.html │ │ └── postgresql.html │ ├── generatedobjects │ │ └── results.html │ ├── configreference │ │ ├── generatorConfiguration.html │ │ ├── property.html │ │ ├── ignoreColumn.html │ │ ├── properties.html │ │ ├── classPathEntry.html │ │ └── jdbcConnection.html │ ├── license.html │ ├── running │ │ └── running.html │ └── migratingFromIbator.html └── todo.txt ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .springBeans 4 | .settings/ 5 | target/ 6 | logs/ 7 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | This product includes software developed by 2 | The Apache Software Foundation (http://www.apache.org/). 3 | 4 | This product includes the EqualsUtil and HashCodeUtil classes 5 | from http://www.javapractices.com. 6 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Package Description for Main MyBatis Generator API Classes 4 | 5 | 6 |

Provides the main classes and interfaces used by clients of MyBatis Generator.

7 | 8 | 9 | -------------------------------------------------------------------------------- /doc/html/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | MyBatis Generator User Manual 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # log4j.properties for 2 | 3 | # Set root category priority to ERROR and its only appender to LOGFILE. 4 | log4j.rootCategory=debug, CONSOLE 5 | 6 | #Spring Framework 7 | #log4j.logger.org.springframework=DEBUG 8 | #log4j.logger.org.springframework.orm=DEBUG 9 | #log4j.logger.org.springframework.transaction=DEBUG 10 | 11 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 12 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 13 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 14 | log4j.appender.CONSOLE.layout.ConversionPattern=%-d{HH:mm:ss} %-5p %60.60c %-25.25M %m%n 15 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # log4j.properties for 2 | 3 | # Set root category priority to ERROR and its only appender to LOGFILE. 4 | log4j.rootCategory=debug, CONSOLE 5 | 6 | #Spring Framework 7 | #log4j.logger.org.springframework=DEBUG 8 | #log4j.logger.org.springframework.orm=DEBUG 9 | #log4j.logger.org.springframework.transaction=DEBUG 10 | 11 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 12 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 13 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 14 | log4j.appender.CONSOLE.layout.ConversionPattern=%-d{HH:mm:ss} %-5p %60.60c %-25.25M %m%n 15 | -------------------------------------------------------------------------------- /doc/todo.txt: -------------------------------------------------------------------------------- 1 | Core: 2 | 3 | Future 4 | 5 | 1. Write page explaining how to use the GENERIC DAOs 6 | 2. Change the base API to allow specifying a connection at 7 | runtime, rather than requiring a connection in the 8 | XML config. 9 | 3. Relationships? 10 | 4. Java merger with ANTLR? 11 | 5. Add a way to override default types in the JavaTypeResolver 12 | 6. - to allow specification of common attributes for a set of 13 | tables 14 | 15 | 16 | 17 | Eclipse: 18 | 19 | Future 20 | 21 | 1. Write a launch configuration for the plugin to allow better 22 | control of the class path. 23 | 24 | 2. Get rid of the "Generate iBATIS Artifacts" popup menu - should 25 | only run with a launch configuration or with Ant. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | -------- 3 | MyBatis Generator (MBG) is a code generator for the MyBatis (and iBATIS) SQL 4 | mapping framefork. MBG will introspect database tables (through JDBC 5 | DatabaseMetaData) and generate SQL Map XML files, Java model object (POJOs) 6 | that match the table, and (optionally) Java client classes that use the other 7 | generated objects. 8 | 9 | For full documentation, please refer to the user's manual at 10 | doc/html/index.html in this distribution. 11 | 12 | # Dependencies 13 | ------------ 14 | There are no dependencies beyond the JRE. JRE 5.0 or above is required. 15 | Also required is a JDBC driver that implements the DatabaseMetaData interface, 16 | especially the "getColumns" and "getPrimaryKeys" methods. 17 | 18 | # Support 19 | ------- 20 | Support is provided through the user mailing list. Mail 21 | questions or bug reports to: 22 | 23 | mybatis-user@googlegroups.com 24 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/update/update.txt: -------------------------------------------------------------------------------- 1 | 1、更新 SerializablePlugin插件变量serialVersionUID的位置 topLevelClass.addField(0,field); 2 | 2、增加了数据库的返回注释,去掉了不必要的一些注释 3 | 3、注释所有的接口方法 4 | 4、为ibatis2增加了个 给dao增加注解的插件 5 | 5、增加公共查询条件类 ibatis 和mybatis 6 | 6、增加了service的生成插件 7 | 7、增加pojo类注释-》table的注释 8 | 8、默认不加入jdbcType属性,如:#{condition.myaccount,jdbcType=VARCHAR}或者 #condition.myaccount:VARCHAR# 9 | 如果需要使用,在即可 11 | 如: 12 | 13 | 9、增加了获取SQL Server表注释和字段注释的方法,仅测试过SQL Server 2000版本。其他版本不适用时,可以修改方法见: 14 | package org.mybatis.generator.internal.db; 15 | 这个下面的private String getTableRemark(String tableName) 方法和 16 | private String getClumnRemark(String tableName, String clumnName)方法 17 | 10、为每个pojo增加serialVersionUID 18 | 11、集中mysql和oracle的分页变量,都叫limit和start 19 | 12、新生成xml的时候,覆盖旧的xml this.isMergeable = isMergeable; 20 | 13、 -------------------------------------------------------------------------------- /src/main/assembly/src.xml: -------------------------------------------------------------------------------- 1 | 4 | bundle 5 | 6 | zip 7 | 8 | 9 | 10 | 11 | ${project.basedir}/README* 12 | ${project.basedir}/LICENSE* 13 | ${project.basedir}/NOTICE* 14 | ${project.basedir}/doc/**/* 15 | 16 | 17 | 18 | ${project.build.directory} 19 | lib 20 | 21 | *.jar 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/logging/AbstractLogFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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 | package org.mybatis.generator.logging; 18 | 19 | /** 20 | * Defines the interface for creating Log implementations. 21 | * 22 | * @author Jeff Butler 23 | * 24 | */ 25 | public interface AbstractLogFactory { 26 | Log getLog(Class aClass); 27 | } 28 | -------------------------------------------------------------------------------- /doc/html/reference/intro.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Reference Information 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Reference Information

19 |

This section collects useful information related to technical topics 20 | with MyBatis Generator.

21 | 22 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/xml/Element.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.xml; 17 | 18 | /** 19 | * @author Jeff Butler 20 | */ 21 | public abstract class Element { 22 | 23 | /** 24 | * 25 | */ 26 | public Element() { 27 | super(); 28 | } 29 | 30 | public abstract String getFormattedContent(int indentLevel); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/AbstractXmlGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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 org.mybatis.generator.api.dom.xml.Document; 19 | 20 | /** 21 | * 22 | * @author Jeff Butler 23 | * 24 | */ 25 | public abstract class AbstractXmlGenerator extends AbstractGenerator { 26 | public abstract Document getDocument(); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/logging/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.logging; 17 | 18 | /** 19 | * 20 | * @author Clinton Begin 21 | */ 22 | public interface Log { 23 | 24 | boolean isDebugEnabled(); 25 | 26 | void error(String s, Throwable e); 27 | 28 | void error(String s); 29 | 30 | public void debug(String s); 31 | 32 | public void warn(String s); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/IntrospectedTableIbatis2Java5Impl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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.ibatis2; 17 | 18 | /** 19 | * 20 | * @author Jeff Butler 21 | * 22 | */ 23 | public class IntrospectedTableIbatis2Java5Impl extends IntrospectedTableIbatis2Java2Impl { 24 | @Override 25 | public boolean isJava5Targeted() { 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /doc/html/usage/intro.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Usage Notes 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Usage Notes

19 |

This section collects usage information for different databases.

20 | 21 | 27 |

Please let us know about any quirks you discover for the database you are using - we will 28 | be happy to add the information 29 | to this section for future reference.

30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/VerboseProgressCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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 | package org.mybatis.generator.api; 18 | 19 | import org.mybatis.generator.internal.NullProgressCallback; 20 | 21 | /** 22 | * @author Jeff Butler 23 | * 24 | */ 25 | public class VerboseProgressCallback extends NullProgressCallback { 26 | 27 | /** 28 | * 29 | */ 30 | public VerboseProgressCallback() { 31 | super(); 32 | } 33 | 34 | @Override 35 | public void startTask(String taskName) { 36 | System.out.println(taskName); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/java/JavaVisibility.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.java; 17 | 18 | /** 19 | * Typesafe enum of possible Java visibility settings 20 | * 21 | * @author Jeff Butler 22 | */ 23 | public enum JavaVisibility { 24 | PUBLIC("public "), //$NON-NLS-1$ 25 | PRIVATE("private "), //$NON-NLS-1$ 26 | PROTECTED("protected "), //$NON-NLS-1$ 27 | DEFAULT(""); //$NON-NLS-1$ 28 | 29 | private String value; 30 | 31 | private JavaVisibility(String value) { 32 | this.value = value; 33 | } 34 | 35 | public String getValue() { 36 | return value; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/exception/InvalidConfigurationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.exception; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * @author Jeff Butler 22 | */ 23 | public class InvalidConfigurationException extends Exception { 24 | static final long serialVersionUID = 4902307610148543411L; 25 | 26 | private List errors; 27 | 28 | /** 29 | * 30 | */ 31 | public InvalidConfigurationException(List errors) { 32 | super(); 33 | this.errors = errors; 34 | } 35 | 36 | public List getErrors() { 37 | return errors; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/sqlprovider/AbstractJavaProviderMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.sqlprovider; 17 | 18 | import org.mybatis.generator.api.dom.java.TopLevelClass; 19 | import org.mybatis.generator.codegen.AbstractGenerator; 20 | 21 | /** 22 | * 23 | * @author Jeff Butler 24 | */ 25 | public abstract class AbstractJavaProviderMethodGenerator extends AbstractGenerator { 26 | 27 | public AbstractJavaProviderMethodGenerator() { 28 | super(); 29 | } 30 | 31 | public abstract void addClassElements(TopLevelClass topLevelClass); 32 | } 33 | -------------------------------------------------------------------------------- /doc/html/usage/db2.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | DB2 Usage Notes 7 | 8 | 9 | 10 | 11 | 17 | 18 |

DB2 Usage Notes

19 |

LONG VARCHAR Fields

20 |

By default MyBatis Generator will map LONG VARCHAR fields to the java.lang.String data type, 21 | and will mark these fields as jdbcType="LONGVARCHAR". This 22 | mapping will cause errors when retrieving data from DB2. DB2 LONG VARCHAR fields should be mapped 23 | to the java.lang.String data type with jdbcType="VARCHAR". To overcome 24 | this problem, use a <columnOverride> as follows:

25 |
26 |   <table schema="DB2ADMIN" tableName="ALLTYPES" >
27 |     <columnOverride column="LONG_VARCHAR_FIELD" javaType="java.lang.String" jdbcType="VARCHAR" />
28 |   </table>
29 | 
30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/xml/TextElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.xml; 17 | 18 | import org.mybatis.generator.api.dom.OutputUtilities; 19 | 20 | /** 21 | * @author Jeff Butler 22 | */ 23 | public class TextElement extends Element { 24 | private String content; 25 | 26 | /** 27 | * 28 | */ 29 | public TextElement(String content) { 30 | super(); 31 | this.content = content; 32 | } 33 | 34 | @Override 35 | public String getFormattedContent(int indentLevel) { 36 | StringBuilder sb = new StringBuilder(); 37 | OutputUtilities.xmlIndent(sb, indentLevel); 38 | sb.append(content); 39 | return sb.toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /doc/html/usage/oracle.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Oracle Usage Notes 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Oracle Usage Notes

19 |

Public Synonyms

20 |

If you want to generate objects for a table that has a public synonym, you 21 | should actually generate the objects against the real table - and then change the 22 | table name at runtime. MyBatis Generator supports this automatically.

23 |

For example, assume there is a public alias "FRED" that points to the 24 | table "HR.EMPLOYEES". The following table configuration will generate the objects 25 | based on HR.EMPLOYEES, but the runtime SQL will only refer to FRED:

26 |
27 |   <table schema="HR" tableName="EMPLOYEES">
28 |     <property name="ignoreQualifiersAtRuntime" value="true" />
29 |     <property name="runtimeTableName" value="FRED" />
30 |   </table>
31 | 
32 | 33 | 34 | -------------------------------------------------------------------------------- /doc/html/generatedobjects/results.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Using the Generated Objects 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Using the Generated Objects

19 |

MyBatis Generator (MBG) generates these types of objects:

20 |
    21 |
  1. Java Model Objects (always)
  2. 22 |
  3. SQL Map Files (always for iBATIS, usually for MyBatis)
  4. 23 |
  5. Java Client Objects (optional)
  6. 24 |
  7. A class for use in the xxxByExample methods. See the following pages for 25 | information about that class: 26 | 30 |
  8. 31 |
32 | 33 |

The individual pages describe these objects, and their usage.

34 | 35 | 36 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/logging/Log4jImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.logging; 17 | 18 | import org.apache.log4j.Logger; 19 | 20 | /** 21 | * 22 | * @author Clinton Begin 23 | * 24 | */ 25 | public class Log4jImpl implements Log { 26 | 27 | private Logger log; 28 | 29 | public Log4jImpl(Class clazz) { 30 | log = Logger.getLogger(clazz); 31 | } 32 | 33 | public boolean isDebugEnabled() { 34 | return log.isDebugEnabled(); 35 | } 36 | 37 | public void error(String s, Throwable e) { 38 | log.error(s, e); 39 | } 40 | 41 | public void error(String s) { 42 | log.error(s); 43 | } 44 | 45 | public void debug(String s) { 46 | log.debug(s); 47 | } 48 | 49 | public void warn(String s) { 50 | log.warn(s); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /doc/html/usage/mysql.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | MySql Usage Notes 7 | 8 | 9 | 10 | 11 | 17 | 18 |

MySql Usage Notes

19 |

Unsigned Fields

20 |

MySql supports both signed, and unsigned, numeric fields. These are not JDBC types, so MyBatis 21 | generator cannot automatically account for these types of fields. The Java data types are always signed. 22 | This can 23 | lead to a loss of precision when using unsigned fields. The solution is to provide a 24 | <columnOverride> for any unsigned numeric field in MySql. Here is an example 25 | of how to deal with an unsigned bigint field in MySql:

26 |
27 |   <table tableName="ALLTYPES" >
28 |     <columnOverride column="UNSIGNED_BIGINT_FIELD" javaType="java.lang.Object" jdbcType="LONG" />
29 |   </table>
30 | 
31 |

You will have to cast the returned value to the appropriate type yourself (in this case, 32 | java.math.BigInteger).

33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/exception/XMLParserException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.exception; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * @author Jeff Butler 23 | */ 24 | public class XMLParserException extends Exception { 25 | 26 | private static final long serialVersionUID = 5172525430401340573L; 27 | 28 | private List errors; 29 | 30 | /** 31 | * 32 | */ 33 | public XMLParserException(List errors) { 34 | super(); 35 | this.errors = errors; 36 | } 37 | 38 | public XMLParserException(String error) { 39 | super(error); 40 | this.errors = new ArrayList(); 41 | errors.add(error); 42 | } 43 | 44 | public List getErrors() { 45 | return errors; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/MergeConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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.config; 17 | 18 | /** 19 | * This class holds constants useful in the XML and Java merging operations. 20 | * 21 | * @author Jeff Butler 22 | * 23 | */ 24 | public class MergeConstants { 25 | 26 | /** 27 | * Utility class - no instances 28 | * 29 | */ 30 | private MergeConstants() { 31 | } 32 | 33 | public static final String[] OLD_XML_ELEMENT_PREFIXES = { "ibatorgenerated_", "abatorgenerated_" }; //$NON-NLS-1$ //$NON-NLS-2$ 34 | 35 | public static final String NEW_ELEMENT_TAG = "@mbggenerated"; //$NON-NLS-1$ 36 | public static final String[] OLD_ELEMENT_TAGS = { "@ibatorgenerated", "@abatorgenerated", "@mbggenerated" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 37 | 38 | } 39 | -------------------------------------------------------------------------------- /doc/html/configreference/generatorConfiguration.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | The <generatorConfiguration> Element 7 | 8 | 9 | 10 | 11 | 17 | 18 |

The <generatorConfiguration> Element

19 |

The <generatorConfiguration> element is the root element of a 20 | MyBatis Generator configuration file. The file should contain the following DOCTYPE: 21 |

22 |
23 | <!DOCTYPE generatorConfiguration PUBLIC
24 |   "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
25 |   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
26 | 
27 | 28 |

Required Attributes

29 |

None

30 | 31 |

Optional Attributes

32 |

None

33 | 34 |

Child Elements

35 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/JavaTypeResolverConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.config; 17 | 18 | import org.mybatis.generator.api.dom.xml.Attribute; 19 | import org.mybatis.generator.api.dom.xml.XmlElement; 20 | 21 | /** 22 | * @author Jeff Butler 23 | */ 24 | public class JavaTypeResolverConfiguration extends TypedPropertyHolder { 25 | 26 | /** 27 | * 28 | */ 29 | public JavaTypeResolverConfiguration() { 30 | super(); 31 | } 32 | 33 | public XmlElement toXmlElement() { 34 | XmlElement answer = new XmlElement("javaTypeResolver"); //$NON-NLS-1$ 35 | if (getConfigurationType() != null) { 36 | answer.addAttribute(new Attribute("type", getConfigurationType())); //$NON-NLS-1$ 37 | } 38 | 39 | addPropertyXmlElements(answer); 40 | 41 | return answer; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/CommentGeneratorConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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 | package org.mybatis.generator.config; 18 | 19 | import org.mybatis.generator.api.dom.xml.Attribute; 20 | import org.mybatis.generator.api.dom.xml.XmlElement; 21 | 22 | /** 23 | * @author Jeff Butler 24 | * 25 | */ 26 | public class CommentGeneratorConfiguration extends TypedPropertyHolder { 27 | 28 | /** 29 | * 30 | */ 31 | public CommentGeneratorConfiguration() { 32 | } 33 | 34 | public XmlElement toXmlElement() { 35 | XmlElement answer = new XmlElement("commentGenerator"); //$NON-NLS-1$ 36 | if (getConfigurationType() != null) { 37 | answer.addAttribute(new Attribute("type", getConfigurationType())); //$NON-NLS-1$ 38 | } 39 | 40 | addPropertyXmlElements(answer); 41 | 42 | return answer; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /doc/html/configreference/property.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | The <property> Element 7 | 8 | 9 | 10 | 11 | 17 | 18 |

The <property> Element

19 |

The <property> element is used to specify properties for many of the 20 | other elements. The documentation page for each element that supports the 21 | property element lists the different values that are valid. This element 22 | can also be used to pass properties to any custom code generators that 23 | you implement.

24 | 25 |

Required Attributes

26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
AttributeDescription
nameThe name of the property (case sensitive).
valueThe value of the property (typically case insensitive).
40 | 41 |

Optional Attributes

42 |

None

43 | 44 |

Child Elements

45 |

None

46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/ant/AntProgressCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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 | package org.mybatis.generator.ant; 18 | 19 | import org.apache.tools.ant.Project; 20 | import org.apache.tools.ant.Task; 21 | import org.mybatis.generator.internal.NullProgressCallback; 22 | 23 | /** 24 | * This callback logs progress messages with the Ant logger 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public class AntProgressCallback extends NullProgressCallback { 30 | 31 | private Task task; 32 | private boolean verbose; 33 | 34 | /** 35 | * 36 | */ 37 | public AntProgressCallback(Task task, boolean verbose) { 38 | super(); 39 | this.task = task; 40 | this.verbose = verbose; 41 | } 42 | 43 | @Override 44 | public void startTask(String subTaskName) { 45 | if (verbose) { 46 | task.log(subTaskName, Project.MSG_VERBOSE); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/exception/ShellException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.exception; 17 | 18 | /** 19 | * This class is used by the ShellCallback methods to denote unrecoverable 20 | * errors. 21 | * 22 | * @author Jeff Butler 23 | */ 24 | public class ShellException extends Exception { 25 | static final long serialVersionUID = -2026841561754434544L; 26 | 27 | /** 28 | * 29 | */ 30 | public ShellException() { 31 | super(); 32 | } 33 | 34 | /** 35 | * @param arg0 36 | */ 37 | public ShellException(String arg0) { 38 | super(arg0); 39 | } 40 | 41 | /** 42 | * @param arg0 43 | * @param arg1 44 | */ 45 | public ShellException(String arg0, Throwable arg1) { 46 | super(arg0, arg1); 47 | } 48 | 49 | /** 50 | * @param arg0 51 | */ 52 | public ShellException(Throwable arg0) { 53 | super(arg0); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/NullProgressCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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; 17 | 18 | import org.mybatis.generator.api.ProgressCallback; 19 | 20 | /** 21 | * This class implements a progress callback that does nothing. It is used when 22 | * the client passes in a null for the ProgressCallback. 23 | * 24 | * @author Jeff Butler 25 | */ 26 | public class NullProgressCallback implements ProgressCallback { 27 | 28 | /** 29 | * 30 | */ 31 | public NullProgressCallback() { 32 | super(); 33 | } 34 | 35 | public void generationStarted(int totalTasks) { 36 | } 37 | 38 | public void introspectionStarted(int totalTasks) { 39 | } 40 | 41 | public void saveStarted(int totalTasks) { 42 | } 43 | 44 | public void startTask(String taskName) { 45 | } 46 | 47 | public void checkCancel() throws InterruptedException { 48 | } 49 | 50 | public void done() { 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/test/_Ibator_Run_Test.java: -------------------------------------------------------------------------------- 1 | package test; 2 | import org.apache.log4j.Logger; 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import org.mybatis.generator.api.MyBatisGenerator; 7 | import org.mybatis.generator.config.Configuration; 8 | import org.mybatis.generator.config.xml.ConfigurationParser; 9 | import org.mybatis.generator.internal.DefaultShellCallback; 10 | 11 | public class _Ibator_Run_Test { 12 | private static final Logger logger = Logger.getLogger(_Ibator_Run_Test.class); 13 | 14 | public static void main(String[] args) { 15 | _Ibator_Run_Test test = new _Ibator_Run_Test(); 16 | // test.main1("grid1.xml"); 17 | // test.main1("mysql.xml"); 18 | // test.main1("mysql_bank.xml"); 19 | test.main1("authority_mysql.xml"); 20 | // test.main1("authority_oracle.xml"); 21 | // test.main1("tsm_oracle.xml"); 22 | // test.main1("ejt_oracle.xml"); 23 | // test.main1("datacheck_sql_2.xml"); 24 | // test.main1("oracle.xml"); 25 | } 26 | 27 | public void main1(String fileName) { 28 | try { 29 | List warnings = new ArrayList(); 30 | ConfigurationParser cp = new ConfigurationParser(warnings); 31 | Configuration config = cp 32 | .parseConfiguration(this.getClass().getClassLoader().getResourceAsStream(fileName)); 33 | 34 | DefaultShellCallback shellCallback = new DefaultShellCallback(true); 35 | 36 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings); 37 | myBatisGenerator.generate(null); 38 | } catch (Exception e) { 39 | logger.error("Exception:", e); 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/MixedMapperGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.xmlmapper; 17 | 18 | import org.mybatis.generator.api.dom.xml.XmlElement; 19 | 20 | /** 21 | * 22 | * @author Jeff Butler 23 | * 24 | */ 25 | public class MixedMapperGenerator extends XMLMapperGenerator { 26 | 27 | @Override 28 | protected void addSelectByPrimaryKeyElement(XmlElement parentElement) { 29 | return; 30 | } 31 | 32 | @Override 33 | protected void addDeleteByPrimaryKeyElement(XmlElement parentElement) { 34 | return; 35 | } 36 | 37 | @Override 38 | protected void addInsertElement(XmlElement parentElement) { 39 | return; 40 | } 41 | 42 | @Override 43 | protected void addUpdateByPrimaryKeyWithBLOBsElement(XmlElement parentElement) { 44 | return; 45 | } 46 | 47 | @Override 48 | protected void addUpdateByPrimaryKeyWithoutBLOBsElement(XmlElement parentElement) { 49 | return; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/TypedPropertyHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005, 2006 The Apache Software Foundation 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.config; 17 | 18 | /** 19 | * @author Jeff Butler 20 | */ 21 | public abstract class TypedPropertyHolder extends PropertyHolder { 22 | 23 | private String configurationType; 24 | 25 | /** 26 | * 27 | */ 28 | public TypedPropertyHolder() { 29 | super(); 30 | } 31 | 32 | public String getConfigurationType() { 33 | return configurationType; 34 | } 35 | 36 | /** 37 | * Sets the value of the type specified in the configuration. If the special 38 | * value DEFAULT is specified, then the value will be ignored. 39 | * 40 | * @param configurationType 41 | * the type specified in the configuration 42 | */ 43 | public void setConfigurationType(String configurationType) { 44 | if (!"DEFAULT".equalsIgnoreCase(configurationType)) { //$NON-NLS-1$ 45 | this.configurationType = configurationType; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /doc/html/license.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Licensing Information 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Licensing Information

19 |

Licensed under the Apache License, Version 2.0 (the "License"); 20 | you may not use this product except in compliance with the License. 21 | You may obtain a copy of the License at 22 |

23 |

       24 | http://www.apache.org/licenses/LICENSE-2.0

25 |

26 | Unless required by applicable law or agreed to in writing, software 27 | distributed under the License is distributed on an "AS IS" BASIS, 28 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 29 | See the License for the specific language governing permissions and 30 | limitations under the License. 31 |

32 | 33 |

This product includes software developed by the Apache Software 34 | Foundation (http://www.apache.org/).

35 | 36 |

This product includes the EqualsUtil and HashCodeUtil classes 37 | from http://www.javapractices.com.

38 | 39 | 40 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.mybatis 6 | mybatis-generator-core 7 | 1.3.1 8 | jar 9 | MyBatis Generator Core 10 | 1.3.1改编版本 11 | 12 | 1.6 13 | 14 | 15 | 16 | 17 | log4j 18 | log4j 19 | 1.2.16 20 | 21 | 22 | org.apache.ant 23 | ant 24 | 1.8.2 25 | 26 | 27 | junit 28 | junit 29 | 4.8.2 30 | 31 | 32 | org.hsqldb 33 | hsqldb 34 | 2.2.4 35 | 36 | 37 | org.apache.commons 38 | commons-lang3 39 | 3.1 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-compiler-plugin 49 | 2.3.2 50 | 51 | ${java.version} 52 | ${java.version} 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/xml/Attribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.xml; 17 | 18 | /** 19 | * @author Jeff Butler 20 | */ 21 | public class Attribute { 22 | private String name; 23 | private String value; 24 | 25 | /** 26 | * 27 | */ 28 | public Attribute(String name, String value) { 29 | super(); 30 | this.name = name; 31 | this.value = value; 32 | } 33 | 34 | /** 35 | * @return Returns the name. 36 | */ 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | /** 42 | * @return Returns the value. 43 | */ 44 | public String getValue() { 45 | return value; 46 | } 47 | 48 | 49 | /** 50 | * @param name */ 51 | public void setName(String name) { 52 | this.name = name; 53 | } 54 | 55 | /** 56 | * @param value */ 57 | public void setValue(String value) { 58 | this.value = value; 59 | } 60 | 61 | public String getFormattedContent() { 62 | StringBuilder sb = new StringBuilder(); 63 | sb.append(name); 64 | sb.append("=\""); //$NON-NLS-1$ 65 | sb.append(value); 66 | sb.append('\"'); 67 | 68 | return sb.toString(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/ModelType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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 | package org.mybatis.generator.config; 18 | 19 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 20 | 21 | /** 22 | * Typesafe enum of different model types 23 | * 24 | * @author Jeff Butler 25 | */ 26 | public enum ModelType { 27 | HIERARCHICAL("hierarchical"), //$NON-NLS-1$ 28 | FLAT("flat"), //$NON-NLS-1$ 29 | CONDITIONAL("conditional"); //$NON-NLS-1$ 30 | 31 | private final String modelType; 32 | 33 | /** 34 | * 35 | */ 36 | private ModelType(String modelType) { 37 | this.modelType = modelType; 38 | } 39 | 40 | public String getModelType() { 41 | return modelType; 42 | } 43 | 44 | public static ModelType getModelType(String type) { 45 | if (HIERARCHICAL.getModelType().equalsIgnoreCase(type)) { 46 | return HIERARCHICAL; 47 | } else if (FLAT.getModelType().equalsIgnoreCase(type)) { 48 | return FLAT; 49 | } else if (CONDITIONAL.getModelType().equalsIgnoreCase(type)) { 50 | return CONDITIONAL; 51 | } else { 52 | throw new RuntimeException(getString("RuntimeError.13", type)); //$NON-NLS-1$ 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/PluginConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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.config; 17 | 18 | import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; 19 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 20 | 21 | import java.util.List; 22 | 23 | import org.mybatis.generator.api.dom.xml.Attribute; 24 | import org.mybatis.generator.api.dom.xml.XmlElement; 25 | 26 | /** 27 | * 28 | * @author Jeff Butler 29 | * 30 | */ 31 | public class PluginConfiguration extends TypedPropertyHolder { 32 | public PluginConfiguration() { 33 | } 34 | 35 | public XmlElement toXmlElement() { 36 | XmlElement answer = new XmlElement("plugin"); //$NON-NLS-1$ 37 | if (getConfigurationType() != null) { 38 | answer.addAttribute(new Attribute("type", getConfigurationType())); //$NON-NLS-1$ 39 | } 40 | 41 | addPropertyXmlElements(answer); 42 | 43 | return answer; 44 | } 45 | 46 | public void validate(List errors, String contextId) { 47 | if (!stringHasValue(getConfigurationType())) { 48 | errors.add(getString("ValidationError.17", //$NON-NLS-1$ 49 | contextId)); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /doc/html/configreference/ignoreColumn.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | The <ignoreColumn> Element 7 | 8 | 9 | 10 | 11 | 17 | 18 |

The <ignoreColumn> Element

19 |

The <ignoreColumn> element is used to tell MyBatis Generator (MBG) to ignore a column 20 | in an introspected table. No generated SQL will refer to the column, and no 21 | property will be generated for the column in the model objects. 22 | This element is an optional child 23 | element of the <table> element.

24 | 25 |

Required Attributes

26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
AttributeDescription
columnThe column name of the ignored column.
36 | 37 |

Optional Attributes

38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 48 | 49 |
AttributeDescription
delimitedColumnNameIf true then MBG will perform a case-sensitive exact 46 | match when matching against returned columns from the database. If 47 | false (default) then the name is considered case-insensitive.
50 | 51 |

Child Elements

52 |

None

53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/AbstractJavaClientGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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 | /** 19 | * This class exists to that Java client generators can specify whether an XML 20 | * generator is required to match the methods in the Java client. For example, a 21 | * Java client built entirely with annotations does not need matching XML. 22 | * 23 | * @author Jeff Butler 24 | * 25 | */ 26 | public abstract class AbstractJavaClientGenerator extends AbstractJavaGenerator { 27 | 28 | private boolean requiresXMLGenerator; 29 | 30 | public AbstractJavaClientGenerator(boolean requiresXMLGenerator) { 31 | super(); 32 | this.requiresXMLGenerator = requiresXMLGenerator; 33 | } 34 | 35 | /** 36 | * @return true if matching XML is required 37 | */ 38 | public boolean requiresXMLGenerator() { 39 | return requiresXMLGenerator; 40 | } 41 | 42 | /** 43 | * This method returns an instance of the XML generator associated with this 44 | * client generator. 45 | * 46 | * @return the matched XML generator. May return null if no XML is required 47 | * by this generator 48 | */ 49 | public abstract AbstractXmlGenerator getMatchedXMLGenerator(); 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/sqlprovider/ProviderSelectByExampleWithBLOBsMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.sqlprovider; 17 | 18 | import java.util.List; 19 | 20 | import org.mybatis.generator.api.IntrospectedColumn; 21 | import org.mybatis.generator.api.dom.java.Method; 22 | import org.mybatis.generator.api.dom.java.TopLevelClass; 23 | 24 | /** 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public class ProviderSelectByExampleWithBLOBsMethodGenerator extends ProviderSelectByExampleWithoutBLOBsMethodGenerator { 30 | 31 | public ProviderSelectByExampleWithBLOBsMethodGenerator() { 32 | super(); 33 | } 34 | 35 | @Override 36 | public List getColumns() { 37 | return introspectedTable.getAllColumns(); 38 | } 39 | 40 | @Override 41 | public String getMethodName() { 42 | return introspectedTable.getSelectByExampleWithBLOBsStatementId(); 43 | } 44 | 45 | @Override 46 | public boolean callPlugins(Method method, TopLevelClass topLevelClass) { 47 | return context.getPlugins().providerSelectByExampleWithBLOBsMethodGenerated(method, topLevelClass, 48 | introspectedTable); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/sqlprovider/ProviderUpdateByExampleWithBLOBsMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.sqlprovider; 17 | 18 | import java.util.List; 19 | 20 | import org.mybatis.generator.api.IntrospectedColumn; 21 | import org.mybatis.generator.api.dom.java.Method; 22 | import org.mybatis.generator.api.dom.java.TopLevelClass; 23 | 24 | /** 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public class ProviderUpdateByExampleWithBLOBsMethodGenerator extends ProviderUpdateByExampleWithoutBLOBsMethodGenerator { 30 | 31 | public ProviderUpdateByExampleWithBLOBsMethodGenerator() { 32 | super(); 33 | } 34 | 35 | @Override 36 | public String getMethodName() { 37 | return introspectedTable.getUpdateByExampleWithBLOBsStatementId(); 38 | } 39 | 40 | @Override 41 | public List getColumns() { 42 | return introspectedTable.getAllColumns(); 43 | } 44 | 45 | @Override 46 | public boolean callPlugins(Method method, TopLevelClass topLevelClass) { 47 | return context.getPlugins().providerUpdateByExampleWithBLOBsMethodGenerated(method, topLevelClass, 48 | introspectedTable); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/rules/FlatModelRules.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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 | package org.mybatis.generator.internal.rules; 18 | 19 | import org.mybatis.generator.api.IntrospectedTable; 20 | 21 | /** 22 | * This class encapsulates all the code generation rules for a table using the 23 | * flat model. 24 | * 25 | * @author Jeff Butler 26 | * 27 | */ 28 | public class FlatModelRules extends BaseRules { 29 | 30 | /** 31 | * 32 | */ 33 | public FlatModelRules(IntrospectedTable introspectedTable) { 34 | super(introspectedTable); 35 | } 36 | 37 | /** 38 | * We never generate a primary key in the flat model. 39 | * 40 | * @return true if the primary key should be generated 41 | */ 42 | public boolean generatePrimaryKeyClass() { 43 | return false; 44 | } 45 | 46 | /** 47 | * We always generate a base record in the flat model. 48 | * 49 | * @return true if the class should be generated 50 | */ 51 | public boolean generateBaseRecordClass() { 52 | return true; 53 | } 54 | 55 | /** 56 | * We never generate a record with BLOBs class in the flat model. 57 | * 58 | * @return true if the record with BLOBs class should be generated 59 | */ 60 | public boolean generateRecordWithBLOBsClass() { 61 | return false; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/logging/JdkLoggingImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.logging; 17 | 18 | import java.util.logging.Level; 19 | import java.util.logging.LogRecord; 20 | import java.util.logging.Logger; 21 | 22 | /** 23 | * 24 | * @author Clinton Begin 25 | * @author Jeff Butler 26 | * 27 | */ 28 | public class JdkLoggingImpl implements Log { 29 | 30 | private Logger log; 31 | 32 | public JdkLoggingImpl(Class clazz) { 33 | log = Logger.getLogger(clazz.getName()); 34 | } 35 | 36 | public boolean isDebugEnabled() { 37 | return log.isLoggable(Level.FINE); 38 | } 39 | 40 | public void error(String s, Throwable e) { 41 | LogRecord lr = new LogRecord(Level.SEVERE, s); 42 | lr.setSourceClassName(log.getName()); 43 | lr.setThrown(e); 44 | 45 | log.log(lr); 46 | } 47 | 48 | public void error(String s) { 49 | LogRecord lr = new LogRecord(Level.SEVERE, s); 50 | lr.setSourceClassName(log.getName()); 51 | 52 | log.log(lr); 53 | } 54 | 55 | public void debug(String s) { 56 | LogRecord lr = new LogRecord(Level.FINE, s); 57 | lr.setSourceClassName(log.getName()); 58 | 59 | log.log(lr); 60 | } 61 | 62 | public void warn(String s) { 63 | LogRecord lr = new LogRecord(Level.WARNING, s); 64 | lr.setSourceClassName(log.getName()); 65 | 66 | log.log(lr); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /doc/html/usage/postgresql.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | PostgreSQL Usage Notes 7 | 8 | 9 | 10 | 11 | 17 | 18 |

PostgreSQL Usage Notes

19 |

Case Sensitivity

20 |

PostgreSQL is case sensitive with regards to all database identifiers 21 | (table names, schema names, column names, etc.) In addition, PostgreSQL 22 | has a distinct preference for all identifiers being in all lower case letters. 23 | If you use all lower case identifiers for PostgreSQL, then MyBatis Generator will find tables 24 | and write correct SQL with no additional consideration. If you used mixed cased, 25 | or upper case, identifiers you will 26 | need to configure MyBatis Generator appropriately:

27 |
    28 |
  • Use the delimitIdentifiers option if your table or schema 29 | is mixed case or all upper case.
  • 30 |
  • For each mixed case or upper case column you could specify a 31 | <columnOverride> element to specifically delimit the column, 32 | or you could specify the delimitAllColumns attribute 33 | to delimit all column names.
  • 34 |
35 |

Examples:

36 |
37 |   <table schema="HR" tableName="Employees"
38 |       delimitIdentifiers="true" delimitAllColumns="true"/>
39 | 
40 |

Or...

41 |
42 |   <table schema="HR" tableName="Employees" delimitIdentifiers="true" >
43 |     <columnOverride column="EmployeeId" delimitedColumnName="true" />
44 |     <columnOverride column="EmployeeName" delimitedColumnName="true" />
45 |   </table>
46 | 
47 | 48 | 49 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/java/CompilationUnit.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.java; 17 | 18 | import java.util.List; 19 | import java.util.Set; 20 | 21 | /** 22 | * This interface describes methods common to all Java compilation units (Java 23 | * classes, interfaces, and enums). 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public interface CompilationUnit { 28 | String getFormattedContent(); 29 | 30 | Set getImportedTypes(); 31 | 32 | Set getStaticImports(); 33 | 34 | FullyQualifiedJavaType getSuperClass(); 35 | 36 | boolean isJavaInterface(); 37 | 38 | boolean isJavaEnumeration(); 39 | 40 | Set getSuperInterfaceTypes(); 41 | 42 | FullyQualifiedJavaType getType(); 43 | 44 | void addImportedType(FullyQualifiedJavaType importedType); 45 | 46 | void addImportedTypes(Set importedTypes); 47 | 48 | void addStaticImport(String staticImport); 49 | 50 | void addStaticImports(Set staticImports); 51 | 52 | /** 53 | * Comments will be written at the top of the file as is, we do not append 54 | * any start or end comment characters. 55 | * 56 | * Note that in the Eclipse plugin, file comments will not be merged. 57 | * 58 | * @param commentLine 59 | */ 60 | void addFileCommentLine(String commentLine); 61 | 62 | List getFileCommentLines(); 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedCountByExampleMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.CountByExampleMethodGenerator; 22 | 23 | /** 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public class AnnotatedCountByExampleMethodGenerator extends CountByExampleMethodGenerator { 28 | 29 | public AnnotatedCountByExampleMethodGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addMapperAnnotations(Interface interfaze, Method method) { 35 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 36 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectProvider")); //$NON-NLS-1$ 37 | StringBuilder sb = new StringBuilder(); 38 | sb.append("@SelectProvider(type="); //$NON-NLS-1$ 39 | sb.append(fqjt.getShortName()); 40 | sb.append(".class, method=\""); //$NON-NLS-1$ 41 | sb.append(introspectedTable.getCountByExampleStatementId()); 42 | sb.append("\")"); //$NON-NLS-1$ 43 | 44 | method.addAnnotation(sb.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedDeleteByExampleMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.DeleteByExampleMethodGenerator; 22 | 23 | /** 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public class AnnotatedDeleteByExampleMethodGenerator extends DeleteByExampleMethodGenerator { 28 | 29 | public AnnotatedDeleteByExampleMethodGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addMapperAnnotations(Interface interfaze, Method method) { 35 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 36 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.DeleteProvider")); //$NON-NLS-1$ 37 | StringBuilder sb = new StringBuilder(); 38 | sb.append("@DeleteProvider(type="); //$NON-NLS-1$ 39 | sb.append(fqjt.getShortName()); 40 | sb.append(".class, method=\""); //$NON-NLS-1$ 41 | sb.append(introspectedTable.getDeleteByExampleStatementId()); 42 | sb.append("\")"); //$NON-NLS-1$ 43 | 44 | method.addAnnotation(sb.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/PropertyHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.config; 17 | 18 | import java.util.Enumeration; 19 | import java.util.Properties; 20 | 21 | import org.mybatis.generator.api.dom.xml.Attribute; 22 | import org.mybatis.generator.api.dom.xml.XmlElement; 23 | 24 | /** 25 | * @author Jeff Butler 26 | */ 27 | public abstract class PropertyHolder { 28 | private Properties properties; 29 | 30 | /** 31 | * 32 | */ 33 | public PropertyHolder() { 34 | super(); 35 | properties = new Properties(); 36 | } 37 | 38 | public void addProperty(String name, String value) { 39 | properties.setProperty(name, value); 40 | } 41 | 42 | public String getProperty(String name) { 43 | return properties.getProperty(name); 44 | } 45 | 46 | public Properties getProperties() { 47 | return properties; 48 | } 49 | 50 | protected void addPropertyXmlElements(XmlElement xmlElement) { 51 | Enumeration enumeration = properties.propertyNames(); 52 | while (enumeration.hasMoreElements()) { 53 | String propertyName = (String) enumeration.nextElement(); 54 | 55 | XmlElement propertyElement = new XmlElement("property"); //$NON-NLS-1$ 56 | propertyElement.addAttribute(new Attribute("name", propertyName)); //$NON-NLS-1$ 57 | propertyElement.addAttribute(new Attribute("value", properties.getProperty(propertyName))); //$NON-NLS-1$ 58 | xmlElement.addElement(propertyElement); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/db/ActualTableName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007 The Apache Software Foundation 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 | package org.mybatis.generator.internal.db; 18 | 19 | import static org.mybatis.generator.internal.util.StringUtility.composeFullyQualifiedTableName; 20 | 21 | /** 22 | * This class holds the actual catalog, schema, and table name returned from the 23 | * database introspection. 24 | * 25 | * @author Jeff Butler 26 | * 27 | */ 28 | public class ActualTableName { 29 | 30 | private String tableName; 31 | private String catalog; 32 | private String schema; 33 | private String fullName; 34 | 35 | public ActualTableName(String catalog, String schema, String tableName) { 36 | this.catalog = catalog; 37 | this.schema = schema; 38 | this.tableName = tableName; 39 | fullName = composeFullyQualifiedTableName(catalog, schema, tableName, '.'); 40 | } 41 | 42 | public String getCatalog() { 43 | return catalog; 44 | } 45 | 46 | public String getSchema() { 47 | return schema; 48 | } 49 | 50 | public String getTableName() { 51 | return tableName; 52 | } 53 | 54 | @Override 55 | public boolean equals(Object obj) { 56 | if (obj == null || !(obj instanceof ActualTableName)) { 57 | return false; 58 | } 59 | 60 | return obj.toString().equals(this.toString()); 61 | } 62 | 63 | @Override 64 | public int hashCode() { 65 | return fullName.hashCode(); 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return fullName; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedUpdateByExampleSelectiveMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByExampleSelectiveMethodGenerator; 22 | 23 | /** 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public class AnnotatedUpdateByExampleSelectiveMethodGenerator extends UpdateByExampleSelectiveMethodGenerator { 28 | 29 | public AnnotatedUpdateByExampleSelectiveMethodGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addMapperAnnotations(Interface interfaze, Method method) { 35 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 36 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.UpdateProvider")); //$NON-NLS-1$ 37 | StringBuilder sb = new StringBuilder(); 38 | sb.append("@UpdateProvider(type="); //$NON-NLS-1$ 39 | sb.append(fqjt.getShortName()); 40 | sb.append(".class, method=\""); //$NON-NLS-1$ 41 | sb.append(introspectedTable.getUpdateByExampleSelectiveStatementId()); 42 | sb.append("\")"); //$NON-NLS-1$ 43 | 44 | method.addAnnotation(sb.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedUpdateByExampleWithBLOBsMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByExampleWithBLOBsMethodGenerator; 22 | 23 | /** 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public class AnnotatedUpdateByExampleWithBLOBsMethodGenerator extends UpdateByExampleWithBLOBsMethodGenerator { 28 | 29 | public AnnotatedUpdateByExampleWithBLOBsMethodGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addMapperAnnotations(Interface interfaze, Method method) { 35 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 36 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.UpdateProvider")); //$NON-NLS-1$ 37 | StringBuilder sb = new StringBuilder(); 38 | sb.append("@UpdateProvider(type="); //$NON-NLS-1$ 39 | sb.append(fqjt.getShortName()); 40 | sb.append(".class, method=\""); //$NON-NLS-1$ 41 | sb.append(introspectedTable.getUpdateByExampleWithBLOBsStatementId()); 42 | sb.append("\")"); //$NON-NLS-1$ 43 | 44 | method.addAnnotation(sb.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedUpdateByExampleWithoutBLOBsMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByExampleWithoutBLOBsMethodGenerator; 22 | 23 | /** 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public class AnnotatedUpdateByExampleWithoutBLOBsMethodGenerator extends UpdateByExampleWithoutBLOBsMethodGenerator { 28 | 29 | public AnnotatedUpdateByExampleWithoutBLOBsMethodGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addMapperAnnotations(Interface interfaze, Method method) { 35 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 36 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.UpdateProvider")); //$NON-NLS-1$ 37 | StringBuilder sb = new StringBuilder(); 38 | sb.append("@UpdateProvider(type="); //$NON-NLS-1$ 39 | sb.append(fqjt.getShortName()); 40 | sb.append(".class, method=\""); //$NON-NLS-1$ 41 | sb.append(introspectedTable.getUpdateByExampleStatementId()); 42 | sb.append("\")"); //$NON-NLS-1$ 43 | 44 | method.addAnnotation(sb.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/AbstractGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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 List warnings; 33 | protected ProgressCallback progressCallback; 34 | 35 | public AbstractGenerator() { 36 | super(); 37 | } 38 | 39 | public Context getContext() { 40 | return context; 41 | } 42 | 43 | public void setContext(Context context) { 44 | this.context = context; 45 | } 46 | 47 | public IntrospectedTable getIntrospectedTable() { 48 | return introspectedTable; 49 | } 50 | 51 | public void setIntrospectedTable(IntrospectedTable introspectedTable) { 52 | this.introspectedTable = introspectedTable; 53 | } 54 | 55 | public List getWarnings() { 56 | return warnings; 57 | } 58 | 59 | public void setWarnings(List warnings) { 60 | this.warnings = warnings; 61 | } 62 | 63 | public ProgressCallback getProgressCallback() { 64 | return progressCallback; 65 | } 66 | 67 | public void setProgressCallback(ProgressCallback progressCallback) { 68 | this.progressCallback = progressCallback; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedUpdateByPrimaryKeySelectiveMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeySelectiveMethodGenerator; 22 | 23 | /** 24 | * 25 | * @author Jeff Butler 26 | */ 27 | public class AnnotatedUpdateByPrimaryKeySelectiveMethodGenerator extends UpdateByPrimaryKeySelectiveMethodGenerator { 28 | 29 | public AnnotatedUpdateByPrimaryKeySelectiveMethodGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addMapperAnnotations(Interface interfaze, Method method) { 35 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 36 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.UpdateProvider")); //$NON-NLS-1$ 37 | StringBuilder sb = new StringBuilder(); 38 | sb.append("@UpdateProvider(type="); //$NON-NLS-1$ 39 | sb.append(fqjt.getShortName()); 40 | sb.append(".class, method=\""); //$NON-NLS-1$ 41 | sb.append(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId()); 42 | sb.append("\")"); //$NON-NLS-1$ 43 | 44 | method.addAnnotation(sb.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/DeleteByExampleElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.xmlmapper.elements; 17 | 18 | import org.mybatis.generator.api.dom.xml.Attribute; 19 | import org.mybatis.generator.api.dom.xml.TextElement; 20 | import org.mybatis.generator.api.dom.xml.XmlElement; 21 | 22 | /** 23 | * 24 | * @author Jeff Butler 25 | * 26 | */ 27 | public class DeleteByExampleElementGenerator extends AbstractXmlElementGenerator { 28 | 29 | public DeleteByExampleElementGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addElements(XmlElement parentElement) { 35 | XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$ 36 | 37 | String fqjt = introspectedTable.getExampleType(); 38 | 39 | answer.addAttribute(new Attribute("id", introspectedTable.getDeleteByExampleStatementId())); //$NON-NLS-1$ 40 | answer.addAttribute(new Attribute("parameterType", fqjt)); //$NON-NLS-1$ 41 | 42 | context.getCommentGenerator().addComment(answer); 43 | 44 | StringBuilder sb = new StringBuilder(); 45 | sb.append("delete from "); //$NON-NLS-1$ 46 | sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); 47 | answer.addElement(new TextElement(sb.toString())); 48 | answer.addElement(getExampleIncludeElement()); 49 | 50 | if (context.getPlugins().sqlMapDeleteByExampleElementGenerated(answer, introspectedTable)) { 51 | parentElement.addElement(answer); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/xml/ParserEntityResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.config.xml; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | 21 | import org.mybatis.generator.codegen.XmlConstants; 22 | import org.xml.sax.EntityResolver; 23 | import org.xml.sax.InputSource; 24 | import org.xml.sax.SAXException; 25 | 26 | /** 27 | * @author Jeff Butler 28 | */ 29 | public class ParserEntityResolver implements EntityResolver { 30 | 31 | /** 32 | * 33 | */ 34 | public ParserEntityResolver() { 35 | super(); 36 | } 37 | 38 | /* 39 | * (non-Javadoc) 40 | * 41 | * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, 42 | * java.lang.String) 43 | */ 44 | public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { 45 | if (XmlConstants.IBATOR_CONFIG_PUBLIC_ID.equalsIgnoreCase(publicId)) { 46 | InputStream is = getClass().getClassLoader().getResourceAsStream( 47 | "org/mybatis/generator/config/xml/ibator-config_1_0.dtd"); //$NON-NLS-1$ 48 | InputSource ins = new InputSource(is); 49 | 50 | return ins; 51 | } else if (XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID.equalsIgnoreCase(publicId)) { 52 | InputStream is = getClass().getClassLoader().getResourceAsStream( 53 | "org/mybatis/generator/config/xml/mybatis-generator-config_1_0.dtd"); //$NON-NLS-1$ 54 | InputSource ins = new InputSource(is); 55 | 56 | return ins; 57 | } else { 58 | return null; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/GeneratedXmlFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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 org.mybatis.generator.api.dom.xml.Document; 19 | 20 | /** 21 | * @author Jeff Butler 22 | */ 23 | public class GeneratedXmlFile extends GeneratedFile { 24 | private Document document; 25 | 26 | private String fileName; 27 | 28 | private String targetPackage; 29 | 30 | private boolean isMergeable; 31 | 32 | /** 33 | * 34 | * @param document 35 | * @param fileName 36 | * @param targetPackage 37 | * @param targetProject 38 | * @param isMergeable 39 | * true if the file can be merged by the built in XML file 40 | * merger. 41 | */ 42 | public GeneratedXmlFile(Document document, String fileName, String targetPackage, String targetProject, 43 | boolean isMergeable) { 44 | super(targetProject); 45 | this.document = document; 46 | this.fileName = fileName; 47 | this.targetPackage = targetPackage; 48 | this.isMergeable = isMergeable; 49 | } 50 | 51 | @Override 52 | public String getFormattedContent() { 53 | return document.getFormattedContent(); 54 | } 55 | 56 | /** 57 | * @return Returns the fileName. 58 | */ 59 | @Override 60 | public String getFileName() { 61 | return fileName; 62 | } 63 | 64 | /** 65 | * @return Returns the targetPackage. 66 | */ 67 | @Override 68 | public String getTargetPackage() { 69 | return targetPackage; 70 | } 71 | 72 | @Override 73 | public boolean isMergeable() { 74 | return isMergeable; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/plugins/AnnotationPlugin.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.generator.plugins; 2 | 3 | import java.util.List; 4 | 5 | import org.mybatis.generator.api.IntrospectedTable; 6 | import org.mybatis.generator.api.PluginAdapter; 7 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 8 | import org.mybatis.generator.api.dom.java.Interface; 9 | import org.mybatis.generator.api.dom.java.Method; 10 | import org.mybatis.generator.api.dom.java.Parameter; 11 | import org.mybatis.generator.api.dom.java.TopLevelClass; 12 | 13 | /** 14 | * 给dao增加注解增加注解 15 | * 16 | * @author QQ:34847009 17 | * @date 2010-10-21 下午09:33:48 18 | */ 19 | public class AnnotationPlugin extends PluginAdapter { 20 | private FullyQualifiedJavaType repository; 21 | private FullyQualifiedJavaType autowired; 22 | private FullyQualifiedJavaType sqlMapClient; 23 | 24 | public AnnotationPlugin() { 25 | super(); 26 | autowired = new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired"); //$NON-NLS-1$ 27 | repository = new FullyQualifiedJavaType("org.springframework.stereotype.Repository"); //$NON-NLS-1$ 28 | sqlMapClient = new FullyQualifiedJavaType("com.ibatis.sqlmap.client.SqlMapClient"); //$NON-NLS-1$ 29 | } 30 | 31 | @Override 32 | public boolean validate(List warnings) { 33 | return true; 34 | } 35 | 36 | @Override 37 | public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 38 | Method method = topLevelClass.getMethods().get(0); 39 | addAnnotation(topLevelClass, method); 40 | method.addParameter(new Parameter(sqlMapClient, "sqlMapClient")); 41 | method.removeBodyLine(0); 42 | method.addBodyLine("super.setSqlMapClient(sqlMapClient);"); 43 | return true; 44 | } 45 | 46 | /** 47 | * 添加注解 48 | * 49 | * @param topLevelClass 50 | * @param method 51 | */ 52 | protected void addAnnotation(TopLevelClass topLevelClass, Method method) { 53 | topLevelClass.addImportedType(sqlMapClient); 54 | topLevelClass.addImportedType(autowired); 55 | topLevelClass.addImportedType(repository); 56 | method.addAnnotation("@Autowired"); 57 | topLevelClass.addAnnotation("@Repository"); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/resources/generatorConfigIbatis2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 |
29 |
30 |
31 |
32 |
33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 |
44 |
45 |
46 | -------------------------------------------------------------------------------- /src/test/resources/generatorConfigIbatis2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 |
29 |
30 |
31 |
32 |
33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 |
44 |
45 |
46 | -------------------------------------------------------------------------------- /src/main/resources/grid.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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 |
43 |
44 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/util/EqualsUtil.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.generator.internal.util; 2 | 3 | /** 4 | * This class is from javapractices.com: 5 | * 6 | * http://www.javapractices.com/Topic17.cjp 7 | * 8 | * Collected methods which allow easy implementation of equals. 9 | * 10 | * Example use case in a class called Car: 11 | * 12 | *
13 |  * public boolean equals(Object that) {
14 |  * 	if (this == that)
15 |  * 		return true;
16 |  * 	if (!(that instanceof Car))
17 |  * 		return false;
18 |  * 	Car thatCar = (Car) that;
19 |  * 	return EqualsUtil.areEqual(this.fName, that.fName) && EqualsUtil.areEqual(this.fNumDoors, that.fNumDoors)
20 |  * 			&& EqualsUtil.areEqual(this.fGasMileage, that.fGasMileage) && EqualsUtil.areEqual(this.fColor, that.fColor)
21 |  * 			&& Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks); // array!
22 |  * }
23 |  * 
24 | * 25 | * Arrays are not handled by this class. This is because the 26 | * Arrays.equals methods should be used for array fields. 27 | */ 28 | public final class EqualsUtil { 29 | 30 | static public boolean areEqual(boolean aThis, boolean aThat) { 31 | return aThis == aThat; 32 | } 33 | 34 | static public boolean areEqual(char aThis, char aThat) { 35 | return aThis == aThat; 36 | } 37 | 38 | static public boolean areEqual(long aThis, long aThat) { 39 | /* 40 | * Implementation Note Note that byte, short, and int are handled by 41 | * this method, through implicit conversion. 42 | */ 43 | return aThis == aThat; 44 | } 45 | 46 | static public boolean areEqual(float aThis, float aThat) { 47 | return Float.floatToIntBits(aThis) == Float.floatToIntBits(aThat); 48 | } 49 | 50 | static public boolean areEqual(double aThis, double aThat) { 51 | return Double.doubleToLongBits(aThis) == Double.doubleToLongBits(aThat); 52 | } 53 | 54 | /** 55 | * Possibly-null object field. 56 | * 57 | * Includes type-safe enumerations and collections, but does not include 58 | * arrays. See class comment. 59 | */ 60 | static public boolean areEqual(Object aThis, Object aThat) { 61 | return aThis == null ? aThat == null : aThis.equals(aThat); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/java/Parameter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.java; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * @author Jeff Butler 23 | */ 24 | public class Parameter { 25 | private String name; 26 | private FullyQualifiedJavaType type; 27 | 28 | private List annotations; 29 | 30 | public Parameter(FullyQualifiedJavaType type, String name) { 31 | super(); 32 | this.name = name; 33 | this.type = type; 34 | annotations = new ArrayList(); 35 | } 36 | 37 | public Parameter(FullyQualifiedJavaType type, String name, String annotation) { 38 | this(type, name); 39 | addAnnotation(annotation); 40 | } 41 | 42 | /** 43 | * @return Returns the name. 44 | */ 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | /** 50 | * @return Returns the type. 51 | */ 52 | public FullyQualifiedJavaType getType() { 53 | return type; 54 | } 55 | 56 | public List getAnnotations() { 57 | return annotations; 58 | } 59 | 60 | public void addAnnotation(String annotation) { 61 | annotations.add(annotation); 62 | } 63 | 64 | public String getFormattedContent() { 65 | StringBuilder sb = new StringBuilder(); 66 | 67 | for (String annotation : annotations) { 68 | sb.append(annotation); 69 | sb.append(' '); 70 | } 71 | 72 | sb.append(type.getShortName()); 73 | sb.append(' '); 74 | sb.append(name); 75 | 76 | return sb.toString(); 77 | } 78 | 79 | @Override 80 | public String toString() { 81 | return getFormattedContent(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/util/ClassloaderUtility.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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 | package org.mybatis.generator.internal.util; 18 | 19 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 20 | 21 | import java.io.File; 22 | import java.net.MalformedURLException; 23 | import java.net.URL; 24 | import java.net.URLClassLoader; 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | /** 29 | * This class holds methods useful for constructing custom classloaders. 30 | * 31 | * @author Jeff Butler 32 | * 33 | */ 34 | public class ClassloaderUtility { 35 | 36 | /** 37 | * Utility Class - No Instances 38 | */ 39 | private ClassloaderUtility() { 40 | } 41 | 42 | public static ClassLoader getCustomClassloader(List entries) { 43 | List urls = new ArrayList(); 44 | File file; 45 | 46 | if (entries != null) { 47 | for (String classPathEntry : entries) { 48 | file = new File(classPathEntry); 49 | if (!file.exists()) { 50 | throw new RuntimeException(getString("RuntimeError.9", classPathEntry)); //$NON-NLS-1$ 51 | } 52 | 53 | try { 54 | urls.add(file.toURI().toURL()); 55 | } catch (MalformedURLException e) { 56 | // this shouldn't happen, but just in case... 57 | throw new RuntimeException(getString("RuntimeError.9", classPathEntry)); //$NON-NLS-1$ 58 | } 59 | } 60 | } 61 | 62 | ClassLoader parent = Thread.currentThread().getContextClassLoader(); 63 | 64 | URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls.size()]), parent); 65 | 66 | return ucl; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/GeneratedJavaFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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 org.mybatis.generator.api.dom.java.CompilationUnit; 19 | 20 | /** 21 | * @author Jeff Butler 22 | */ 23 | public class GeneratedJavaFile extends GeneratedFile { 24 | private CompilationUnit compilationUnit; 25 | 26 | /** 27 | * Default constructor 28 | */ 29 | public GeneratedJavaFile(CompilationUnit compilationUnit, String targetProject) { 30 | super(targetProject); 31 | this.compilationUnit = compilationUnit; 32 | } 33 | 34 | @Override 35 | public String getFormattedContent() { 36 | return compilationUnit.getFormattedContent(); 37 | } 38 | 39 | @Override 40 | public String getFileName() { 41 | return compilationUnit.getType().getShortName() + ".java"; //$NON-NLS-1$ 42 | } 43 | 44 | public String getTargetPackage() { 45 | return compilationUnit.getType().getPackageName(); 46 | } 47 | 48 | /** 49 | * This method is required by the Eclipse Java merger. If you are not 50 | * running in Eclipse, or some other system that implements the Java merge 51 | * function, you may return null from this method. 52 | * 53 | * @return the CompilationUnit associated with this file, or null if the 54 | * file is not mergeable. 55 | */ 56 | public CompilationUnit getCompilationUnit() { 57 | return compilationUnit; 58 | } 59 | 60 | /** 61 | * A Java file is mergeable if the getCompilationUnit() method returns a 62 | * valid compilation unit. 63 | * 64 | */ 65 | @Override 66 | public boolean isMergeable() { 67 | return true; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/CountByExampleElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.xmlmapper.elements; 17 | 18 | import org.mybatis.generator.api.dom.xml.Attribute; 19 | import org.mybatis.generator.api.dom.xml.TextElement; 20 | import org.mybatis.generator.api.dom.xml.XmlElement; 21 | 22 | /** 23 | * 24 | * @author Jeff Butler 25 | * 26 | */ 27 | public class CountByExampleElementGenerator extends AbstractXmlElementGenerator { 28 | 29 | public CountByExampleElementGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addElements(XmlElement parentElement) { 35 | XmlElement answer = new XmlElement("select"); //$NON-NLS-1$ 36 | 37 | String fqjt = introspectedTable.getExampleType(); 38 | 39 | answer.addAttribute(new Attribute("id", introspectedTable.getCountByExampleStatementId())); //$NON-NLS-1$ 40 | answer.addAttribute(new Attribute("parameterType", fqjt)); //$NON-NLS-1$ 41 | answer.addAttribute(new Attribute("resultType", "java.lang.Integer")); //$NON-NLS-1$ //$NON-NLS-2$ 42 | 43 | context.getCommentGenerator().addComment(answer); 44 | 45 | StringBuilder sb = new StringBuilder(); 46 | sb.append("select count(*) from "); //$NON-NLS-1$ 47 | sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); 48 | answer.addElement(new TextElement(sb.toString())); 49 | answer.addElement(getExampleIncludeElement()); 50 | 51 | if (context.getPlugins().sqlMapCountByExampleElementGenerated(answer, introspectedTable)) { 52 | parentElement.addElement(answer); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/resources/grid.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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 |
43 |
44 | -------------------------------------------------------------------------------- /doc/html/configreference/properties.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | The <properties> Element 7 | 8 | 9 | 10 | 11 | 17 | 18 |

The <properties> Element

19 |

The <properties> element is used to specify an external properties file 20 | for use in the parsing of the configuration. Any attribute in the 21 | configuration will accept a property in the form ${property}. 22 | The specified properties file will be searched for a matching value and the 23 | matching value will be substituted. The properties file is of the normal 24 | format for a Java properties file.

25 | 26 |

The <properties> element is a child element of the 27 | <generatorConfiguration> 28 | element.

29 | 30 | 31 |

Required Attributes

32 |

One, and only one, of the following attributes is required.

33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 44 | 45 | 46 | 47 | 50 | 51 |
AttributeDescription
resourceThe qualified name of the property file. When specifying the resource, the 41 | classpath will be searched for the properties file. So a file specified as 42 | com/myproject/generatorConfig.properties must exist in the 43 | com.myproject package.
urlA URL value to use for the property file. This can be used to specify a property 48 | file in a specific place on the file system when used in a form like 49 | file:///C:/myfolder/generatorConfig.properties.
52 | 53 |

Optional Attributes

54 |

None

55 | 56 |

Child Elements

57 |

None

58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedInsertSelectiveMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 The MyBatis Team 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.mybatis3.javamapper.elements.annotated; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.Interface; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertSelectiveMethodGenerator; 22 | import org.mybatis.generator.config.GeneratedKey; 23 | 24 | /** 25 | * 26 | * @author Jeff Butler 27 | */ 28 | public class AnnotatedInsertSelectiveMethodGenerator extends InsertSelectiveMethodGenerator { 29 | 30 | public AnnotatedInsertSelectiveMethodGenerator() { 31 | super(); 32 | } 33 | 34 | @Override 35 | public void addMapperAnnotations(Interface interfaze, Method method) { 36 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); 37 | interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.InsertProvider")); //$NON-NLS-1$ 38 | StringBuilder sb = new StringBuilder(); 39 | sb.append("@InsertProvider(type="); //$NON-NLS-1$ 40 | sb.append(fqjt.getShortName()); 41 | sb.append(".class, method=\""); //$NON-NLS-1$ 42 | sb.append(introspectedTable.getInsertSelectiveStatementId()); 43 | sb.append("\")"); //$NON-NLS-1$ 44 | 45 | method.addAnnotation(sb.toString()); 46 | 47 | GeneratedKey gk = introspectedTable.getGeneratedKey(); 48 | if (gk != null) { 49 | addGeneratedKeyAnnotation(interfaze, method, gk); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/util/messages/Messages.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.messages; 17 | 18 | import java.text.MessageFormat; 19 | import java.util.MissingResourceException; 20 | import java.util.ResourceBundle; 21 | 22 | /** 23 | * @author Jeff Butler 24 | */ 25 | public class Messages { 26 | private static final String BUNDLE_NAME = "org.mybatis.generator.internal.util.messages.messages"; //$NON-NLS-1$ 27 | 28 | private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); 29 | 30 | private Messages() { 31 | } 32 | 33 | public static String getString(String key) { 34 | try { 35 | return RESOURCE_BUNDLE.getString(key); 36 | } catch (MissingResourceException e) { 37 | return '!' + key + '!'; 38 | } 39 | } 40 | 41 | public static String getString(String key, String parm1) { 42 | try { 43 | return MessageFormat.format(RESOURCE_BUNDLE.getString(key), new Object[] { parm1 }); 44 | } catch (MissingResourceException e) { 45 | return '!' + key + '!'; 46 | } 47 | } 48 | 49 | public static String getString(String key, String parm1, String parm2) { 50 | try { 51 | return MessageFormat.format(RESOURCE_BUNDLE.getString(key), new Object[] { parm1, parm2 }); 52 | } catch (MissingResourceException e) { 53 | return '!' + key + '!'; 54 | } 55 | } 56 | 57 | public static String getString(String key, String parm1, String parm2, String parm3) { 58 | try { 59 | return MessageFormat.format(RESOURCE_BUNDLE.getString(key), new Object[] { parm1, parm2, parm3 }); 60 | } catch (MissingResourceException e) { 61 | return '!' + key + '!'; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/resources/mysql_bank.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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
43 |
44 |
45 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/rules/HierarchicalModelRules.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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 | package org.mybatis.generator.internal.rules; 18 | 19 | import org.mybatis.generator.api.IntrospectedTable; 20 | 21 | /** 22 | * This class encapsulates all the code generation rules for a table using the 23 | * hierarchical model. 24 | * 25 | * @author Jeff Butler 26 | * 27 | */ 28 | public class HierarchicalModelRules extends BaseRules { 29 | 30 | /** 31 | * 32 | */ 33 | public HierarchicalModelRules(IntrospectedTable introspectedTable) { 34 | super(introspectedTable); 35 | } 36 | 37 | /** 38 | * Implements the rule for determining whether to generate a primary key 39 | * class. If the physical table has a primary key, then we generate the 40 | * class. 41 | * 42 | * @return true if the primary key should be generated 43 | */ 44 | public boolean generatePrimaryKeyClass() { 45 | return introspectedTable.hasPrimaryKeyColumns(); 46 | } 47 | 48 | /** 49 | * Implements the rule for generating a base record. If the table has fields 50 | * that are not in the primary key, and non-BLOB fields, then generate the 51 | * class. 52 | * 53 | * @return true if the class should be generated 54 | */ 55 | public boolean generateBaseRecordClass() { 56 | return introspectedTable.hasBaseColumns(); 57 | } 58 | 59 | /** 60 | * Implements the rule for generating a record with BLOBs. A record with 61 | * BLOBs is generated if the table contains any BLOB fields. 62 | * 63 | * @return true if the record with BLOBs class should be generated 64 | */ 65 | public boolean generateRecordWithBLOBsClass() { 66 | return introspectedTable.hasBLOBColumns(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/test/resources/grid1.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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
43 |
44 |
45 | -------------------------------------------------------------------------------- /doc/html/running/running.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Running MyBatis Generator 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Running MyBatis Generator

19 |

MyBatis Generator (MBG) can be run in the following ways:

20 | 29 |

Each method is described in detail on the linked pages.

30 |

Note: there is also an Eclipse 31 | plugin for MBG that adds extra function - namely good integration into Eclipse, 32 | an Eclipse enabled Ant task, and support for automatic merging of 33 | Java files. See the 34 | MyBatis 35 | web site page for information on installing the Eclipse plugin.

36 | 37 |

Important: When running outside of an IDE environment like Eclipse, 38 | MBG interprets the targetProject and 39 | targetPackage attributes in all XML configurations as follows:

40 |
    41 |
  • targetProject is assumed to be an existing directory structure. 42 | MBG will fail if this directory structure does not exist. There is one exception to this 43 | rule - when MBG is running through the Maven plugin. See the Maven plugin page 44 | for information about how targetProject is interpreted in Maven.
  • 45 |
  • targetPackage will be translated to a suitable subdirectory 46 | structure of the targetProject 47 | directory structure. MBG will create these subdirectories if necessary.
  • 48 |
49 | 50 | -------------------------------------------------------------------------------- /doc/html/migratingFromIbator.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Migrating from Ibator 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Migrating from Ibator

19 |

This page details changes between MyBatis Generator (MBG) 20 | and Ibator. For most users, the changes should be simple. 21 | If you extended any of Ibator's classes to supply custom implementations 22 | of code generators or the Java type resolver, you will need to rework 23 | those custom classes.

24 |

Changes 25 | are described assuming you are using XML configuration for MBG. If you 26 | are using Java based configuration, then the changes are still required 27 | and should be easy to deduce from the description of the XML changes.

28 |

Important Note: MBG will correctly parse older Ibator configuration files, so no change is actually 29 | required. However, new features will only be implemented in configuration files 30 | that conform to the MyBatis Generator DTD.

31 |

Required for Some Users

32 |
    33 |
  • The DTD has changed. The new DOCTYPE should be 34 |
    35 | 
    36 | <!DOCTYPE generatorConfiguration
    37 |   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    38 |   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    39 |     
    40 |
  • 41 |
  • MyBatis3 is now the default target runtime.
  • 42 |
  • The <ibatorConfiguration> element is renamed to 43 | <generatorConfiguration>
  • 44 |
  • The <ibatorContext> element is renamed to 45 | <context>
  • 46 |
  • The <ibatorPlugin> element is renamed to 47 | <plugin>
  • 48 |
  • The <daoGenerator> element is renamed to 49 | <javaClientGenerator>
  • 50 |
  • If you implemented a plugin, the daoXXX methods have been renamed 51 | to clientXXX.
  • 52 |
53 | 54 | -------------------------------------------------------------------------------- /doc/html/configreference/classPathEntry.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | The <classPathEntry> Element 7 | 8 | 9 | 10 | 11 | 17 | 18 |

The <classPathEntry> Element

19 |

The <classPathEntry> element is used to add classpath locations to the 20 | classpath of the MyBatis Generator (MBG) run. The <classPathEntry> element is an option child 21 | element of the <generatorConfiguration> element. 22 | MBG loads classes from these locations in these instances:

23 |
    24 |
  • When loading the JDBC driver for database introspection
  • 25 |
  • When loading root classes in the JavaModelGenerator to check for overridden 26 | methods
  • 27 |
28 | 29 |

This element is optional and is not required if you setup the classpath externally to MBG 30 | (e.g. with the -cp argument of the java command

31 | 32 |

Important Note: these locations are NOT used when loading classes that 33 | extend one of MBG's classes or implement one of MBG's interfaces. In those cases 34 | you must add your external classes to the runtime classpath in the same way you 35 | add MBG to the classpath (e.g. with the -cp argument of the 36 | java command).

37 | 38 |

Required Attributes

39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 48 | 49 |
AttributeDescription
locationThe full path name of a JAR/ZIP file to add to the classpath, or 47 | a directory to add to the classpath.
50 | 51 |

Optional Attributes

52 |

None

53 | 54 |

Child Elements

55 |

None

56 | 57 | 58 |

Example

59 |

This element specifies the location of a DB2 JDBC driver:

60 |
61 | <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
62 | 
63 | 64 | 65 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/sqlmap/elements/DeleteByExampleElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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.ibatis2.sqlmap.elements; 17 | 18 | import org.mybatis.generator.api.dom.xml.Attribute; 19 | import org.mybatis.generator.api.dom.xml.TextElement; 20 | import org.mybatis.generator.api.dom.xml.XmlElement; 21 | 22 | /** 23 | * 24 | * @author Jeff Butler 25 | * 26 | */ 27 | public class DeleteByExampleElementGenerator extends AbstractXmlElementGenerator { 28 | 29 | public DeleteByExampleElementGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addElements(XmlElement parentElement) { 35 | XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$ 36 | 37 | answer.addAttribute(new Attribute("id", introspectedTable.getDeleteByExampleStatementId())); //$NON-NLS-1$ 38 | answer.addAttribute(new Attribute("parameterClass", introspectedTable.getExampleType())); //$NON-NLS-1$ 39 | 40 | context.getCommentGenerator().addComment(answer); 41 | 42 | StringBuilder sb = new StringBuilder(); 43 | sb.append("delete from "); //$NON-NLS-1$ 44 | sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); 45 | answer.addElement(new TextElement(sb.toString())); 46 | 47 | XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$ 48 | sb.setLength(0); 49 | sb.append(introspectedTable.getIbatis2SqlMapNamespace()); 50 | sb.append('.'); 51 | sb.append(introspectedTable.getExampleWhereClauseId()); 52 | includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$ 53 | sb.toString())); 54 | 55 | answer.addElement(includeElement); 56 | 57 | if (context.getPlugins().sqlMapDeleteByExampleElementGenerated(answer, introspectedTable)) { 58 | parentElement.addElement(answer); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/sqlmap/elements/BaseColumnListElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.ibatis2.sqlmap.elements; 17 | 18 | import java.util.Iterator; 19 | 20 | import org.mybatis.generator.api.IntrospectedColumn; 21 | import org.mybatis.generator.api.dom.xml.Attribute; 22 | import org.mybatis.generator.api.dom.xml.TextElement; 23 | import org.mybatis.generator.api.dom.xml.XmlElement; 24 | import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities; 25 | 26 | /** 27 | * 28 | * @author Jeff Butler 29 | * 30 | */ 31 | public class BaseColumnListElementGenerator extends AbstractXmlElementGenerator { 32 | 33 | public BaseColumnListElementGenerator() { 34 | super(); 35 | } 36 | 37 | @Override 38 | public void addElements(XmlElement parentElement) { 39 | XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$ 40 | 41 | answer.addAttribute(new Attribute("id", //$NON-NLS-1$ 42 | introspectedTable.getBaseColumnListId())); 43 | 44 | context.getCommentGenerator().addComment(answer); 45 | 46 | StringBuilder sb = new StringBuilder(); 47 | Iterator iter = introspectedTable.getNonBLOBColumns().iterator(); 48 | while (iter.hasNext()) { 49 | sb.append(Ibatis2FormattingUtilities.getSelectListPhrase(iter.next())); 50 | 51 | if (iter.hasNext()) { 52 | sb.append(", "); //$NON-NLS-1$ 53 | } 54 | 55 | if (sb.length() > 80) { 56 | answer.addElement(new TextElement(sb.toString())); 57 | sb.setLength(0); 58 | } 59 | } 60 | 61 | if (sb.length() > 0) { 62 | answer.addElement((new TextElement(sb.toString()))); 63 | } 64 | 65 | if (context.getPlugins().sqlMapBaseColumnListElementGenerated(answer, introspectedTable)) { 66 | parentElement.addElement(answer); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/sqlmap/elements/BlobColumnListElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.ibatis2.sqlmap.elements; 17 | 18 | import java.util.Iterator; 19 | 20 | import org.mybatis.generator.api.IntrospectedColumn; 21 | import org.mybatis.generator.api.dom.xml.Attribute; 22 | import org.mybatis.generator.api.dom.xml.TextElement; 23 | import org.mybatis.generator.api.dom.xml.XmlElement; 24 | import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities; 25 | 26 | /** 27 | * 28 | * @author Jeff Butler 29 | * 30 | */ 31 | public class BlobColumnListElementGenerator extends AbstractXmlElementGenerator { 32 | 33 | public BlobColumnListElementGenerator() { 34 | super(); 35 | } 36 | 37 | @Override 38 | public void addElements(XmlElement parentElement) { 39 | XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$ 40 | 41 | answer.addAttribute(new Attribute("id", //$NON-NLS-1$ 42 | introspectedTable.getBlobColumnListId())); 43 | 44 | context.getCommentGenerator().addComment(answer); 45 | 46 | StringBuilder sb = new StringBuilder(); 47 | 48 | Iterator iter = introspectedTable.getBLOBColumns().iterator(); 49 | while (iter.hasNext()) { 50 | sb.append(Ibatis2FormattingUtilities.getSelectListPhrase(iter.next())); 51 | 52 | if (iter.hasNext()) { 53 | sb.append(", "); //$NON-NLS-1$ 54 | } 55 | 56 | if (sb.length() > 80) { 57 | answer.addElement(new TextElement(sb.toString())); 58 | sb.setLength(0); 59 | } 60 | } 61 | 62 | if (sb.length() > 0) { 63 | answer.addElement((new TextElement(sb.toString()))); 64 | } 65 | 66 | if (context.getPlugins().sqlMapBlobColumnListElementGenerated(answer, introspectedTable)) { 67 | parentElement.addElement(answer); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/xml/ParserErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.config.xml; 17 | 18 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 19 | 20 | import java.util.List; 21 | 22 | import org.xml.sax.ErrorHandler; 23 | import org.xml.sax.SAXException; 24 | import org.xml.sax.SAXParseException; 25 | 26 | /** 27 | * @author Jeff Butler 28 | */ 29 | public class ParserErrorHandler implements ErrorHandler { 30 | private List warnings; 31 | 32 | private List errors; 33 | 34 | /** 35 | * 36 | */ 37 | public ParserErrorHandler(List warnings, List errors) { 38 | super(); 39 | this.warnings = warnings; 40 | this.errors = errors; 41 | } 42 | 43 | /* 44 | * (non-Javadoc) 45 | * 46 | * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) 47 | */ 48 | public void warning(SAXParseException exception) throws SAXException { 49 | warnings.add(getString("Warning.7", //$NON-NLS-1$ 50 | Integer.toString(exception.getLineNumber()), exception.getMessage())); 51 | } 52 | 53 | /* 54 | * (non-Javadoc) 55 | * 56 | * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) 57 | */ 58 | public void error(SAXParseException exception) throws SAXException { 59 | errors.add(getString("RuntimeError.4", //$NON-NLS-1$ 60 | Integer.toString(exception.getLineNumber()), exception.getMessage())); 61 | } 62 | 63 | /* 64 | * (non-Javadoc) 65 | * 66 | * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) 67 | */ 68 | public void fatalError(SAXParseException exception) throws SAXException { 69 | errors.add(getString("RuntimeError.4", //$NON-NLS-1$ 70 | Integer.toString(exception.getLineNumber()), exception.getMessage())); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/BaseColumnListElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.xmlmapper.elements; 17 | 18 | import java.util.Iterator; 19 | 20 | import org.mybatis.generator.api.IntrospectedColumn; 21 | import org.mybatis.generator.api.dom.xml.Attribute; 22 | import org.mybatis.generator.api.dom.xml.TextElement; 23 | import org.mybatis.generator.api.dom.xml.XmlElement; 24 | import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; 25 | 26 | /** 27 | * 28 | * @author Jeff Butler 29 | * 30 | */ 31 | public class BaseColumnListElementGenerator extends AbstractXmlElementGenerator { 32 | 33 | public BaseColumnListElementGenerator() { 34 | super(); 35 | } 36 | 37 | @Override 38 | public void addElements(XmlElement parentElement) { 39 | XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$ 40 | 41 | answer.addAttribute(new Attribute("id", //$NON-NLS-1$ 42 | introspectedTable.getBaseColumnListId())); 43 | 44 | context.getCommentGenerator().addComment(answer); 45 | 46 | StringBuilder sb = new StringBuilder(); 47 | Iterator iter = introspectedTable.getNonBLOBColumns().iterator(); 48 | while (iter.hasNext()) { 49 | sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter.next())); 50 | 51 | if (iter.hasNext()) { 52 | sb.append(", "); //$NON-NLS-1$ 53 | } 54 | 55 | if (sb.length() > 80) { 56 | answer.addElement(new TextElement(sb.toString())); 57 | sb.setLength(0); 58 | } 59 | } 60 | 61 | if (sb.length() > 0) { 62 | answer.addElement((new TextElement(sb.toString()))); 63 | } 64 | 65 | if (context.getPlugins().sqlMapBaseColumnListElementGenerated(answer, introspectedTable)) { 66 | parentElement.addElement(answer); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/BlobColumnListElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.xmlmapper.elements; 17 | 18 | import java.util.Iterator; 19 | 20 | import org.mybatis.generator.api.IntrospectedColumn; 21 | import org.mybatis.generator.api.dom.xml.Attribute; 22 | import org.mybatis.generator.api.dom.xml.TextElement; 23 | import org.mybatis.generator.api.dom.xml.XmlElement; 24 | import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; 25 | 26 | /** 27 | * 28 | * @author Jeff Butler 29 | * 30 | */ 31 | public class BlobColumnListElementGenerator extends AbstractXmlElementGenerator { 32 | 33 | public BlobColumnListElementGenerator() { 34 | super(); 35 | } 36 | 37 | @Override 38 | public void addElements(XmlElement parentElement) { 39 | XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$ 40 | 41 | answer.addAttribute(new Attribute("id", //$NON-NLS-1$ 42 | introspectedTable.getBlobColumnListId())); 43 | 44 | context.getCommentGenerator().addComment(answer); 45 | 46 | StringBuilder sb = new StringBuilder(); 47 | 48 | Iterator iter = introspectedTable.getBLOBColumns().iterator(); 49 | while (iter.hasNext()) { 50 | sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter.next())); 51 | 52 | if (iter.hasNext()) { 53 | sb.append(", "); //$NON-NLS-1$ 54 | } 55 | 56 | if (sb.length() > 80) { 57 | answer.addElement(new TextElement(sb.toString())); 58 | sb.setLength(0); 59 | } 60 | } 61 | 62 | if (sb.length() > 0) { 63 | answer.addElement((new TextElement(sb.toString()))); 64 | } 65 | 66 | if (context.getPlugins().sqlMapBlobColumnListElementGenerated(answer, introspectedTable)) { 67 | parentElement.addElement(answer); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/resources/authority_mysql.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 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 |
40 | 41 |
42 | 43 |
44 | 45 |
46 | 47 |
48 | 49 |
50 | 51 |
52 |
53 |
54 | -------------------------------------------------------------------------------- /src/test/resources/datacheck_mysql.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 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 39 | 40 | 41 | 42 | 43 | 44 |
45 |
46 | 47 |
48 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/GeneratedFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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 | /** 19 | * Abstract class that holds information common to all generated files. 20 | * 21 | * @author Jeff Butler 22 | */ 23 | public abstract class GeneratedFile { 24 | private String targetProject; 25 | 26 | /** 27 | * 28 | */ 29 | public GeneratedFile(String targetProject) { 30 | super(); 31 | this.targetProject = targetProject; 32 | } 33 | 34 | /** 35 | * This method returns the entire contents of the generated file. Clients 36 | * can simply save the value returned from this method as the file contents. 37 | * Subclasses such as @see org.mybatis.generator.api.GeneratedJavaFile offer 38 | * more fine grained access to file parts, but still implement this method 39 | * in the event that the entire contents are desired. 40 | * 41 | * @return Returns the content. 42 | */ 43 | public abstract String getFormattedContent(); 44 | 45 | /** 46 | * Get the file name (without any path). Clients should use this method to 47 | * determine how to save the results. 48 | * 49 | * @return Returns the file name. 50 | */ 51 | public abstract String getFileName(); 52 | 53 | /** 54 | * Gets the target project. Clients can call this method to determine how to 55 | * save the results. 56 | * 57 | * @return the target project 58 | */ 59 | public String getTargetProject() { 60 | return targetProject; 61 | } 62 | 63 | /** 64 | * Get the target package for the file. Clients should use this method to 65 | * determine how to save the results. 66 | * 67 | * @return Returns the target project. 68 | */ 69 | public abstract String getTargetPackage(); 70 | 71 | @Override 72 | public String toString() { 73 | return getFormattedContent(); 74 | } 75 | 76 | public abstract boolean isMergeable(); 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/InsertMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class InsertMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public InsertMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | Set importedTypes = new TreeSet(); 41 | Method method = new Method(); 42 | 43 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 44 | method.setVisibility(JavaVisibility.PUBLIC); 45 | method.setName(introspectedTable.getInsertStatementId()); 46 | 47 | FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); 48 | 49 | importedTypes.add(parameterType); 50 | method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ 51 | 52 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable,"保存记录,不管记录里面的属性是否为空"); 53 | 54 | addMapperAnnotations(interfaze, method); 55 | 56 | if (context.getPlugins().clientInsertMethodGenerated(method, interfaze, introspectedTable)) { 57 | interfaze.addImportedTypes(importedTypes); 58 | interfaze.addMethod(method); 59 | } 60 | } 61 | 62 | public void addMapperAnnotations(Interface interfaze, Method method) { 63 | return; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/CountByExampleMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class CountByExampleMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public CountByExampleMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType()); 41 | 42 | Set importedTypes = new TreeSet(); 43 | importedTypes.add(fqjt); 44 | 45 | Method method = new Method(); 46 | method.setVisibility(JavaVisibility.PUBLIC); 47 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 48 | method.setName(introspectedTable.getCountByExampleStatementId()); 49 | method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$ 50 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable, "根据条件查询记录总数"); 51 | 52 | addMapperAnnotations(interfaze, method); 53 | 54 | if (context.getPlugins().clientCountByExampleMethodGenerated(method, interfaze, introspectedTable)) { 55 | interfaze.addImportedTypes(importedTypes); 56 | interfaze.addMethod(method); 57 | } 58 | } 59 | 60 | public void addMapperAnnotations(Interface interfaze, Method method) { 61 | return; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/DeleteByExampleMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class DeleteByExampleMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public DeleteByExampleMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | Set importedTypes = new TreeSet(); 41 | FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); 42 | importedTypes.add(type); 43 | 44 | Method method = new Method(); 45 | method.setVisibility(JavaVisibility.PUBLIC); 46 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 47 | method.setName(introspectedTable.getDeleteByExampleStatementId()); 48 | method.addParameter(new Parameter(type, "example")); //$NON-NLS-1$ 49 | 50 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable,"根据条件删除记录"); 51 | 52 | addMapperAnnotations(interfaze, method); 53 | 54 | if (context.getPlugins().clientDeleteByExampleMethodGenerated(method, interfaze, introspectedTable)) { 55 | interfaze.addImportedTypes(importedTypes); 56 | interfaze.addMethod(method); 57 | } 58 | } 59 | 60 | public void addMapperAnnotations(Interface interfaze, Method method) { 61 | return; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/api/dom/xml/Document.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.dom.xml; 17 | 18 | import org.mybatis.generator.api.dom.OutputUtilities; 19 | 20 | /** 21 | * @author Jeff Butler 22 | */ 23 | public class Document { 24 | private String publicId; 25 | 26 | private String systemId; 27 | 28 | private XmlElement rootElement; 29 | 30 | /** 31 | * 32 | */ 33 | public Document(String publicId, String systemId) { 34 | super(); 35 | this.publicId = publicId; 36 | this.systemId = systemId; 37 | } 38 | 39 | public Document() { 40 | super(); 41 | } 42 | 43 | /** 44 | * @return Returns the rootElement. 45 | */ 46 | public XmlElement getRootElement() { 47 | return rootElement; 48 | } 49 | 50 | /** 51 | * @param rootElement 52 | * The rootElement to set. 53 | */ 54 | public void setRootElement(XmlElement rootElement) { 55 | this.rootElement = rootElement; 56 | } 57 | 58 | /** 59 | * @return Returns the publicId. 60 | */ 61 | public String getPublicId() { 62 | return publicId; 63 | } 64 | 65 | /** 66 | * @return Returns the systemId. 67 | */ 68 | public String getSystemId() { 69 | return systemId; 70 | } 71 | 72 | public String getFormattedContent() { 73 | StringBuilder sb = new StringBuilder(); 74 | 75 | sb.append(""); //$NON-NLS-1$ 76 | 77 | if (publicId != null && systemId != null) { 78 | OutputUtilities.newLine(sb); 79 | sb.append(""); //$NON-NLS-1$ 86 | } 87 | 88 | OutputUtilities.newLine(sb); 89 | sb.append(rootElement.getFormattedContent(0)); 90 | 91 | return sb.toString(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/InsertSelectiveMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class InsertSelectiveMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public InsertSelectiveMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | Set importedTypes = new TreeSet(); 41 | Method method = new Method(); 42 | 43 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 44 | method.setVisibility(JavaVisibility.PUBLIC); 45 | method.setName(introspectedTable.getInsertSelectiveStatementId()); 46 | 47 | FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); 48 | 49 | importedTypes.add(parameterType); 50 | method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ 51 | 52 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable,"保存属性不为空的记录"); 53 | 54 | addMapperAnnotations(interfaze, method); 55 | 56 | if (context.getPlugins().clientInsertSelectiveMethodGenerated(method, interfaze, introspectedTable)) { 57 | interfaze.addImportedTypes(importedTypes); 58 | interfaze.addMethod(method); 59 | } 60 | } 61 | 62 | public void addMapperAnnotations(Interface interfaze, Method method) { 63 | return; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/XmlConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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 | /** 19 | * @author Jeff Butler 20 | */ 21 | public class XmlConstants { 22 | 23 | /** 24 | * Utility Class, no instances 25 | */ 26 | private XmlConstants() { 27 | super(); 28 | } 29 | 30 | public static final String IBATIS2_SQL_MAP_SYSTEM_ID = "http://ibatis.apache.org/dtd/sql-map-2.dtd"; //$NON-NLS-1$ 31 | 32 | public static final String IBATIS2_SQL_MAP_PUBLIC_ID = "-//ibatis.apache.org//DTD SQL Map 2.0//EN"; //$NON-NLS-1$ 33 | 34 | public static final String IBATIS2_SQL_MAP_CONFIG_SYSTEM_ID = "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"; //$NON-NLS-1$ 35 | 36 | public static final String IBATIS2_SQL_MAP_CONFIG_PUBLIC_ID = "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"; //$NON-NLS-1$ 37 | 38 | public static final String MYBATIS3_MAPPER_SYSTEM_ID = "http://mybatis.org/dtd/mybatis-3-mapper.dtd"; //$NON-NLS-1$ 39 | 40 | public static final String MYBATIS3_MAPPER_PUBLIC_ID = "-//mybatis.org//DTD Mapper 3.0//EN"; //$NON-NLS-1$ 41 | 42 | public static final String MYBATIS3_MAPPER_CONFIG_SYSTEM_ID = "http://mybatis.org/dtd/mybatis-3-config.dtd"; //$NON-NLS-1$ 43 | 44 | public static final String MYBATIS3_MAPPER_CONFIG_PUBLIC_ID = "-//mybatis.org//DTD Config 3.0//EN"; //$NON-NLS-1$ 45 | 46 | public static final String IBATOR_CONFIG_SYSTEM_ID = "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd"; //$NON-NLS-1$ 47 | 48 | public static final String IBATOR_CONFIG_PUBLIC_ID = "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN"; //$NON-NLS-1$ 49 | 50 | public static final String MYBATIS_GENERATOR_CONFIG_SYSTEM_ID = "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"; //$NON-NLS-1$ 51 | 52 | public static final String MYBATIS_GENERATOR_CONFIG_PUBLIC_ID = "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"; //$NON-NLS-1$ 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/sqlmap/elements/CountByExampleElementGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 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.ibatis2.sqlmap.elements; 17 | 18 | import org.mybatis.generator.api.dom.xml.Attribute; 19 | import org.mybatis.generator.api.dom.xml.TextElement; 20 | import org.mybatis.generator.api.dom.xml.XmlElement; 21 | 22 | /** 23 | * 24 | * @author Jeff Butler 25 | * 26 | */ 27 | public class CountByExampleElementGenerator extends AbstractXmlElementGenerator { 28 | 29 | public CountByExampleElementGenerator() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void addElements(XmlElement parentElement) { 35 | XmlElement answer = new XmlElement("select"); //$NON-NLS-1$ 36 | 37 | answer.addAttribute(new Attribute("id", introspectedTable.getCountByExampleStatementId())); //$NON-NLS-1$ 38 | answer.addAttribute(new Attribute("parameterClass", introspectedTable.getExampleType())); //$NON-NLS-1$ 39 | answer.addAttribute(new Attribute("resultClass", "java.lang.Integer")); //$NON-NLS-1$ //$NON-NLS-2$ 40 | 41 | context.getCommentGenerator().addComment(answer); 42 | 43 | StringBuilder sb = new StringBuilder(); 44 | sb.append("select count(*) from "); //$NON-NLS-1$ 45 | sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); 46 | answer.addElement(new TextElement(sb.toString())); 47 | 48 | XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$ 49 | sb.setLength(0); 50 | sb.append(introspectedTable.getIbatis2SqlMapNamespace()); 51 | sb.append('.'); 52 | sb.append(introspectedTable.getExampleWhereClauseId()); 53 | includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$ 54 | sb.toString())); 55 | 56 | answer.addElement(includeElement); 57 | 58 | if (context.getPlugins().sqlMapCountByExampleElementGenerated(answer, introspectedTable)) { 59 | parentElement.addElement(answer); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/generator/MyBatisGeneratorTest.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.generator; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.fail; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.junit.Test; 10 | import org.mybatis.generator.api.MyBatisGenerator; 11 | import org.mybatis.generator.config.Configuration; 12 | import org.mybatis.generator.config.xml.ConfigurationParser; 13 | import org.mybatis.generator.exception.InvalidConfigurationException; 14 | import org.mybatis.generator.internal.DefaultShellCallback; 15 | 16 | public class MyBatisGeneratorTest { 17 | 18 | @Test 19 | public void testGenerateMyBatis3() throws Exception { 20 | List warnings = new ArrayList(); 21 | ConfigurationParser cp = new ConfigurationParser(warnings); 22 | Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigMyBatis3.xml")); 23 | 24 | DefaultShellCallback shellCallback = new DefaultShellCallback(true); 25 | 26 | boolean gotException = false; 27 | try { 28 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings); 29 | myBatisGenerator.generate(null); 30 | } catch (InvalidConfigurationException e) { 31 | assertEquals(3, e.getErrors().size()); 32 | gotException = true; 33 | } 34 | 35 | if (!gotException) { 36 | fail("Should throw InvalidConfigurationException"); 37 | } 38 | } 39 | 40 | @Test 41 | public void testGenerateIbatis2() throws Exception { 42 | List warnings = new ArrayList(); 43 | ConfigurationParser cp = new ConfigurationParser(warnings); 44 | Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigIbatis2.xml")); 45 | 46 | DefaultShellCallback shellCallback = new DefaultShellCallback(true); 47 | 48 | boolean gotException = false; 49 | try { 50 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings); 51 | myBatisGenerator.generate(null); 52 | } catch (InvalidConfigurationException e) { 53 | assertEquals(1, e.getErrors().size()); 54 | gotException = true; 55 | } 56 | 57 | if (!gotException) { 58 | fail("Should throw InvalidConfigurationException"); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/UpdateByPrimaryKeyWithoutBLOBsMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class UpdateByPrimaryKeyWithoutBLOBsMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public UpdateByPrimaryKeyWithoutBLOBsMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | Set importedTypes = new TreeSet(); 41 | FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); 42 | importedTypes.add(parameterType); 43 | 44 | Method method = new Method(); 45 | method.setVisibility(JavaVisibility.PUBLIC); 46 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 47 | method.setName(introspectedTable.getUpdateByPrimaryKeyStatementId()); 48 | method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ 49 | 50 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable, "根据主键更新记录"); 51 | 52 | addMapperAnnotations(interfaze, method); 53 | 54 | if (context.getPlugins().clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(method, interfaze, 55 | introspectedTable)) { 56 | interfaze.addImportedTypes(importedTypes); 57 | interfaze.addMethod(method); 58 | } 59 | } 60 | 61 | public void addMapperAnnotations(Interface interfaze, Method method) { 62 | return; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /doc/html/configreference/jdbcConnection.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | The <jdbcConnection> Element 7 | 8 | 9 | 10 | 11 | 17 | 18 |

The <jdbcConnection> Element

19 |

The <jdbcConnection> element is used to specify the properties of 20 | the database connection required to introspect tables. MyBatis Generator uses JDBC's 21 | DatabaseMetaData class to discover the properties of the tables you specify 22 | in the configuration. One <jdbcConnection> element is required for 23 | each <context> element.

24 | 25 |

Required Attributes

26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 |
AttributeDescription
driverClassThe fully qualified class name for the JDBC driver used to access the 34 | database. 35 |
connectionURLThe JDBC connection URL used to access the database.
42 | 43 |

Optional Attributes

44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 |
AttributeDescription
userIdThe user ID used to connect to the database.
passwordThe password used to connect to the database.
58 | 59 |

Child Elements

60 |
    61 |
  • <property> (0..N) 62 | Note: any properties specified here will be added to the 63 | properties of the JDBC driver.
  • 64 |
65 | 66 |

Example

67 |

This element will connect to a DB2 database configured as 68 | MBGTEST in the DB2 client configuration utility, using 69 | the default install location for the JDBC driver:

70 |
71 | <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
72 |     connectionURL="jdbc:db2:MBGTEST"
73 |     userId="db2admin"
74 |     password="db2admin">
75 | </jdbcConnection>
76 | 
77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/SqlMapGeneratorConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.config; 17 | 18 | import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; 19 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 20 | 21 | import java.util.List; 22 | 23 | import org.mybatis.generator.api.dom.xml.Attribute; 24 | import org.mybatis.generator.api.dom.xml.XmlElement; 25 | 26 | /** 27 | * @author Jeff Butler 28 | */ 29 | public class SqlMapGeneratorConfiguration extends PropertyHolder { 30 | private String targetPackage; 31 | 32 | private String targetProject; 33 | 34 | /** 35 | * 36 | */ 37 | public SqlMapGeneratorConfiguration() { 38 | super(); 39 | } 40 | 41 | public String getTargetProject() { 42 | return targetProject; 43 | } 44 | 45 | public void setTargetProject(String targetProject) { 46 | this.targetProject = targetProject; 47 | } 48 | 49 | public String getTargetPackage() { 50 | return targetPackage; 51 | } 52 | 53 | public void setTargetPackage(String targetPackage) { 54 | this.targetPackage = targetPackage; 55 | } 56 | 57 | public XmlElement toXmlElement() { 58 | XmlElement answer = new XmlElement("sqlMapGenerator"); //$NON-NLS-1$ 59 | 60 | if (targetPackage != null) { 61 | answer.addAttribute(new Attribute("targetPackage", targetPackage)); //$NON-NLS-1$ 62 | } 63 | 64 | if (targetProject != null) { 65 | answer.addAttribute(new Attribute("targetProject", targetProject)); //$NON-NLS-1$ 66 | } 67 | 68 | addPropertyXmlElements(answer); 69 | 70 | return answer; 71 | } 72 | 73 | public void validate(List errors, String contextId) { 74 | if (!stringHasValue(targetProject)) { 75 | errors.add(getString("ValidationError.1", contextId)); //$NON-NLS-1$ 76 | } 77 | 78 | if (!stringHasValue(targetPackage)) { 79 | errors.add(getString("ValidationError.12", //$NON-NLS-1$ 80 | "SQLMapGenerator", contextId)); //$NON-NLS-1$ 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/dao/templates/SpringDAOTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.ibatis2.dao.templates; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.JavaVisibility; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | 22 | /** 23 | * @author Jeff Butler 24 | */ 25 | public class SpringDAOTemplate extends AbstractDAOTemplate { 26 | 27 | /** 28 | * 29 | */ 30 | public SpringDAOTemplate() { 31 | super(); 32 | } 33 | 34 | @Override 35 | protected void configureConstructorTemplate() { 36 | Method method = new Method(); 37 | method.setConstructor(true); 38 | method.setVisibility(JavaVisibility.PUBLIC); 39 | method.addBodyLine("super();"); //$NON-NLS-1$ 40 | setConstructorTemplate(method); 41 | } 42 | 43 | @Override 44 | protected void configureDeleteMethodTemplate() { 45 | setDeleteMethodTemplate("getSqlMapClientTemplate().delete(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 46 | } 47 | 48 | @Override 49 | protected void configureInsertMethodTemplate() { 50 | setInsertMethodTemplate("getSqlMapClientTemplate().insert(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 51 | } 52 | 53 | @Override 54 | protected void configureQueryForListMethodTemplate() { 55 | setQueryForListMethodTemplate("getSqlMapClientTemplate().queryForList(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 56 | } 57 | 58 | @Override 59 | protected void configureQueryForObjectMethodTemplate() { 60 | setQueryForObjectMethodTemplate("getSqlMapClientTemplate().queryForObject(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 61 | } 62 | 63 | @Override 64 | protected void configureSuperClass() { 65 | setSuperClass(new FullyQualifiedJavaType("org.springframework.orm.ibatis.support.SqlMapClientDaoSupport")); //$NON-NLS-1$ 66 | } 67 | 68 | @Override 69 | protected void configureUpdateMethodTemplate() { 70 | setUpdateMethodTemplate("getSqlMapClientTemplate().update(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/config/JavaModelGeneratorConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005 The Apache Software Foundation 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.config; 17 | 18 | import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; 19 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 20 | 21 | import java.util.List; 22 | 23 | import org.mybatis.generator.api.dom.xml.Attribute; 24 | import org.mybatis.generator.api.dom.xml.XmlElement; 25 | 26 | /** 27 | * @author Jeff Butler 28 | */ 29 | public class JavaModelGeneratorConfiguration extends PropertyHolder { 30 | 31 | private String targetPackage; 32 | 33 | private String targetProject; 34 | 35 | /** 36 | * 37 | */ 38 | public JavaModelGeneratorConfiguration() { 39 | super(); 40 | } 41 | 42 | public String getTargetProject() { 43 | return targetProject; 44 | } 45 | 46 | public void setTargetProject(String targetProject) { 47 | this.targetProject = targetProject; 48 | } 49 | 50 | public String getTargetPackage() { 51 | return targetPackage; 52 | } 53 | 54 | public void setTargetPackage(String targetPackage) { 55 | this.targetPackage = targetPackage; 56 | } 57 | 58 | public XmlElement toXmlElement() { 59 | XmlElement answer = new XmlElement("javaModelGenerator"); //$NON-NLS-1$ 60 | 61 | if (targetPackage != null) { 62 | answer.addAttribute(new Attribute("targetPackage", targetPackage)); //$NON-NLS-1$ 63 | } 64 | 65 | if (targetProject != null) { 66 | answer.addAttribute(new Attribute("targetProject", targetProject)); //$NON-NLS-1$ 67 | } 68 | 69 | addPropertyXmlElements(answer); 70 | 71 | return answer; 72 | } 73 | 74 | public void validate(List errors, String contextId) { 75 | if (!stringHasValue(targetProject)) { 76 | errors.add(getString("ValidationError.0", contextId)); //$NON-NLS-1$ 77 | } 78 | 79 | if (!stringHasValue(targetPackage)) { 80 | errors.add(getString("ValidationError.12", //$NON-NLS-1$ 81 | "JavaModelGenerator", contextId)); //$NON-NLS-1$ 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /doc/html/reference/building.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Building from Source 7 | 8 | 9 | 10 | 11 | 17 | 18 |

Building from Source

19 |

All MyBatis Generator (MBG) distributions include source code. The only compile time dependency 20 | is on ant.jar - for successful compilation of the included Ant task. 21 | It is straight forward to compile MBG from source - simply unzip the source in 22 | a distribution and compile it with your favorite tool.

23 |

The MBG distribution does not contain the tests that are run during the build, 24 | or other classes that are a necessary part of the build. If you would like to 25 | inspect those classes, or build MBG from the very latest version of the source code 26 | at Google then follow these steps:

27 |
    28 |
  1. MBG is built with Apache Maven. First you must get maven 29 | running. If you are new to Maven, here are the most simple steps (for Windows):
  2. 30 |
      31 |
    1. Download a Maven distribution from 32 | http://maven.apache.org/
    2. 33 |
    3. Unzip the distribution somewhere convenient
    4. 34 |
    5. Setup environment variables and the PATH. Examples:
      35 | set JAVA_HOME=C:\JavaTools\jdk1.6.0_17
      36 | set M2_HOME=C:\JavaTools\apache-maven-3.0
      37 | set PATH=%PATH%;%M2_HOME%\bin; 38 |
    6. 39 |
    40 |
  3. Do a Subversion checkout of the source tree from the location 41 | 42 | https://mybatis.googlecode.com/svn/sub-projects/generator/trunk/core/ 43 | (You may use any Subversion client you prefer. We recommend 44 | TortoiseSVN.) 45 |
  4. 46 |
  5. Open a command window in the directory where you checked out the source and 47 | run the command mvn clean install - or any of the other Maven lifecycle commands. 48 | You can use the mvn site command to generate the code coverage reports, but the 49 | MyBatis website is not built with Maven at this time. 50 |
  6. 51 |
52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/logging/LogFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.logging; 17 | 18 | import static org.mybatis.generator.internal.util.messages.Messages.getString; 19 | 20 | import org.mybatis.generator.internal.ObjectFactory; 21 | 22 | /** 23 | * Factory for creating loggers. Uses runtime introspection to determine the 24 | * AbstractLogFactory implementation. 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public class LogFactory { 30 | private static AbstractLogFactory logFactory; 31 | 32 | static { 33 | try { 34 | ObjectFactory.internalClassForName("org.apache.log4j.Logger"); //$NON-NLS-1$ 35 | logFactory = new Log4jLoggingLogFactory(); 36 | } catch (Exception e) { 37 | logFactory = new JdkLoggingLogFactory(); 38 | } 39 | } 40 | 41 | public static Log getLog(Class clazz) { 42 | try { 43 | return logFactory.getLog(clazz); 44 | } catch (Throwable t) { 45 | throw new RuntimeException(getString("RuntimeError.21", //$NON-NLS-1$ 46 | clazz.getName(), t.getMessage()), t); 47 | } 48 | } 49 | 50 | /** 51 | * This method will switch the logging implementation to Java native 52 | * logging. This is useful in situations where you want to use Java native 53 | * logging to log activity but Log4J is on the classpath. Note that this 54 | * method is only effective for log classes obtained after calling this 55 | * method. If you intend to use this method you should call it before 56 | * calling any other method. 57 | */ 58 | public static synchronized void forceJavaLogging() { 59 | logFactory = new JdkLoggingLogFactory(); 60 | } 61 | 62 | private static class JdkLoggingLogFactory implements AbstractLogFactory { 63 | public Log getLog(Class clazz) { 64 | return new JdkLoggingImpl(clazz); 65 | } 66 | } 67 | 68 | private static class Log4jLoggingLogFactory implements AbstractLogFactory { 69 | public Log getLog(Class clazz) { 70 | return new Log4jImpl(clazz); 71 | } 72 | } 73 | 74 | public static void setLogFactory(AbstractLogFactory logFactory) { 75 | LogFactory.logFactory = logFactory; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/UpdateByPrimaryKeyWithBLOBsMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class UpdateByPrimaryKeyWithBLOBsMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public UpdateByPrimaryKeyWithBLOBsMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | Set importedTypes = new TreeSet(); 41 | FullyQualifiedJavaType parameterType; 42 | 43 | if (introspectedTable.getRules().generateRecordWithBLOBsClass()) { 44 | parameterType = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType()); 45 | } else { 46 | parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); 47 | } 48 | 49 | importedTypes.add(parameterType); 50 | 51 | Method method = new Method(); 52 | method.setVisibility(JavaVisibility.PUBLIC); 53 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 54 | method.setName(introspectedTable.getUpdateByPrimaryKeyWithBLOBsStatementId()); 55 | method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ 56 | 57 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable, "根据主键更新记录"); 58 | 59 | addMapperAnnotations(interfaze, method); 60 | 61 | if (context.getPlugins().clientUpdateByPrimaryKeyWithBLOBsMethodGenerated(method, interfaze, introspectedTable)) { 62 | interfaze.addImportedTypes(importedTypes); 63 | interfaze.addMethod(method); 64 | } 65 | } 66 | 67 | public void addMapperAnnotations(Interface interfaze, Method method) { 68 | return; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/UpdateByPrimaryKeySelectiveMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class UpdateByPrimaryKeySelectiveMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | public UpdateByPrimaryKeySelectiveMethodGenerator() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void addInterfaceElements(Interface interfaze) { 40 | Set importedTypes = new TreeSet(); 41 | FullyQualifiedJavaType parameterType; 42 | 43 | if (introspectedTable.getRules().generateRecordWithBLOBsClass()) { 44 | parameterType = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType()); 45 | } else { 46 | parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); 47 | } 48 | 49 | importedTypes.add(parameterType); 50 | 51 | Method method = new Method(); 52 | method.setVisibility(JavaVisibility.PUBLIC); 53 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 54 | method.setName(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId()); 55 | method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ 56 | 57 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable,"根据主键更新属性不为空的记录"); 58 | 59 | addMapperAnnotations(interfaze, method); 60 | 61 | if (context.getPlugins().clientUpdateByPrimaryKeySelectiveMethodGenerated(method, interfaze, introspectedTable)) { 62 | interfaze.addImportedTypes(importedTypes); 63 | interfaze.addMethod(method); 64 | } 65 | } 66 | 67 | public void addMapperAnnotations(Interface interfaze, Method method) { 68 | return; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/resources/ConfigIbatis_bank.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 49 | 50 | 51 | 52 |
53 | 54 | 55 |
56 | 57 | 58 | 59 | 60 |
61 |
62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/internal/rules/ConditionalModelRules.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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 | package org.mybatis.generator.internal.rules; 18 | 19 | import org.mybatis.generator.api.IntrospectedTable; 20 | 21 | /** 22 | * This class encapsulates all the code generation rules for a table using the 23 | * conditional model. In this model we do not generate primary key or record 24 | * with BLOBs classes if the class would only hold one field. 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public class ConditionalModelRules extends BaseRules { 30 | 31 | /** 32 | * 33 | */ 34 | public ConditionalModelRules(IntrospectedTable introspectedTable) { 35 | super(introspectedTable); 36 | } 37 | 38 | /** 39 | * We generate a primary key if there is more than one primary key field. 40 | * 41 | * @return true if the primary key should be generated 42 | */ 43 | public boolean generatePrimaryKeyClass() { 44 | return introspectedTable.getPrimaryKeyColumns().size() > 1; 45 | } 46 | 47 | /** 48 | * Generate a base record if there are any base columns, or if there is only 49 | * one primary key coulmn (in which case we will not generate a primary key 50 | * class), or if there is only one BLOB column (in which case we will not 51 | * generate a record with BLOBs class). 52 | * 53 | * @return true if the class should be generated 54 | */ 55 | public boolean generateBaseRecordClass() { 56 | return introspectedTable.getBaseColumns().size() > 0 || introspectedTable.getPrimaryKeyColumns().size() == 1 57 | || (introspectedTable.getBLOBColumns().size() > 0 && !generateRecordWithBLOBsClass()); 58 | 59 | } 60 | 61 | /** 62 | * We generate a record with BLOBs class if there is more than one BLOB 63 | * column. Do not generate a BLOBs class if any other super class would only 64 | * contain one field 65 | * 66 | * @return true if the record with BLOBs class should be generated 67 | */ 68 | public boolean generateRecordWithBLOBsClass() { 69 | int otherColumnCount = introspectedTable.getPrimaryKeyColumns().size() 70 | + introspectedTable.getBaseColumns().size(); 71 | 72 | return otherColumnCount > 1 && introspectedTable.getBLOBColumns().size() > 1; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/ibatis2/dao/templates/IbatisDAOTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 The Apache Software Foundation 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.ibatis2.dao.templates; 17 | 18 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 19 | import org.mybatis.generator.api.dom.java.JavaVisibility; 20 | import org.mybatis.generator.api.dom.java.Method; 21 | import org.mybatis.generator.api.dom.java.Parameter; 22 | 23 | /** 24 | * @author Jeff Butler 25 | */ 26 | public class IbatisDAOTemplate extends AbstractDAOTemplate { 27 | 28 | private FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("com.ibatis.dao.client.DaoManager"); //$NON-NLS-1$ 29 | 30 | /** 31 | * 32 | */ 33 | public IbatisDAOTemplate() { 34 | super(); 35 | } 36 | 37 | @Override 38 | protected void configureConstructorTemplate() { 39 | Method method = new Method(); 40 | method.setConstructor(true); 41 | method.setVisibility(JavaVisibility.PUBLIC); 42 | method.addParameter(new Parameter(fqjt, "daoManager")); //$NON-NLS-1$ 43 | method.addBodyLine("super(daoManager);"); //$NON-NLS-1$ 44 | setConstructorTemplate(method); 45 | } 46 | 47 | @Override 48 | protected void configureDeleteMethodTemplate() { 49 | setDeleteMethodTemplate("delete(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 50 | } 51 | 52 | @Override 53 | protected void configureImplementationImports() { 54 | addImplementationImport(fqjt); 55 | } 56 | 57 | @Override 58 | protected void configureInsertMethodTemplate() { 59 | setInsertMethodTemplate("insert(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 60 | } 61 | 62 | @Override 63 | protected void configureQueryForListMethodTemplate() { 64 | setQueryForListMethodTemplate("queryForList(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 65 | } 66 | 67 | @Override 68 | protected void configureQueryForObjectMethodTemplate() { 69 | setQueryForObjectMethodTemplate("queryForObject(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 70 | } 71 | 72 | @Override 73 | protected void configureSuperClass() { 74 | setSuperClass(new FullyQualifiedJavaType("com.ibatis.dao.client.template.SqlMapDaoTemplate")); //$NON-NLS-1$ 75 | } 76 | 77 | @Override 78 | protected void configureUpdateMethodTemplate() { 79 | setUpdateMethodTemplate("update(\"{0}.{1}\", {2});"); //$NON-NLS-1$ 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/UpdateByExampleSelectiveMethodGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009 The Apache Software Foundation 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.mybatis3.javamapper.elements; 17 | 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 22 | import org.mybatis.generator.api.dom.java.Interface; 23 | import org.mybatis.generator.api.dom.java.JavaVisibility; 24 | import org.mybatis.generator.api.dom.java.Method; 25 | import org.mybatis.generator.api.dom.java.Parameter; 26 | 27 | /** 28 | * 29 | * @author Jeff Butler 30 | * 31 | */ 32 | public class UpdateByExampleSelectiveMethodGenerator extends AbstractJavaMapperMethodGenerator { 33 | 34 | @Override 35 | public void addInterfaceElements(Interface interfaze) { 36 | Set importedTypes = new TreeSet(); 37 | Method method = new Method(); 38 | method.setVisibility(JavaVisibility.PUBLIC); 39 | method.setReturnType(FullyQualifiedJavaType.getIntInstance()); 40 | method.setName(introspectedTable.getUpdateByExampleSelectiveStatementId()); 41 | 42 | FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); 43 | method.addParameter(new Parameter(parameterType, "record", "@Param(\"record\")")); //$NON-NLS-1$ //$NON-NLS-2$ 44 | importedTypes.add(parameterType); 45 | 46 | FullyQualifiedJavaType exampleType = new FullyQualifiedJavaType(introspectedTable.getExampleType()); 47 | method.addParameter(new Parameter(exampleType, "example", "@Param(\"example\")")); //$NON-NLS-1$ //$NON-NLS-2$ 48 | importedTypes.add(exampleType); 49 | 50 | importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param")); //$NON-NLS-1$ 51 | 52 | context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable,"根据条件更新属性不为空的记录"); 53 | 54 | addMapperAnnotations(interfaze, method); 55 | 56 | if (context.getPlugins().clientUpdateByExampleSelectiveMethodGenerated(method, interfaze, introspectedTable)) { 57 | interfaze.addImportedTypes(importedTypes); 58 | interfaze.addMethod(method); 59 | } 60 | } 61 | 62 | public void addMapperAnnotations(Interface interfaze, Method method) { 63 | return; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/resources/ConfigIbatis_wht.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | 41 | 42 | 43 | 44 | 45 | 47 |
48 | 49 | 50 | 52 | 54 |
55 |
56 |
57 | --------------------------------------------------------------------------------