├── src ├── test │ ├── resources │ │ ├── script │ │ │ ├── table_test_slice_mod.sql │ │ │ └── table_test_slice_month.sql │ │ ├── mybatis_config.xml │ │ ├── generatorConfig-mysql.xml │ │ └── mapping │ │ │ ├── TableTestSliceMonthMapper.xml │ │ │ └── TableTestSliceModMapper.xml │ └── java │ │ └── com │ │ └── github │ │ └── oceanc │ │ └── mybatis3 │ │ └── generator │ │ └── plugin │ │ ├── mapper │ │ ├── TableTestSliceMonthMapper.java │ │ └── TableTestSliceModMapper.java │ │ ├── model │ │ ├── TableTestSliceMonth.java │ │ ├── TableTestSliceMod.java │ │ ├── TableTestSliceMonthExample.java │ │ └── TableTestSliceModExample.java │ │ ├── Generator.java │ │ └── MapperTest.java └── main │ └── java │ └── com │ └── github │ └── oceanc │ └── mybatis3 │ └── generator │ └── plugin │ ├── LombokAnnotationPlugin.java │ ├── WhereSqlTextPlugin.java │ ├── JacksonToJsonPlugin.java │ ├── PaginationPlugin.java │ ├── UpdateSqlTextOfUpdateSelectivePlugin.java │ ├── PluginUtils.java │ ├── SumSelectivePlugin.java │ ├── BatchInsertPlugin.java │ ├── JacksonAnnotationPlugin.java │ ├── MinMaxPlugin.java │ ├── OptimisticLockAutoIncreasePlugin.java │ └── SliceTablePlugin.java ├── .gitignore ├── pom.xml └── README.md /src/test/resources/script/table_test_slice_mod.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE `TABLE_TEST_SLICE_MOD`DROP PRIMARY KEY; 2 | 3 | DROP TABLE `TABLE_TEST_SLICE_MOD`; 4 | 5 | CREATE TABLE `TABLE_TEST_SLICE_MOD` ( 6 | `ID` bigint(20) NOT NULL AUTO_INCREMENT, 7 | `SLICE_MOD_ID` bigint(32) NOT NULL DEFAULT 0, 8 | `JACKSON_ID1` bigint(32) NOT NULL DEFAULT 0, 9 | `JACKSON_ID2` varchar(255) NOT NULL, 10 | `JACKSON_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 11 | `COULD_SUM_COL` int(8) NOT NULL DEFAULT 0, 12 | `VERSION` bigint(32) NOT NULL DEFAULT 0, 13 | PRIMARY KEY (`ID`) 14 | ) 15 | ENGINE=InnoDB 16 | DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci 17 | AUTO_INCREMENT=1; 18 | 19 | -------------------------------------------------------------------------------- /src/test/resources/script/table_test_slice_month.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE `TABLE_TEST_SLICE_MONTH`DROP PRIMARY KEY; 2 | 3 | DROP TABLE `TABLE_TEST_SLICE_MONTH`; 4 | 5 | CREATE TABLE `TABLE_TEST_SLICE_MONTH` ( 6 | `ID` bigint(20) NOT NULL AUTO_INCREMENT, 7 | `SLICE_MONTH_ID` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 8 | `JACKSON_ID1` bigint(20) NOT NULL, 9 | `VERSION` bigint(32) NOT NULL DEFAULT 0, 10 | `JACKSON_ID2` varchar(255) NOT NULL, 11 | `JACKSON_TIME` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 12 | `COULD_SUM_COL` int(11) NOT NULL DEFAULT 0, 13 | PRIMARY KEY (`ID`) 14 | ) 15 | ENGINE=InnoDB 16 | DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci 17 | AUTO_INCREMENT=1; 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/LombokAnnotationPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 6 | import org.mybatis.generator.api.dom.java.Method; 7 | import org.mybatis.generator.api.dom.java.TopLevelClass; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * Created by chengyang 14 | */ 15 | public class LombokAnnotationPlugin extends PluginAdapter { 16 | 17 | @Override 18 | public boolean validate(List warnings) { 19 | return true; 20 | } 21 | 22 | @Override 23 | public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 24 | topLevelClass.addAnnotation("@Data"); 25 | topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.Data")); 26 | List methods = topLevelClass.getMethods(); 27 | List remove = new ArrayList(); 28 | for (Method method : methods) { 29 | if (method.getBodyLines().size() < 2) { 30 | remove.add(method); 31 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + "'s method=" + method.getName() + " removed cause lombok annotation."); 32 | } 33 | } 34 | methods.removeAll(remove); 35 | return true; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/mapper/TableTestSliceMonthMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin.mapper; 2 | 3 | import com.github.oceanc.mybatis3.generator.plugin.model.TableTestSliceMonth; 4 | import com.github.oceanc.mybatis3.generator.plugin.model.TableTestSliceMonthExample; 5 | import java.util.List; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.apache.ibatis.session.RowBounds; 8 | 9 | public interface TableTestSliceMonthMapper { 10 | int countByExample(TableTestSliceMonthExample example); 11 | 12 | int deleteByExample(TableTestSliceMonthExample example); 13 | 14 | int deleteByPrimaryKey(TableTestSliceMonth record); 15 | 16 | int insert(TableTestSliceMonth record); 17 | 18 | int insertSelective(TableTestSliceMonth record); 19 | 20 | List selectByExampleWithRowbounds(TableTestSliceMonthExample example, RowBounds rowBounds); 21 | 22 | List selectByExample(TableTestSliceMonthExample example); 23 | 24 | TableTestSliceMonth selectByPrimaryKey(TableTestSliceMonth record); 25 | 26 | int updateByExampleSelective(@Param("record") TableTestSliceMonth record, @Param("example") TableTestSliceMonthExample example); 27 | 28 | int updateByExample(@Param("record") TableTestSliceMonth record, @Param("example") TableTestSliceMonthExample example); 29 | 30 | int updateByPrimaryKeySelective(TableTestSliceMonth record); 31 | 32 | int updateByPrimaryKey(TableTestSliceMonth record); 33 | 34 | Long sumByExample(TableTestSliceMonthExample example); 35 | 36 | void batchInsert(@Param("items") List items); 37 | } -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/model/TableTestSliceMonth.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import java.io.IOException; 7 | import java.io.Serializable; 8 | import java.util.Calendar; 9 | import java.util.Date; 10 | import java.util.TimeZone; 11 | import lombok.Data; 12 | 13 | @Data 14 | public class TableTestSliceMonth implements Serializable { 15 | private Long id; 16 | 17 | private Date sliceMonthId; 18 | 19 | @JsonProperty("jacksonProperty1") 20 | private Long jacksonId1; 21 | 22 | private Long version; 23 | 24 | @JsonProperty("jacksonProperty2") 25 | private String jacksonId2; 26 | 27 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 28 | private Date jacksonTime; 29 | 30 | private Integer couldSumCol; 31 | 32 | private static final long serialVersionUID = 1L; 33 | 34 | private String tableNameSuffix; 35 | 36 | private String updateSql; 37 | 38 | public void setSliceMonthId(Date sliceMonthId) { 39 | this.sliceMonthId = sliceMonthId; 40 | if (this.sliceMonthId != null ) { 41 | Calendar calendar = Calendar.getInstance(); 42 | calendar.setTimeInMillis(sliceMonthId.getTime()); 43 | int m = calendar.get(Calendar.MONTH) + 1; 44 | this.tableNameSuffix = calendar.get(Calendar.YEAR) + (m < 10 ? "0" + m : "" + m); 45 | } 46 | } 47 | 48 | public String toJson() throws IOException { 49 | ObjectMapper mapper = new ObjectMapper(); 50 | mapper.setTimeZone(TimeZone.getDefault()); 51 | return mapper.writeValueAsString(this); 52 | } 53 | } -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/WhereSqlTextPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.java.*; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * Created by chengyang 11 | */ 12 | public class WhereSqlTextPlugin extends PluginAdapter { 13 | 14 | @Override 15 | public boolean validate(List warnings) { 16 | return true; 17 | } 18 | 19 | @Override 20 | public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 21 | for (InnerClass innerClass : topLevelClass.getInnerClasses()) { 22 | if (FullyQualifiedJavaType.getGeneratedCriteriaInstance().equals(innerClass.getType())) { 23 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 24 | Method method = new Method(); 25 | method.setName("addConditionSql"); 26 | method.setVisibility(JavaVisibility.PUBLIC); 27 | method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); 28 | method.addBodyLine("addCriterion(conditionSql);"); 29 | method.addBodyLine("return (Criteria) this;"); 30 | method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "conditionSql")); 31 | PluginUtils.addDoc(this.getContext(), method, tableName); 32 | innerClass.getMethods().add(method); 33 | 34 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add method=addConditionSql for custom sql statement in where clause."); 35 | } 36 | } 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/mapper/TableTestSliceModMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin.mapper; 2 | 3 | import com.github.oceanc.mybatis3.generator.plugin.model.TableTestSliceMod; 4 | import com.github.oceanc.mybatis3.generator.plugin.model.TableTestSliceModExample; 5 | import java.util.Date; 6 | import java.util.List; 7 | import org.apache.ibatis.annotations.Param; 8 | import org.apache.ibatis.session.RowBounds; 9 | 10 | public interface TableTestSliceModMapper { 11 | int countByExample(TableTestSliceModExample example); 12 | 13 | int deleteByExample(TableTestSliceModExample example); 14 | 15 | int deleteByPrimaryKey(TableTestSliceMod record); 16 | 17 | int insert(TableTestSliceMod record); 18 | 19 | int insertSelective(TableTestSliceMod record); 20 | 21 | List selectByExampleWithRowbounds(TableTestSliceModExample example, RowBounds rowBounds); 22 | 23 | List selectByExample(TableTestSliceModExample example); 24 | 25 | TableTestSliceMod selectByPrimaryKey(TableTestSliceMod record); 26 | 27 | int updateByExampleSelective(@Param("record") TableTestSliceMod record, @Param("example") TableTestSliceModExample example); 28 | 29 | int updateByExample(@Param("record") TableTestSliceMod record, @Param("example") TableTestSliceModExample example); 30 | 31 | int updateByPrimaryKeySelective(TableTestSliceMod record); 32 | 33 | int updateByPrimaryKey(TableTestSliceMod record); 34 | 35 | Long sumByExample(TableTestSliceModExample example); 36 | 37 | void batchInsert(@Param("items") List items); 38 | 39 | Long minSliceModIByExample(TableTestSliceModExample example); 40 | 41 | Date minJacksonTimByExample(TableTestSliceModExample example); 42 | 43 | Long maxSliceModIByExample(TableTestSliceModExample example); 44 | 45 | Date maxJacksonTimByExample(TableTestSliceModExample example); 46 | } -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/model/TableTestSliceMod.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import java.io.IOException; 7 | import java.io.Serializable; 8 | import java.math.BigDecimal; 9 | import java.util.Date; 10 | import java.util.TimeZone; 11 | import lombok.Data; 12 | 13 | @Data 14 | public class TableTestSliceMod implements Serializable { 15 | private Long id; 16 | 17 | private Long sliceModId; 18 | 19 | @JsonProperty("jacksonProperty1") 20 | private Long jacksonId1; 21 | 22 | @JsonProperty("jacksonProperty2") 23 | private String jacksonId2; 24 | 25 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 26 | private Date jacksonTime; 27 | 28 | private Integer couldSumCol; 29 | 30 | private Long version; 31 | 32 | private static final long serialVersionUID = 1L; 33 | 34 | private String tableNameSuffix; 35 | 36 | private String updateSql; 37 | 38 | public void setSliceModId(Long sliceModId) { 39 | this.sliceModId = sliceModId; 40 | if (this.sliceModId != null ) { 41 | long nan = 0; 42 | StringBuilder sb = new StringBuilder("0"); 43 | for (char c : String.valueOf(sliceModId).toCharArray()) { 44 | if (Character.isDigit(c)) sb.append(c); 45 | else nan += c; 46 | } 47 | long lid = new BigDecimal(sb.toString()).longValue(); 48 | if(nan > 0) lid += 83 + nan; 49 | this.tableNameSuffix = (Math.abs(lid) % 83) + ""; 50 | } 51 | } 52 | 53 | public String toJson() throws IOException { 54 | ObjectMapper mapper = new ObjectMapper(); 55 | mapper.setTimeZone(TimeZone.getDefault()); 56 | return mapper.writeValueAsString(this); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/JacksonToJsonPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 6 | import org.mybatis.generator.api.dom.java.JavaVisibility; 7 | import org.mybatis.generator.api.dom.java.Method; 8 | import org.mybatis.generator.api.dom.java.TopLevelClass; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created by chengyang 14 | */ 15 | public class JacksonToJsonPlugin extends PluginAdapter { 16 | 17 | @Override 18 | public boolean validate(List warnings) { 19 | return true; 20 | } 21 | 22 | @Override 23 | public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 24 | Method method = new Method(); 25 | method.setName("toJson"); 26 | method.setVisibility(JavaVisibility.PUBLIC); 27 | method.setReturnType(FullyQualifiedJavaType.getStringInstance()); 28 | method.addException(new FullyQualifiedJavaType("java.io.IOException")); 29 | method.addBodyLine("ObjectMapper mapper = new ObjectMapper();"); 30 | method.addBodyLine("mapper.setTimeZone(TimeZone.getDefault());"); 31 | method.addBodyLine("return mapper.writeValueAsString(this);"); 32 | PluginUtils.addDoc(this.getContext(), method, introspectedTable.getFullyQualifiedTableNameAtRuntime()); 33 | 34 | topLevelClass.addImportedType(new FullyQualifiedJavaType("java.io.IOException")); 35 | topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.databind.ObjectMapper")); 36 | topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.TimeZone")); 37 | 38 | topLevelClass.addMethod(method); 39 | 40 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add method=toJson implement by Jackson2."); 41 | return true; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/Generator.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.MyBatisGenerator; 4 | import org.mybatis.generator.api.ProgressCallback; 5 | import org.mybatis.generator.api.VerboseProgressCallback; 6 | import org.mybatis.generator.config.Configuration; 7 | import org.mybatis.generator.config.xml.ConfigurationParser; 8 | import org.mybatis.generator.internal.DefaultShellCallback; 9 | 10 | import java.io.File; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by chengyang 16 | */ 17 | public class Generator { 18 | 19 | public static void main(String[] args) { 20 | 21 | String root = System.getProperty("user.dir"); 22 | String projectPath = root + "/src/test/java"; 23 | String modelPackage = "com.github.oceanc.mybatis3.generator.plugin.model"; 24 | String mapperPackage = "com.github.oceanc.mybatis3.generator.plugin.mapper"; 25 | String xmlProjectPath = root + "/src/test/resources"; 26 | String xmlPackage = "mapping"; 27 | 28 | try { 29 | List warnings = new ArrayList(); 30 | File configFile = new File(xmlProjectPath + "/generatorConfig-mysql.xml"); 31 | ConfigurationParser cp = new ConfigurationParser(warnings); 32 | Configuration config = cp.parseConfiguration(configFile); 33 | 34 | config.getContexts().get(0).getJavaModelGeneratorConfiguration().setTargetProject(projectPath); 35 | config.getContexts().get(0).getJavaModelGeneratorConfiguration().setTargetPackage(modelPackage); 36 | 37 | config.getContexts().get(0).getJavaClientGeneratorConfiguration().setTargetProject(projectPath); 38 | config.getContexts().get(0).getJavaClientGeneratorConfiguration().setTargetPackage(mapperPackage); 39 | 40 | config.getContexts().get(0).getSqlMapGeneratorConfiguration().setTargetProject(xmlProjectPath); 41 | config.getContexts().get(0).getSqlMapGeneratorConfiguration().setTargetPackage(xmlPackage); 42 | 43 | DefaultShellCallback callback = new DefaultShellCallback(true); 44 | ProgressCallback progressCallback = new VerboseProgressCallback(); 45 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 46 | myBatisGenerator.generate(progressCallback); 47 | } catch (Throwable e) { 48 | e.printStackTrace(); 49 | } 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/resources/mybatis_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### OSX template 3 | .DS_Store 4 | .AppleDouble 5 | .LSOverride 6 | 7 | # Icon must end with two \r 8 | Icon 9 | 10 | # Thumbnails 11 | ._* 12 | 13 | # Files that might appear in the root of a volume 14 | .DocumentRevisions-V100 15 | .fseventsd 16 | .Spotlight-V100 17 | .TemporaryItems 18 | .Trashes 19 | .VolumeIcon.icns 20 | 21 | # Directories potentially created on remote AFP share 22 | .AppleDB 23 | .AppleDesktop 24 | Network Trash Folder 25 | Temporary Items 26 | .apdisk 27 | 28 | 29 | ### Maven template 30 | target/ 31 | pom.xml.tag 32 | pom.xml.releaseBackup 33 | pom.xml.versionsBackup 34 | pom.xml.next 35 | release.properties 36 | dependency-reduced-pom.xml 37 | 38 | 39 | ### Java template 40 | *.class 41 | 42 | # Mobile Tools for Java (J2ME) 43 | .mtj.tmp/ 44 | 45 | # Package Files # 46 | *.jar 47 | *.war 48 | *.ear 49 | 50 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 51 | hs_err_pid* 52 | 53 | 54 | ### Eclipse template 55 | *.pydevproject 56 | .metadata 57 | .gradle 58 | bin/ 59 | tmp/ 60 | *.tmp 61 | *.bak 62 | *.swp 63 | *~.nib 64 | local.properties 65 | .settings/ 66 | .loadpath 67 | 68 | # Eclipse Core 69 | .project 70 | 71 | # External tool builders 72 | .externalToolBuilders/ 73 | 74 | # Locally stored "Eclipse launch configurations" 75 | *.launch 76 | 77 | # CDT-specific 78 | .cproject 79 | 80 | # JDT-specific (Eclipse Java Development Tools) 81 | .classpath 82 | 83 | # PDT-specific 84 | .buildpath 85 | 86 | # sbteclipse plugin 87 | .target 88 | 89 | # TeXlipse plugin 90 | .texlipse 91 | 92 | 93 | ### JetBrains template 94 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 95 | 96 | *.iml 97 | 98 | ## Directory-based project format: 99 | .idea/ 100 | # if you remove the above rule, at least ignore the following: 101 | 102 | # User-specific stuff: 103 | # .idea/workspace.xml 104 | # .idea/tasks.xml 105 | # .idea/dictionaries 106 | 107 | # Sensitive or high-churn files: 108 | # .idea/dataSources.ids 109 | # .idea/dataSources.xml 110 | # .idea/sqlDataSources.xml 111 | # .idea/dynamic.xml 112 | # .idea/uiDesigner.xml 113 | 114 | # Gradle: 115 | # .idea/gradle.xml 116 | # .idea/libraries 117 | 118 | # Mongo Explorer plugin: 119 | # .idea/mongoSettings.xml 120 | 121 | ## File-based project format: 122 | *.ipr 123 | *.iws 124 | 125 | ## Plugin-specific files: 126 | 127 | # IntelliJ 128 | out/ 129 | 130 | # mpeltonen/sbt-idea plugin 131 | .idea_modules/ 132 | 133 | # JIRA plugin 134 | atlassian-ide-plugin.xml 135 | 136 | # Crashlytics plugin (for Android Studio and IntelliJ) 137 | com_crashlytics_export_strings.xml 138 | crashlytics.properties 139 | crashlytics-build.properties 140 | 141 | 142 | -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/MapperTest.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import com.github.oceanc.mybatis3.generator.plugin.mapper.TableTestSliceModMapper; 4 | import com.github.oceanc.mybatis3.generator.plugin.model.TableTestSliceMod; 5 | import com.github.oceanc.mybatis3.generator.plugin.model.TableTestSliceModExample; 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | 10 | import java.io.InputStream; 11 | import java.text.SimpleDateFormat; 12 | import java.util.ArrayList; 13 | import java.util.Date; 14 | import java.util.List; 15 | 16 | /** 17 | * Created by chengyang 18 | */ 19 | public class MapperTest { 20 | public static void main(String[] args) { 21 | String resource = "/mybatis_config.xml"; 22 | 23 | //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) 24 | InputStream is = MapperTest.class.getResourceAsStream(resource); 25 | //构建sqlSession的工厂 26 | SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); 27 | 28 | //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) 29 | //Reader reader = Resources.getResourceAsReader(resource); 30 | //构建sqlSession的工厂 31 | //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 32 | 33 | //创建能执行映射文件中sql的sqlSession 34 | SqlSession session = sessionFactory.openSession(); 35 | 36 | TableTestSliceModMapper mapper = session.getMapper(TableTestSliceModMapper.class); 37 | 38 | TableTestSliceMod mod = new TableTestSliceMod(); 39 | mod.setSliceModId(83L); 40 | mod.setCouldSumCol(1); 41 | mod.setJacksonId1(11L); 42 | mod.setJacksonId2("jks2"); 43 | mod.setJacksonTime(new Date()); 44 | 45 | TableTestSliceMod mod2 = new TableTestSliceMod(); 46 | mod2.setSliceModId(166L); 47 | mod2.setCouldSumCol(2); 48 | mod2.setJacksonId1(22L); 49 | mod2.setJacksonId2("jks2"); 50 | mod2.setJacksonTime(new Date()); 51 | 52 | List list = new ArrayList(1); 53 | list.add(mod); 54 | list.add(mod2); 55 | 56 | mapper.batchInsert(list); 57 | session.commit(); 58 | 59 | TableTestSliceModExample example = new TableTestSliceModExample(); 60 | example.page(0, 2); 61 | example.setTableNameSuffix("0"); 62 | List ls = mapper.selectByExample(example); 63 | System.out.println(ls.get(0).getSliceModId() == 83); 64 | System.out.println(ls.get(1).getSliceModId() == 166); 65 | 66 | Date date = mapper.minJacksonTimByExample(example); 67 | System.out.println(SimpleDateFormat.getDateInstance().format(date)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/PaginationPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.java.*; 6 | import org.mybatis.generator.api.dom.xml.Element; 7 | import org.mybatis.generator.api.dom.xml.TextElement; 8 | import org.mybatis.generator.api.dom.xml.XmlElement; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created by chengyang 14 | */ 15 | public class PaginationPlugin extends PluginAdapter { 16 | @Override 17 | public boolean validate(List warnings) { 18 | return true; 19 | } 20 | 21 | @Override 22 | public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 23 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 24 | PluginUtils.addProperty(OFFSET, INTEGER_TYPE, topLevelClass, this.getContext(), tableName); 25 | PluginUtils.addProperty(LIMIT, INTEGER_TYPE, topLevelClass, this.getContext(), tableName); 26 | 27 | Method method = new Method(); 28 | method.setName("page"); 29 | method.setVisibility(JavaVisibility.PUBLIC); 30 | method.setReturnType(topLevelClass.getType()); 31 | method.addBodyLine("this.offset = offset;"); 32 | method.addBodyLine("this.limit = limit;"); 33 | method.addBodyLine("return this;"); 34 | method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "offset")); 35 | method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "limit")); 36 | PluginUtils.addDoc(this.getContext(), method, tableName); 37 | topLevelClass.getMethods().add(method); 38 | 39 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add method=page for pagination."); 40 | return true; 41 | } 42 | 43 | @Override 44 | public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 45 | List els = element.getElements(); 46 | els.add(new TextElement(template)); 47 | return true; 48 | } 49 | 50 | @Override 51 | public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 52 | List els = element.getElements(); 53 | els.add(new TextElement(template)); 54 | return true; 55 | } 56 | 57 | private final static FullyQualifiedJavaType INTEGER_TYPE = new FullyQualifiedJavaType("java.lang.Integer"); 58 | private static final String OFFSET = "offset"; 59 | private static final String LIMIT = "limit"; 60 | private static final String template = 61 | "\n" + 62 | " limit ${offset}, ${limit}\n" + 63 | " "; 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/UpdateSqlTextOfUpdateSelectivePlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.java.*; 6 | import org.mybatis.generator.api.dom.xml.TextElement; 7 | import org.mybatis.generator.api.dom.xml.XmlElement; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * Created by chengyang 13 | */ 14 | public class UpdateSqlTextOfUpdateSelectivePlugin extends PluginAdapter { 15 | 16 | @Override 17 | public boolean validate(List warnings) { 18 | return true; 19 | } 20 | 21 | @Override 22 | public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 23 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 24 | Field field = new Field(); 25 | field.setName(UPDATE_SQL); 26 | field.setVisibility(JavaVisibility.PRIVATE); 27 | field.setType(FullyQualifiedJavaType.getStringInstance()); 28 | PluginUtils.addDoc(this.getContext(), field, tableName); 29 | 30 | Method getter = new Method(); 31 | getter.setName("get" + UPDATE_SQL.substring(0, 1).toUpperCase() + UPDATE_SQL.substring(1, UPDATE_SQL.length())); 32 | getter.setVisibility(JavaVisibility.PUBLIC); 33 | getter.setReturnType(FullyQualifiedJavaType.getStringInstance()); 34 | getter.addBodyLine("return this." + UPDATE_SQL + ";"); 35 | PluginUtils.addDoc(this.getContext(), getter, tableName); 36 | 37 | Method setter = new Method(); 38 | setter.setName("set" + UPDATE_SQL.substring(0, 1).toUpperCase() + UPDATE_SQL.substring(1, UPDATE_SQL.length())); 39 | setter.setVisibility(JavaVisibility.PUBLIC); 40 | setter.setReturnType(null); 41 | setter.addBodyLine("this." + UPDATE_SQL + " = " + UPDATE_SQL + ";"); 42 | setter.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), UPDATE_SQL)); 43 | PluginUtils.addDoc(this.getContext(), setter, tableName); 44 | 45 | topLevelClass.addField(field); 46 | topLevelClass.addMethod(getter); 47 | topLevelClass.addMethod(setter); 48 | 49 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add field=" + UPDATE_SQL + " and getter & setter related that is only effective in update selective model."); 50 | return true; 51 | } 52 | 53 | @Override 54 | public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 55 | String node = "\n" + 56 | " ${record.updateSql},\n" + 57 | " "; 58 | TextElement ele = new TextElement(node); 59 | XmlElement set = (XmlElement) element.getElements().get(1); 60 | set.getElements().add(ele); 61 | return true; 62 | } 63 | 64 | private final static String UPDATE_SQL = "updateSql"; 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/PluginUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.dom.java.*; 4 | import org.mybatis.generator.config.Context; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | import java.util.Locale; 9 | 10 | /** 11 | * Created by chengyang 12 | */ 13 | public abstract class PluginUtils { 14 | 15 | public static void addProperty(String field, FullyQualifiedJavaType fieldType, TopLevelClass topLevelClass, Context context, String tableName) { 16 | for (Method method : topLevelClass.getMethods()) { 17 | if (method.getName().equals("clear")) { 18 | method.addBodyLine("this." + field + " = null;"); 19 | } 20 | } 21 | topLevelClass.addField(makeStringField(context, field, fieldType, tableName)); 22 | topLevelClass.addMethod(makeGetterStringMethod(context, field, fieldType, tableName)); 23 | topLevelClass.addMethod(makeSetterStringMethod(context, field, fieldType, tableName)); 24 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add field " + field + " and getter related."); 25 | } 26 | 27 | public static Field makeStringField(Context context, String fieldName, FullyQualifiedJavaType fieldType, String tableName) { 28 | Field field = new Field(); 29 | field.setName(fieldName); 30 | field.setVisibility(JavaVisibility.PRIVATE); 31 | field.setType(fieldType); 32 | addDoc(context, field, tableName); 33 | return field; 34 | } 35 | 36 | public static Method makeGetterStringMethod(Context context, String fieldName, FullyQualifiedJavaType fieldType, String tableName) { 37 | String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); 38 | Method method = new Method(); 39 | method.setName(methodName); 40 | method.setVisibility(JavaVisibility.PUBLIC); 41 | method.setReturnType(fieldType); 42 | method.addBodyLine("return this." + fieldName + ";"); 43 | addDoc(context, method, tableName); 44 | return method; 45 | } 46 | 47 | public static Method makeSetterStringMethod(Context context, String fieldName, FullyQualifiedJavaType fieldType, String tableName) { 48 | String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); 49 | Method method = new Method(); 50 | method.setName(methodName); 51 | method.setVisibility(JavaVisibility.PUBLIC); 52 | method.addParameter(new Parameter(fieldType, fieldName)); 53 | method.addBodyLine("this." + fieldName + " = " + fieldName + ";"); 54 | addDoc(context, method, tableName); 55 | return method; 56 | } 57 | 58 | public static void addDoc(Context context, JavaElement element, String tableName) { 59 | String suppressAllComments = context.getCommentGeneratorConfiguration().getProperty("suppressAllComments"); 60 | if (!"true".equals(suppressAllComments)) { 61 | String type = element.getClass() == Field.class ? "field" : "method"; 62 | element.addJavaDocLine("/**"); 63 | element.addJavaDocLine("* This " + type + " was generated by MyBatis Generator."); 64 | element.addJavaDocLine("* This " + type + " corresponds to the database table " + tableName); 65 | element.addJavaDocLine("*"); 66 | element.addJavaDocLine("* @mbggenerated " + df.format(new Date())); 67 | element.addJavaDocLine("*/"); 68 | } 69 | } 70 | 71 | private static SimpleDateFormat df = new SimpleDateFormat("EEE MMM ww HH:mm:ss z yyyy", Locale.US); 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/SumSelectivePlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedColumn; 4 | import org.mybatis.generator.api.IntrospectedTable; 5 | import org.mybatis.generator.api.PluginAdapter; 6 | import org.mybatis.generator.api.dom.java.*; 7 | import org.mybatis.generator.api.dom.xml.Document; 8 | import org.mybatis.generator.api.dom.xml.TextElement; 9 | 10 | import java.text.MessageFormat; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by chengyang 15 | */ 16 | public class SumSelectivePlugin extends PluginAdapter { 17 | @Override 18 | public boolean validate(List warnings) { 19 | return true; 20 | } 21 | 22 | @Override 23 | public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 24 | FullyQualifiedJavaType paramType = new FullyQualifiedJavaType(introspectedTable.getExampleType()); 25 | Method method = new Method(); 26 | method.setName("sumByExample"); 27 | method.setVisibility(JavaVisibility.PUBLIC); 28 | method.addParameter(new Parameter(paramType, "example")); 29 | method.setReturnType(LONG_TYPE); 30 | interfaze.addMethod(method); 31 | System.out.println("-----------------" + interfaze.getType().getShortName() + " add method sumByExample."); 32 | return true; 33 | } 34 | 35 | @Override 36 | public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 37 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 38 | PluginUtils.addProperty(SUM_COL_FIELD, STRING_TYPE, topLevelClass, this.getContext(), tableName); 39 | 40 | List columns = introspectedTable.getAllColumns(); 41 | for (IntrospectedColumn column : columns) { 42 | String field = column.getJavaProperty(); 43 | String mn = "sum" + field.substring(0, 1).toUpperCase() + field.substring(1, field.length()); 44 | Method method = new Method(); 45 | method.setName(mn); 46 | method.setVisibility(JavaVisibility.PUBLIC); 47 | method.setReturnType(topLevelClass.getType()); 48 | method.addBodyLine("this." + SUM_COL_FIELD + "=\"" + column.getActualColumnName() + "\";"); 49 | method.addBodyLine("return this;"); 50 | PluginUtils.addDoc(this.getContext(), method, tableName); 51 | topLevelClass.addMethod(method); 52 | } 53 | 54 | return true; 55 | } 56 | 57 | @Override 58 | public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { 59 | String paramType = introspectedTable.getExampleType(); 60 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 61 | if(!tableName.contains("_${tableNameSuffix}") && SliceTablePlugin.needPartition(introspectedTable)) { 62 | tableName = tableName + "_${tableNameSuffix}"; 63 | } 64 | String xml = MessageFormat.format(template, paramType, tableName); 65 | 66 | document.getRootElement().getElements().add(new TextElement(xml)); 67 | 68 | return true; 69 | } 70 | 71 | private final static String SUM_COL_FIELD = "sumCol"; 72 | private final static FullyQualifiedJavaType LONG_TYPE = new FullyQualifiedJavaType("java.lang.Long"); 73 | private final static FullyQualifiedJavaType STRING_TYPE = FullyQualifiedJavaType.getStringInstance(); 74 | 75 | private static final String template = "" + 76 | ""; 82 | } 83 | -------------------------------------------------------------------------------- /src/test/resources/generatorConfig-mysql.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 |
58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 |
67 |
68 |
-------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/BatchInsertPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.java.*; 6 | import org.mybatis.generator.api.dom.xml.*; 7 | 8 | import java.text.MessageFormat; 9 | import java.util.List; 10 | 11 | /** 12 | * Created by chengyang 13 | */ 14 | public class BatchInsertPlugin extends PluginAdapter { 15 | 16 | @Override 17 | public boolean validate(List warnings) { 18 | return true; 19 | } 20 | 21 | @Override 22 | public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 23 | FullyQualifiedJavaType paramType = FullyQualifiedJavaType.getNewListInstance(); 24 | paramType.addTypeArgument(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType())); 25 | Method method = new Method(); 26 | method.setName(methodName); 27 | method.setVisibility(JavaVisibility.PUBLIC); 28 | method.addParameter(new Parameter(paramType, "items", "@Param(\"items\")")); 29 | interfaze.addMethod(method); 30 | interfaze.addImportedType(PARAM_ANOTS); 31 | System.out.println("-----------------" + interfaze.getType().getShortName() + " add method " + methodName + "."); 32 | return true; 33 | } 34 | 35 | @Override 36 | public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { 37 | String bind = ""; 38 | if (SliceTablePlugin.needPartition(introspectedTable)) { 39 | bind = "\n"; 40 | } 41 | 42 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 43 | if(!tableName.contains("_${tableNameSuffix}") && SliceTablePlugin.needPartition(introspectedTable)) { 44 | tableName = tableName + "_${tableNameSuffix}"; 45 | } 46 | 47 | XmlElement node = findInsertNode(document); 48 | if (node != null) { 49 | StringBuilder values = new StringBuilder(); 50 | for (Element el : node.getElements()) { 51 | if (el.getClass() == TextElement.class) { 52 | TextElement tl = (TextElement) el; 53 | values.append(" ").append(tl.getContent()).append("\n"); 54 | } 55 | } 56 | 57 | String[] insertStr = values.toString().split("values \\("); 58 | String fs = insertStr[0]; 59 | fs = " (" + fs.split(" \\(")[1].trim(); 60 | 61 | String xx = " (" + insertStr[1]; 62 | xx = xx.replaceAll("#\\{", "#\\{item."); 63 | 64 | String xml = MessageFormat.format(template, bind, tableName, fs, xx); 65 | document.getRootElement().getElements().add(new TextElement(xml)); 66 | return true; 67 | } 68 | return false; 69 | } 70 | 71 | private XmlElement findInsertNode(Document document) { 72 | for (Element el : document.getRootElement().getElements()) { 73 | if (el.getClass() == XmlElement.class) { 74 | XmlElement xl = (XmlElement) el; 75 | if (xl.getName().equals("insert")) { 76 | for (Attribute attr : xl.getAttributes()) { 77 | if (attr.getName().equals("id") && attr.getValue().equals("insert")) { 78 | return xl; 79 | } 80 | } 81 | } 82 | } 83 | } 84 | return null; 85 | } 86 | 87 | private final static FullyQualifiedJavaType PARAM_ANOTS = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"); 88 | private final static String methodName = "batchInsert"; 89 | private final static String template = 90 | "\n" + 91 | " \n" + 92 | " {0}" + 93 | " insert into {1} {2}\n" + 94 | " values\n" + 95 | " \n{3}" + 96 | " \n" + 97 | " \n" + 98 | " "; 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/JacksonAnnotationPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedColumn; 4 | import org.mybatis.generator.api.IntrospectedTable; 5 | import org.mybatis.generator.api.PluginAdapter; 6 | import org.mybatis.generator.api.dom.java.Field; 7 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; 8 | import org.mybatis.generator.api.dom.java.TopLevelClass; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created by chengyang 14 | */ 15 | public class JacksonAnnotationPlugin extends PluginAdapter { 16 | 17 | @Override 18 | public boolean validate(List warnings) { 19 | return true; 20 | } 21 | 22 | @Override 23 | public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { 24 | String prop = annotateProperty(introspectedColumn, introspectedTable); 25 | if (prop != null) { 26 | String ann = "@JsonProperty(\"" + prop + "\")"; 27 | field.addAnnotation(ann); 28 | topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.annotation.JsonProperty")); 29 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add field annotation " + ann + " to " + field.getName() + " implement by Jackson2."); 30 | } 31 | String format = annotateFormat(introspectedColumn, introspectedTable); 32 | if (format != null) { 33 | String ann ="@JsonFormat(pattern = \"" + format + "\")"; 34 | field.addAnnotation(ann); 35 | topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.annotation.JsonFormat")); 36 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add field annotation " + ann + " to " + field.getName() + " implement by Jackson2."); 37 | } 38 | annotateIgnore(field, topLevelClass, introspectedColumn, introspectedTable); 39 | return true; 40 | } 41 | 42 | private String annotateProperty(IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable) { 43 | String cls = introspectedTable.getTableConfigurationProperty(JACKSON_COLUMNS); 44 | if (cls != null && !"".equals(cls)) { 45 | String[] columns = cls.split(DELIMITER); 46 | for (int i = 0; i < columns.length; i++) { 47 | if (columns[i].trim().equals(introspectedColumn.getActualColumnName())) { 48 | String pps = introspectedTable.getTableConfigurationProperty(JACKSON_PROPERTIES); 49 | return pps.split(DELIMITER)[i].trim(); 50 | } 51 | } 52 | } 53 | return null; 54 | } 55 | 56 | private String annotateFormat(IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable) { 57 | String fmts = introspectedTable.getTableConfigurationProperty(JACKSON_FORMATS); 58 | if (fmts != null && !"".equals(fmts)) { 59 | String[] formats = fmts.split(DELIMITER_FORMAT); 60 | String anno = formats[1]; 61 | String[] columns = formats[0].split(DELIMITER); 62 | for (String column : columns) { 63 | if (column.equals(introspectedColumn.getActualColumnName())) { 64 | return anno; 65 | } 66 | } 67 | } 68 | return null; 69 | } 70 | 71 | private void annotateIgnore(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable) { 72 | String cls = introspectedTable.getTableConfigurationProperty(JACKSON_IGNORES); 73 | if (cls != null && !"".equals(cls)) { 74 | String[] columns = cls.split(DELIMITER); 75 | for (String column : columns) { 76 | if (column.trim().equals(introspectedColumn.getActualColumnName())) { 77 | field.addAnnotation("@JsonIgnore"); 78 | topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.annotation.JsonIgnore")); 79 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add field annotation @JsonIgnore to " + field.getName() + " implement by Jackson2."); 80 | } 81 | } 82 | } 83 | } 84 | 85 | private static final String DELIMITER = ","; 86 | private static final String DELIMITER_FORMAT = "@"; 87 | 88 | private static final String JACKSON_COLUMNS = "jacksonColumns"; 89 | private static final String JACKSON_PROPERTIES = "jacksonProperties"; 90 | private static final String JACKSON_FORMATS = "jacksonFormats"; 91 | private static final String JACKSON_IGNORES = "jacksonIgnores"; 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/MinMaxPlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedColumn; 4 | import org.mybatis.generator.api.IntrospectedTable; 5 | import org.mybatis.generator.api.PluginAdapter; 6 | import org.mybatis.generator.api.dom.java.*; 7 | import org.mybatis.generator.api.dom.xml.Document; 8 | import org.mybatis.generator.api.dom.xml.Element; 9 | import org.mybatis.generator.api.dom.xml.TextElement; 10 | 11 | import java.text.MessageFormat; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by chengyang 16 | */ 17 | public class MinMaxPlugin extends PluginAdapter { 18 | @Override 19 | public boolean validate(List warnings) { 20 | return true; 21 | } 22 | 23 | @Override 24 | public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 25 | String minColumns = introspectedTable.getTableConfigurationProperty(MIN_COLUMNS); 26 | String maxColumns = introspectedTable.getTableConfigurationProperty(MAX_COLUMNS); 27 | 28 | if (minColumns != null && !"".equals(minColumns)) { 29 | for (String column : minColumns.split(DELIMITER)) { 30 | for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { 31 | if (column.trim().equals(introspectedColumn.getActualColumnName())) { 32 | addMinMethod(interfaze, introspectedTable, introspectedColumn); 33 | } 34 | } 35 | } 36 | } 37 | 38 | if (maxColumns != null && !"".equals(maxColumns)) { 39 | for (String column : maxColumns.split(DELIMITER)) { 40 | for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { 41 | if (column.trim().equals(introspectedColumn.getActualColumnName())) { 42 | addMaxMethod(interfaze, introspectedTable, introspectedColumn); 43 | } 44 | } 45 | } 46 | } 47 | return true; 48 | } 49 | 50 | @Override 51 | public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { 52 | List els = document.getRootElement().getElements(); 53 | 54 | String paramType = introspectedTable.getExampleType(); 55 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 56 | if(!tableName.contains("_${tableNameSuffix}") && SliceTablePlugin.needPartition(introspectedTable)) { 57 | tableName = tableName + "_${tableNameSuffix}"; 58 | } 59 | 60 | String minColumns = introspectedTable.getTableConfigurationProperty(MIN_COLUMNS); 61 | String maxColumns = introspectedTable.getTableConfigurationProperty(MAX_COLUMNS); 62 | 63 | if (minColumns != null && !"".equals(minColumns)) { 64 | for (String column : minColumns.split(DELIMITER)) { 65 | for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { 66 | if (column.trim().equals(introspectedColumn.getActualColumnName())) { 67 | String field = makeFieldName(introspectedColumn); 68 | String returnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName(); 69 | String xml = MessageFormat.format(templateMin, field, paramType, returnType, column, tableName); 70 | els.add(new TextElement(xml)); 71 | } 72 | } 73 | } 74 | } 75 | 76 | if (maxColumns != null && !"".equals(maxColumns)) { 77 | for (String column : maxColumns.split(DELIMITER)) { 78 | for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { 79 | if (column.trim().equals(introspectedColumn.getActualColumnName())) { 80 | String field = makeFieldName(introspectedColumn); 81 | String returnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName(); 82 | String xml = MessageFormat.format(templateMax, field, paramType, returnType, column, tableName); 83 | els.add(new TextElement(xml)); 84 | } 85 | } 86 | } 87 | } 88 | 89 | return true; 90 | } 91 | 92 | private void addMinMethod(Interface interfaze, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { 93 | String mName = "min" + makeFieldName(introspectedColumn) + "ByExample"; 94 | FullyQualifiedJavaType paramType = new FullyQualifiedJavaType(introspectedTable.getExampleType()); 95 | FullyQualifiedJavaType returnType = introspectedColumn.getFullyQualifiedJavaType(); 96 | Method method = new Method(); 97 | method.setName(mName); 98 | method.setVisibility(JavaVisibility.PUBLIC); 99 | method.addParameter(new Parameter(paramType, "example")); 100 | method.setReturnType(returnType); 101 | interfaze.addMethod(method); 102 | String importType = returnType.getFullyQualifiedName(); 103 | if (!importType.startsWith("java.lang")) { 104 | interfaze.addImportedType(returnType); 105 | } 106 | System.out.println("-----------------" + interfaze.getType().getShortName() + " add method " + mName + "."); 107 | } 108 | 109 | private void addMaxMethod(Interface interfaze, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { 110 | String mName = "max" + makeFieldName(introspectedColumn) + "ByExample"; 111 | FullyQualifiedJavaType paramType = new FullyQualifiedJavaType(introspectedTable.getExampleType()); 112 | FullyQualifiedJavaType returnType = introspectedColumn.getFullyQualifiedJavaType(); 113 | Method method = new Method(); 114 | method.setName(mName); 115 | method.setVisibility(JavaVisibility.PUBLIC); 116 | method.addParameter(new Parameter(paramType, "example")); 117 | method.setReturnType(returnType); 118 | interfaze.addMethod(method); 119 | String importType = returnType.getFullyQualifiedName(); 120 | if (!importType.startsWith("java.lang")) { 121 | interfaze.addImportedType(returnType); 122 | } 123 | System.out.println("-----------------" + interfaze.getType().getShortName() + " add method " + mName + "."); 124 | } 125 | 126 | private String makeFieldName(IntrospectedColumn introspectedColumn) { 127 | String field = introspectedColumn.getJavaProperty(); 128 | field = field.substring(0, 1).toUpperCase() + field.substring(1, field.length() - 1); 129 | return field; 130 | } 131 | 132 | private static final String DELIMITER = ","; 133 | private final static String MIN_COLUMNS = "minColumns"; 134 | private final static String MAX_COLUMNS = "maxColumns"; 135 | 136 | private static final String templateMin = "" + 137 | ""; 143 | private static final String templateMax = "" + 144 | ""; 150 | } 151 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.oceanc 8 | mybatis3-generator-plugin 9 | 0.4.0 10 | jar 11 | MyBatis3 Generator Plugins 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | A custom plugins based on the project http://mybatis.github.io/generator/ 20 | https://github.com/oceanc/mybatis3-generator-plugins 21 | 22 | 23 | 24 | The Apache Software License, Version 2.0 25 | http://www.apache.org/licenses/LICENSE-2.0.txt 26 | 27 | 28 | 29 | 30 | 31 | chengyang 32 | oceanc2006@gmail.com 33 | 34 | 35 | 36 | 37 | https://github.com/oceanc/mybatis3-generator-plugins 38 | scm:git:git@github.com:oceanc/mybatis3-generator-plugins.git 39 | https://github.com/oceanc/mybatis3-generator-plugins.git 40 | 41 | 42 | 43 | UTF-8 44 | utf-8 45 | 2.5.1 46 | 47 | 48 | 49 | 50 | org.mybatis.generator 51 | mybatis-generator-core 52 | 1.3.2 53 | provided 54 | 55 | 56 | org.mybatis 57 | mybatis 58 | 3.2.8 59 | test 60 | 61 | 62 | com.fasterxml.jackson.core 63 | jackson-databind 64 | ${jackson.version} 65 | test 66 | 67 | 68 | com.fasterxml.jackson.core 69 | jackson-core 70 | ${jackson.version} 71 | test 72 | 73 | 74 | com.fasterxml.jackson.core 75 | jackson-annotations 76 | ${jackson.version} 77 | test 78 | 79 | 80 | org.projectlombok 81 | lombok 82 | 1.16.0 83 | test 84 | 85 | 86 | 87 | 88 | 89 | 90 | org.apache.maven.plugins 91 | maven-compiler-plugin 92 | 3.3 93 | 94 | 1.6 95 | 1.6 96 | utf-8 97 | 98 | 99 | 100 | org.apache.maven.plugins 101 | maven-surefire-plugin 102 | 2.14.1 103 | 104 | true 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | release 124 | 125 | 126 | ossrh 127 | https://oss.sonatype.org/content/repositories/snapshots 128 | 129 | 130 | ossrh 131 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 132 | 133 | 134 | 135 | 136 | 137 | org.sonatype.plugins 138 | nexus-staging-maven-plugin 139 | 1.6.5 140 | true 141 | 142 | ossrh 143 | https://oss.sonatype.org/ 144 | true 145 | 146 | 147 | 148 | org.apache.maven.plugins 149 | maven-source-plugin 150 | 2.4 151 | 152 | 153 | package 154 | 155 | jar-no-fork 156 | 157 | 158 | 159 | 160 | 161 | org.apache.maven.plugins 162 | maven-javadoc-plugin 163 | 2.10.2 164 | 165 | 166 | package 167 | 168 | jar 169 | 170 | 171 | 172 | 173 | 174 | org.apache.maven.plugins 175 | maven-gpg-plugin 176 | 1.6 177 | 178 | 179 | sign-artifacts 180 | verify 181 | 182 | sign 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/OptimisticLockAutoIncreasePlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedTable; 4 | import org.mybatis.generator.api.PluginAdapter; 5 | import org.mybatis.generator.api.dom.xml.Attribute; 6 | import org.mybatis.generator.api.dom.xml.Element; 7 | import org.mybatis.generator.api.dom.xml.TextElement; 8 | import org.mybatis.generator.api.dom.xml.XmlElement; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created by chengyang 14 | */ 15 | public class OptimisticLockAutoIncreasePlugin extends PluginAdapter { 16 | 17 | @Override 18 | public boolean validate(List warnings) { 19 | return true; 20 | } 21 | 22 | @Override 23 | public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 24 | String column = introspectedTable.getTableConfigurationProperty(OPTIMISTIC_LOCK_COLUMN); 25 | if (column != null && !"".equals(column)) { 26 | int index = -1; 27 | XmlElement sets = null; 28 | XmlElement setVs = null; 29 | for (Element el : element.getElements()) { 30 | if (el.getClass() == XmlElement.class) { 31 | XmlElement xl = (XmlElement) el; 32 | for (Attribute attr : xl.getAttributes()) { 33 | if (attr.getName().equals("prefix") && attr.getValue().equals("(")) { 34 | sets = xl; 35 | } else if (attr.getName().equals("prefix") && attr.getValue().equals("values (")) { 36 | setVs = xl; 37 | } 38 | } 39 | } 40 | } 41 | if(sets != null && setVs != null) { 42 | List fields = sets.getElements(); 43 | for (int i = 0; i < fields.size(); i++) { 44 | List ctnts = ((XmlElement) fields.get(i)).getElements(); 45 | for (Element ctnt : ctnts) { 46 | TextElement tx = (TextElement)ctnt; 47 | if (tx.getContent().equals(column + ",")) { 48 | index = i; 49 | break; 50 | } 51 | } 52 | } 53 | if (index > -1) { 54 | fields.remove(index); 55 | fields.add(index, new TextElement(column + ",")); 56 | setVs.getElements().remove(index); 57 | setVs.getElements().add(index, new TextElement(("0,"))); 58 | } 59 | } 60 | } 61 | 62 | return true; 63 | } 64 | 65 | @Override 66 | public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 67 | String column = introspectedTable.getTableConfigurationProperty(OPTIMISTIC_LOCK_COLUMN); 68 | if (column != null && !"".equals(column)) { 69 | int nodeIndex = -1; 70 | int fieldIndex = -1; 71 | List elements = element.getElements(); 72 | int start = 0; 73 | for (int i = 0; i < elements.size(); i++) { 74 | if (elements.get(i).getClass() == TextElement.class) { 75 | start = i; 76 | break; 77 | } 78 | } 79 | int middle = (elements.size() - start) / 2; 80 | 81 | for (int i = start; i <= middle; i++) { 82 | if(fieldIndex > -1) { 83 | break; 84 | } 85 | Element node = elements.get(i); 86 | if (node.getClass() == TextElement.class) { 87 | TextElement sets = (TextElement) node; 88 | String[] fields = sets.getContent().split(","); 89 | for (int j = 0; j < fields.length; j++) { 90 | if (column.equals(fields[j].trim()) || fields[j].trim().equals(column + ")")) { 91 | fieldIndex = j; 92 | nodeIndex = i; 93 | break; 94 | } 95 | } 96 | } 97 | } 98 | int index = middle + nodeIndex; 99 | TextElement values = (TextElement)elements.get(index); 100 | boolean needComma = values.getContent().trim().lastIndexOf(",") == values.getContent().trim().length() - 1; 101 | boolean needRightBracket = values.getContent().trim().lastIndexOf(")") == values.getContent().trim().length() - 1; 102 | String[] temp = values.getContent().trim().split(","); 103 | String[] vs = new String[temp.length / 2]; 104 | for (int i = 0; i < temp.length; i+=2) { 105 | vs[i / 2] = temp[i] + "," + temp[i + 1]; 106 | } 107 | vs[fieldIndex] = "0"; 108 | StringBuilder sb = new StringBuilder(); 109 | for (int i = 0; i < vs.length; i++) { 110 | sb.append(vs[i].trim()); 111 | if (i != vs.length - 1) { 112 | sb.append(", "); 113 | } 114 | } 115 | if (needComma) { 116 | sb.append(", "); 117 | } 118 | if (needRightBracket) { 119 | sb.append(")"); 120 | } 121 | elements.remove(index); 122 | elements.add(index, new TextElement(sb.toString())); 123 | } 124 | return true; 125 | } 126 | 127 | @Override 128 | public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 129 | this.replaceSelective(element, introspectedTable); 130 | return true; 131 | } 132 | 133 | @Override 134 | public boolean sqlMapUpdateByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 135 | this.replace(element, introspectedTable); 136 | return true; 137 | } 138 | 139 | @Override 140 | public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 141 | this.replace(element, introspectedTable); 142 | return true; 143 | } 144 | 145 | @Override 146 | public boolean sqlMapUpdateByPrimaryKeySelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 147 | this.replaceSelective(element, introspectedTable); 148 | return true; 149 | } 150 | 151 | @Override 152 | public boolean sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 153 | this.replace(element, introspectedTable); 154 | return true; 155 | } 156 | 157 | @Override 158 | public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 159 | this.replace(element, introspectedTable); 160 | return true; 161 | } 162 | 163 | private void replaceSelective(XmlElement element, IntrospectedTable introspectedTable) { 164 | String column = introspectedTable.getTableConfigurationProperty(OPTIMISTIC_LOCK_COLUMN); 165 | if (column != null && !"".equals(column)) { 166 | Element set = element.getElements().get(1); 167 | XmlElement setXml = (XmlElement) set; 168 | List fields = setXml.getElements(); 169 | int index = -1; 170 | String newSql = null; 171 | for (int i = 0; i < fields.size(); i++) { 172 | if (index > -1) { 173 | break; 174 | } 175 | if (fields.get(i).getClass() == XmlElement.class) { 176 | XmlElement fieldXml = (XmlElement) fields.get(i); 177 | 178 | List elements = fieldXml.getElements(); 179 | for (Element element1 : elements) { 180 | if (element1.getClass() == TextElement.class) { 181 | TextElement oldSql = (TextElement) element1; 182 | if (oldSql.getContent().contains(column + " = ")) { 183 | index = i; 184 | String comma = oldSql.getContent().lastIndexOf(",") == oldSql.getContent().length() - 1 ? "," : ""; 185 | String[] sqlArr = oldSql.getContent().split(column); 186 | newSql = sqlArr[0] + column + " = " + column + " + 1" + comma; 187 | break; 188 | } 189 | } 190 | } 191 | } 192 | } 193 | if (index > -1) { 194 | fields.remove(index); 195 | fields.add(index, new TextElement(newSql)); 196 | } 197 | } 198 | } 199 | 200 | private void replace(XmlElement element, IntrospectedTable introspectedTable) { 201 | String column = introspectedTable.getTableConfigurationProperty(OPTIMISTIC_LOCK_COLUMN); 202 | if (column != null && !"".equals(column)) { 203 | List elements = element.getElements(); 204 | int index = -1; 205 | String newsql = null; 206 | for (int i = 0; i < elements.size(); i++) { 207 | if (elements.get(i).getClass() == TextElement.class) { 208 | TextElement setSql = (TextElement) elements.get(i); 209 | if (setSql.getContent().contains(column + " = ")) { 210 | index = i; 211 | String comma = setSql.getContent().lastIndexOf(",") == setSql.getContent().length() - 1 ? "," : ""; 212 | String[] sqlArr = setSql.getContent().split(column); 213 | newsql = sqlArr[0] + column + " = " + column + " + 1" + comma; 214 | break; 215 | } 216 | } 217 | } 218 | if (index > -1) { 219 | elements.remove(index); 220 | elements.add(index, new TextElement(newsql)); 221 | } 222 | } 223 | } 224 | 225 | private static final String OPTIMISTIC_LOCK_COLUMN = "optimisticLockColumn"; 226 | } 227 | -------------------------------------------------------------------------------- /src/test/resources/mapping/TableTestSliceMonthMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | and ${criterion.condition} 22 | 23 | 24 | and ${criterion.condition} #{criterion.value} 25 | 26 | 27 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 28 | 29 | 30 | and ${criterion.condition} 31 | 32 | #{listItem} 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | and ${criterion.condition} 51 | 52 | 53 | and ${criterion.condition} #{criterion.value} 54 | 55 | 56 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 57 | 58 | 59 | and ${criterion.condition} 60 | 61 | #{listItem} 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | ID, SLICE_MONTH_ID, JACKSON_ID1, VERSION, JACKSON_ID2, JACKSON_TIME, COULD_SUM_COL 73 | 74 | 91 | 97 | 98 | delete from TABLE_TEST_SLICE_${tableNameSuffix} 99 | where ID = #{id,jdbcType=BIGINT} 100 | 101 | 102 | delete from TABLE_TEST_SLICE_${tableNameSuffix} 103 | 104 | 105 | 106 | 107 | 108 | 109 | SELECT LAST_INSERT_ID() 110 | 111 | insert into TABLE_TEST_SLICE_${tableNameSuffix} (SLICE_MONTH_ID, JACKSON_ID1, VERSION, 112 | JACKSON_ID2, JACKSON_TIME, COULD_SUM_COL 113 | ) 114 | values (#{sliceMonthId,jdbcType=TIMESTAMP}, #{jacksonId1,jdbcType=BIGINT}, 0, 115 | #{jacksonId2,jdbcType=VARCHAR}, #{jacksonTime,jdbcType=TIMESTAMP}, #{couldSumCol,jdbcType=INTEGER} 116 | ) 117 | 118 | 119 | 120 | SELECT LAST_INSERT_ID() 121 | 122 | insert into TABLE_TEST_SLICE_${tableNameSuffix} 123 | 124 | 125 | SLICE_MONTH_ID, 126 | 127 | 128 | JACKSON_ID1, 129 | 130 | VERSION, 131 | 132 | JACKSON_ID2, 133 | 134 | 135 | JACKSON_TIME, 136 | 137 | 138 | COULD_SUM_COL, 139 | 140 | 141 | 142 | 143 | #{sliceMonthId,jdbcType=TIMESTAMP}, 144 | 145 | 146 | #{jacksonId1,jdbcType=BIGINT}, 147 | 148 | 0, 149 | 150 | #{jacksonId2,jdbcType=VARCHAR}, 151 | 152 | 153 | #{jacksonTime,jdbcType=TIMESTAMP}, 154 | 155 | 156 | #{couldSumCol,jdbcType=INTEGER}, 157 | 158 | 159 | 160 | 166 | 167 | update TABLE_TEST_SLICE_${record.tableNameSuffix}TABLE_TEST_SLICE_${example.tableNameSuffix}TABLE_TEST_SLICE_ 168 | 169 | 170 | ID = #{record.id,jdbcType=BIGINT}, 171 | 172 | 173 | SLICE_MONTH_ID = #{record.sliceMonthId,jdbcType=TIMESTAMP}, 174 | 175 | 176 | JACKSON_ID1 = #{record.jacksonId1,jdbcType=BIGINT}, 177 | 178 | VERSION = VERSION + 1, 179 | 180 | JACKSON_ID2 = #{record.jacksonId2,jdbcType=VARCHAR}, 181 | 182 | 183 | JACKSON_TIME = #{record.jacksonTime,jdbcType=TIMESTAMP}, 184 | 185 | 186 | COULD_SUM_COL = #{record.couldSumCol,jdbcType=INTEGER}, 187 | 188 | 189 | ${record.updateSql}, 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | update TABLE_TEST_SLICE_${record.tableNameSuffix}TABLE_TEST_SLICE_${example.tableNameSuffix}TABLE_TEST_SLICE_ 198 | set ID = #{record.id,jdbcType=BIGINT}, 199 | SLICE_MONTH_ID = #{record.sliceMonthId,jdbcType=TIMESTAMP}, 200 | JACKSON_ID1 = #{record.jacksonId1,jdbcType=BIGINT}, 201 | VERSION = VERSION + 1, 202 | JACKSON_ID2 = #{record.jacksonId2,jdbcType=VARCHAR}, 203 | JACKSON_TIME = #{record.jacksonTime,jdbcType=TIMESTAMP}, 204 | COULD_SUM_COL = #{record.couldSumCol,jdbcType=INTEGER} 205 | 206 | 207 | 208 | 209 | 210 | update TABLE_TEST_SLICE_${tableNameSuffix} 211 | 212 | 213 | SLICE_MONTH_ID = #{sliceMonthId,jdbcType=TIMESTAMP}, 214 | 215 | 216 | JACKSON_ID1 = #{jacksonId1,jdbcType=BIGINT}, 217 | 218 | VERSION = VERSION + 1, 219 | 220 | JACKSON_ID2 = #{jacksonId2,jdbcType=VARCHAR}, 221 | 222 | 223 | JACKSON_TIME = #{jacksonTime,jdbcType=TIMESTAMP}, 224 | 225 | 226 | COULD_SUM_COL = #{couldSumCol,jdbcType=INTEGER}, 227 | 228 | 229 | where ID = #{id,jdbcType=BIGINT} 230 | 231 | 232 | update TABLE_TEST_SLICE_${tableNameSuffix} 233 | set SLICE_MONTH_ID = #{sliceMonthId,jdbcType=TIMESTAMP}, 234 | JACKSON_ID1 = #{jacksonId1,jdbcType=BIGINT}, 235 | VERSION = VERSION + 1, 236 | JACKSON_ID2 = #{jacksonId2,jdbcType=VARCHAR}, 237 | JACKSON_TIME = #{jacksonTime,jdbcType=TIMESTAMP}, 238 | COULD_SUM_COL = #{couldSumCol,jdbcType=INTEGER} 239 | where ID = #{id,jdbcType=BIGINT} 240 | 241 | 255 | 261 | 262 | 263 | 264 | insert into TABLE_TEST_SLICE_${tableNameSuffix} (SLICE_MONTH_ID, JACKSON_ID1, VERSION, 265 | JACKSON_ID2, JACKSON_TIME, COULD_SUM_COL 266 | ) 267 | values 268 | 269 | (#{item.sliceMonthId,jdbcType=TIMESTAMP}, #{item.jacksonId1,jdbcType=BIGINT}, 0, 270 | #{item.jacksonId2,jdbcType=VARCHAR}, #{item.jacksonTime,jdbcType=TIMESTAMP}, #{item.couldSumCol,jdbcType=INTEGER} 271 | ) 272 | 273 | 274 | 275 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Introduction 2 | 3 | mybatis3-generator-plugins的目的是扩展[MBG](http://www.mybatis.org/generator)的代码生成功能。如其名称所示,通过mybatis3-generator-plugins生成的java代码遵循 [MyBatis 3.x](http://www.mybatis.org/mybatis-3/) 的规范。以下内容假定用户熟悉[MBG](http://www.mybatis.org/generator)的基本配置和使用。 4 | 5 | ##NOTE: 6 | 受sql dialect的限制,多数plugin目前仅支持 Mysql 5.x。 7 | 8 | ##Download 9 | 在 Maven Central 上最新的发布版本是: 10 | 11 | ```xml 12 | 13 | com.github.oceanc 14 | mybatis3-generator-plugin 15 | 0.4.0 16 | 17 | ``` 18 | 19 | mybatis3-generator-plugins目前提供了如下可用插件: 20 | 21 | * BatchInsertPlugin 22 | * JacksonAnnotationPlugin 23 | * JacksonToJsonPlugin 24 | * LombokAnnotationPlugin 25 | * MinMaxPlugin 26 | * OptimisticLockAutoIncreasePlugin 27 | * PaginationPlugin 28 | * SliceTablePlugin 29 | * SumSelectivePlugin 30 | * UpdateSqlTextOfUpdateSelectivePlugin 31 | * WhereSqlTextPlugin 32 | 33 | 34 | #How to use it 35 | 36 | 在[MyBatis GeneratorXML Configuration File](http://www.mybatis.org/generator/configreference/xmlconfig.html)中添加你需要用到的``元素: 37 | 38 | ```xml 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | ...... 47 | ...... 48 | 49 | ``` 50 | 51 | 为了举例,假设我们创建一张简单的账户表,并命名为`Account`。DDL如下: 52 | 53 | ```sql 54 | CREATE TABLE `Account` ( 55 | `id` bigint(16) NOT NULL, 56 | `create_time` timestamp NOT NULL, 57 | `name` varchar(64) DEFAULT NULL, 58 | `age` tinyint(3) DEFAULT NULL, 59 | `version` int(11) NOT NULL, 60 | PRIMARY KEY (`id`) 61 | )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 62 | ``` 63 | 64 | ##NOTE: 65 | 如果使用了SliceTablePlugin,为了其产生的作用始终有效,推荐将其放置在自定义\声明的第一条,如上例所示。这么做的原因是,其他plugin(BatchInsertPlugin、MinMaxPlugin、和SumSelectivePlugin)会通过检测SliceTablePlugin是否应用,来适配其效果。 66 | 67 | 68 |
69 | ##BatchInsertPlugin 70 | 该plugin是为了增加批量insert的功能。特别适用于初始化数据、或迁移数据。 71 | 72 | ###Java sample 73 | 74 | ```java 75 | AccountMapper mapper = ... 76 | List as = new ArrayList<>(); 77 | as.add(...) 78 | mapper.batchInsert(as); 79 | ``` 80 | 如果使用了SliceTablePlugin,则需要对List中每一个Account实例设置分表因子: 81 | 82 | ```java 83 | AccountMapper mapper = ... 84 | List as = new ArrayList<>(); 85 | for (Account account : accounts) { 86 | account.setId(...); 87 | // or account.setCreateTime(...); 88 | } 89 | mapper.batchInsert(as); 90 | ``` 91 | 92 | * 通过取模分表时,必须调用`setId`并传入合适的参数 93 | * 通过自然月分表时,必须调用`setCreateTime`并传入合适的参数 94 | 95 | 96 |
97 | ##JacksonAnnotationPlugin 98 | 该plugin是为生成的model类添加[jackson](https://github.com/FasterXML/jackson)的`@JsonProperty`、`@JsonIgnore`、 和`@JsonFormat`注解。若使用此插件,需要额外依赖`jackson 2.5 +`。 99 | 100 | ###Xml config: 101 | 在[MyBatis GeneratorXML Configuration File](http://www.mybatis.org/generator/configreference/xmlconfig.html)的``元素中添加四个\(可选的): 102 | 103 | ```xml 104 |
105 | 106 | 107 | 108 | 109 |
110 | ``` 111 | * `jacksonColumns`指需要添加`@JsonProperty`注解的列,由`,`分割的列名组成。该\必须和`jacksonProperties`成对出现 112 | * `jacksonProperties`指`@JsonProperty`注解所需的参数值,由`,`分割,这些值的数量和顺序必须和`jacksonColumns`的值一一对应。该\必须和`jacksonColumns`成对出现 113 | * `jacksonFormats`指需要添加`@JsonFormat`注解的列,值由`,`分割的键值对组成,键值对由`@`分割,键为列名,值为`@JsonFormat`所需参数 114 | * `jacksonIgnores`指需要添加`@JsonIgnore`注解的列,值由`,`分割的列名组成 115 | 116 | ###Java sample 117 | 118 | ```java 119 | public class Account implements Serializable { 120 | @JsonIgnore 121 | private Long id; 122 | @JsonProperty("nickName") 123 | private String name; 124 | @JsonProperty("realAge") 125 | private Integer age; 126 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 127 | private Date createTime; 128 | @JsonIgnore 129 | private Long version; 130 | } 131 | ``` 132 | 133 | 134 |
135 | ##JacksonToJsonPlugin 136 | 该plugin是为生成的model类添加`toJson`方法。`toJson`方法的实现依赖于[jackson](https://github.com/FasterXML/jackson)。若使用此插件,需要额外依赖`jackson 2.5 +`。 137 | 138 | ###Java sample 139 | 140 | ```java 141 | public class Account implements Serializable { 142 | public String toJson() throws IOException { 143 | ... ... 144 | } 145 | } 146 | ``` 147 | 148 | 149 |
150 | ##LombokAnnotationPlugin 151 | 该plugin是为生成的model类添加[lombok](https://projectlombok.org/)注解,避免了java bean中繁琐的setter和getter。生成的代码看起来也更干净、紧凑。若使用此插件,需要额外依赖lombok。 152 | 153 | ###Java sample 154 | 155 | ```java 156 | @Data 157 | public class Account implements Serializable { 158 | private Long id; 159 | ... ... 160 | } 161 | ``` 162 | 163 | 164 |
165 | ##MinMaxPlugin 166 | 167 | ###Xml config: 168 | 在[MyBatis GeneratorXML Configuration File](http://www.mybatis.org/generator/configreference/xmlconfig.html)的``元素中添加两个\(可选的): 169 | 170 | ```xml 171 |
172 | 173 | 174 |
175 | ``` 176 | * `minColumns`是可进行min操作的列,值由`,`分割的列名组成 177 | * `maxColumns`是可进行max操作的列,值由`,`分割的列名组成 178 | 179 | ###Java sample 180 | 181 | ```java 182 | AccountMapper mapper = ... 183 | AccountExample example = new AccountExample(); 184 | example.createCriteria().andAgeEqualTo(33); 185 | long min = mapper.minIdByExample(example); 186 | long max = mapper.maxIdByExample(example); 187 | ``` 188 | 如果使用了SliceTablePlugin,别忘了对分表因子赋值:`example.partitionFactorCreateTime(...)` 189 | 190 | 191 |
192 | ##OptimisticLockAutoIncreasePlugin 193 | 在处理并发写操作时,如果数据竞争较低,通常会采用乐观锁,避免并发问题的同时获得较好的性能。实现乐观锁的一般方式是在数据中添加版本信息,就像Account表中的version列。当写操作成功时,版本信息也相应递增。该plugin就是解决version自动递增问题的,可以避免手动对version+1。 194 | 195 | ###Xml config: 196 | 在[MyBatis GeneratorXML Configuration File](http://www.mybatis.org/generator/configreference/xmlconfig.html)的``元素中添加一个\: 197 | 198 | ```xml 199 |
200 | 201 |
202 | ``` 203 | * `optimisticLockColumn`指具有版本信息语义的列,`version`是具体的列名 204 | 205 | ###Java sample 206 | 207 | ```java 208 | AccountMapper mapper = ... 209 | Account record = new Account(); 210 | record.setName("tom"); 211 | // record.setVersion(1) 无须手工对version进行赋值 212 | AccountExample example = new AccountExample(); 213 | example.createCriteria().andAgeEqualTo(33); 214 | mapper.updateByExampleSelective(record, example); 215 | ``` 216 | 如果使用了SliceTablePlugin,别忘了对分表因子赋值:`example.partitionFactorId(...)` 217 | 218 | 219 |
220 | ##PaginationPlugin 221 | 222 | ###Java sample 223 | 224 | ```java 225 | AccountMapper mapper = ... 226 | AccountExample example = new AccountExample(); 227 | example.page(0, 2).createCriteria().andAgeEqualTo(33); 228 | List as = mapper.selectByExample(example); 229 | ``` 230 | 如果使用了SliceTablePlugin,别忘了对分表因子赋值:`example.partitionFactorCreateTime(...)` 231 | 232 | 233 |
234 | ## SliceTablePlugin 235 | 236 | 当数据库单表数据量过大时,可以通过水平拆分把单表分解为多表。例如,若Account表中的预期数据量过大时(也许5、6亿),可以把一张Account表分解为多张Account表。**SliceTablePlugin并不会自动创建和执行拆分后的DDL,因此必须手工创建DDL并按下述约定修改表名。** 237 | 238 | SliceTablePlugin目前支持两种分表命名方式: 239 | 240 | * 对指定列取模。拆分后的表名为`原表名_N`。如:Account\_0、Account\_1、Account\_3 ...... 241 | * 对指定的时间类型列,按单自然月拆分。拆分后的表名为`原表名_yyyyMM`。如:Account\_201501、Account\_201502 ...... Account\_201512 242 | 243 | 244 | ###Xml config: 245 | ####1.利用取模 246 | 假如通过Account表的`id`字段做分表,计划拆分为`97`张表。在[MyBatis GeneratorXML Configuration File](http://www.mybatis.org/generator/configreference/xmlconfig.html)的``元素中添加两个\: 247 | 248 | ```xml 249 |
250 | 251 | 252 |
253 | ``` 254 | * `sliceMod`指按取模方式分表,`97`是取模的模数 255 | * `sliceColumn`指取模所使用的列,`id`是具体列名 256 | 257 | ####2.利用自然月 258 | 假如通过Account表的`create_time `字段做拆分: 259 | 260 | ```xml 261 | 262 | 263 | 264 |
265 | ``` 266 | * `sliceMonth `指按自然月分表,`1`指按单个自然月。**Note:目前只支持按单月分表,此处的值 1 无实际意义** 267 | * `sliceColumn`指时间类型的列,`create_time`是具体列名 268 | 269 | 270 | ###Java sample 271 | ####insert 272 | 273 | ```java 274 | AccountMapper mapper = ... 275 | Account record = new Account(); 276 | record.setAge(33); 277 | record.setId(101); 278 | record.setCreateTime(new Date()); 279 | mapper.insert(record); 280 | // or mapper.insertSelective(record) 281 | ``` 282 | * 通过取模分表时,必须调用`setId`并传入合适的参数 283 | * 通过自然月分表时,必须调用`setCreateTime`并传入合适的参数 284 | 285 | ####read 286 | 287 | ```java 288 | AccountMapper mapper = ... 289 | AccountExample example = new AccountExample(); 290 | example.partitionFactorId(id).createCriteria().andAgeEqualTo(33); 291 | // or example.partitionFactorCreateTime(new Date()).createCriteria().andAgeEqualTo(33); 292 | List as = mapper.selectByExample(example); 293 | ``` 294 | * 通过取模分表时,`partitionFactorId`方法表示分表因子是Id字段,必须调用该方法并传入合适的参数 295 | * 通过自然月分表时,`partitionFactorCreateTime`方法表示分表因子是createTime字段,必须调用该方法并传入合适的参数 296 | 297 | ####update 298 | 299 | ```java 300 | AccountMapper mapper = ... 301 | Account record = new Account(); 302 | record.setAge(33); 303 | AccountExample example = new AccountExample(); 304 | example.partitionFactorId(id).createCriteria().andAgeEqualTo(33); 305 | // or example.partitionFactorCreateTime(new Date()).createCriteria().andAgeEqualTo(33); 306 | mapper.updateByExampleSelective(record, example); 307 | ``` 308 | 上例的用法和read操作一样,在example对象上必须调用`partitionFactorId`或`partitionFactorCreateTime`方法。 309 | 除此之外,还可以用如下方式进行update: 310 | 311 | ```java 312 | AccountMapper mapper = ... 313 | Account record = new Account(); 314 | record.setCreateTime(new Date()); 315 | record.setId(101); 316 | // or record.setCreateTime(new Date()); 317 | AccountExample example = new AccountExample(); 318 | example.createCriteria().andAgeEqualTo(33); 319 | mapper.updateByExampleSelective(record, example); 320 | ``` 321 | 由于在record对象调用了`setId`或`setCreateTime`,就无须在example对象指定分表因子。 322 | 323 | ####delete 324 | 325 | ```java 326 | AccountMapper mapper = ... 327 | AccountExample example = new AccountExample(); 328 | example.partitionFactorId(id).createCriteria().andAgeEqualTo(33); 329 | // or example.partitionFactorCreateTime(new Date()).createCriteria().andVersionEqualTo(0); 330 | mapper.deleteByExample(example); 331 | ``` 332 | * 通过取模分表时,`partitionFactorId`方法表示分表因子是Id字段,必须调用该方法并传入合适的参数 333 | * 通过自然月分表时,`partitionFactorCreateTime`方法表示分表因子是createTime字段,必须调用该方法并传入合适的参数 334 | 335 | ####other 336 | 当无法获得分表因子的值时、或者确定所操作的表名时,可以通过: 337 | 338 | * `record.setTableNameSuffix(...)`取代`record.setId(...)`或`record.setId(...)` 339 | * `example.setTableNameSuffix(...)`取代`example.partitionFactorId(...)` 340 | 341 | WARNING:由于`setTableNameSuffix`的参数是String类型,在 Mybatis3 的 mapper xml 中生成`${}`变量,这种变量不会做sql转义,而直接嵌入到sql语句中。如果以用户输入作为`setTableNameSuffix`的参数,会导致潜在的`SQL Injection`攻击,需谨慎使用。 342 | 343 | 344 |
345 | ##SumSelectivePlugin 346 | 347 | ###Java sample 348 | 349 | ```java 350 | AccountMapper mapper = ... 351 | AccountExample example = new AccountExample(); 352 | example.sumAge().createCriteria().andVersionEqualTo(0); 353 | long sum = mapper.sumByExample(example); 354 | ``` 355 | 如果使用了SliceTablePlugin,别忘了对分表因子赋值:`example.partitionFactorCreateTime(...)` 356 | 357 | 358 |
359 | ##UpdateSqlTextOfUpdateSelectivePlugin 360 | 361 | ###Java sample 362 | 363 | ```java 364 | AccountMapper mapper = ... 365 | Account record = new Account(); 366 | record.setUpdateSql("version = version + 1") 367 | record.setAge(33); 368 | AccountExample example = new AccountExample(); 369 | example.createCriteria().andAgeEqualTo(22); 370 | mapper.updateByExampleSelective(record.setAge, example); 371 | ``` 372 | 如果使用了SliceTablePlugin,别忘了对分表因子赋值:`example.partitionFactorCreateTime(...)` 373 | WARNING:由于`setUpdateSql`的参数是String类型,在 Mybatis3 的 mapper xml 中生成`${}`变量,这种变量不会做sql转义,而直接嵌入到sql语句中。如果以用户输入作为`setUpdateSql`的参数,会导致潜在的`SQL Injection`攻击,需谨慎使用。 374 | 375 | 376 |
377 | ##WhereSqlTextPlugin 378 | 379 | ###Java sample 380 | 381 | ```java 382 | AccountMapper mapper = ... 383 | int v = 1; 384 | AccountExample example = new AccountExample(); 385 | example.createCriteria().andAgeEqualTo(33).addConditionSql("version =" + v + " + 1"); 386 | List as = mapper.selectByExample(example); 387 | ``` 388 | 如果使用了SliceTablePlugin,别忘了对分表因子赋值:`example.partitionFactorCreateTime(...)` 389 | WARNING:由于`addConditionSql`的参数是String类型,在 Mybatis3 的 mapper xml 中生成`${}`变量,这种变量不会做sql转义,而直接嵌入到sql语句中。如果以用户输入作为`addConditionSql`的参数,会导致潜在的`SQL Injection`攻击,需谨慎使用。 -------------------------------------------------------------------------------- /src/test/resources/mapping/TableTestSliceModMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | and ${criterion.condition} 22 | 23 | 24 | and ${criterion.condition} #{criterion.value} 25 | 26 | 27 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 28 | 29 | 30 | and ${criterion.condition} 31 | 32 | #{listItem} 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | and ${criterion.condition} 51 | 52 | 53 | and ${criterion.condition} #{criterion.value} 54 | 55 | 56 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 57 | 58 | 59 | and ${criterion.condition} 60 | 61 | #{listItem} 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | ID, SLICE_MOD_ID, JACKSON_ID1, JACKSON_ID2, JACKSON_TIME, COULD_SUM_COL, VERSION 73 | 74 | 91 | 97 | 98 | delete from TABLE_TEST_SLICE_MOD_${tableNameSuffix} 99 | where ID = #{id,jdbcType=BIGINT} 100 | 101 | 102 | delete from TABLE_TEST_SLICE_MOD_${tableNameSuffix} 103 | 104 | 105 | 106 | 107 | 108 | 109 | SELECT LAST_INSERT_ID() 110 | 111 | insert into TABLE_TEST_SLICE_MOD_${tableNameSuffix} (SLICE_MOD_ID, JACKSON_ID1, JACKSON_ID2, 112 | JACKSON_TIME, COULD_SUM_COL, VERSION 113 | ) 114 | values (#{sliceModId,jdbcType=BIGINT}, #{jacksonId1,jdbcType=BIGINT}, #{jacksonId2,jdbcType=VARCHAR}, 115 | #{jacksonTime,jdbcType=TIMESTAMP}, #{couldSumCol,jdbcType=INTEGER}, 0 116 | ) 117 | 118 | 119 | 120 | SELECT LAST_INSERT_ID() 121 | 122 | insert into TABLE_TEST_SLICE_MOD_${tableNameSuffix} 123 | 124 | 125 | SLICE_MOD_ID, 126 | 127 | 128 | JACKSON_ID1, 129 | 130 | 131 | JACKSON_ID2, 132 | 133 | 134 | JACKSON_TIME, 135 | 136 | 137 | COULD_SUM_COL, 138 | 139 | VERSION, 140 | 141 | 142 | 143 | #{sliceModId,jdbcType=BIGINT}, 144 | 145 | 146 | #{jacksonId1,jdbcType=BIGINT}, 147 | 148 | 149 | #{jacksonId2,jdbcType=VARCHAR}, 150 | 151 | 152 | #{jacksonTime,jdbcType=TIMESTAMP}, 153 | 154 | 155 | #{couldSumCol,jdbcType=INTEGER}, 156 | 157 | 0, 158 | 159 | 160 | 166 | 167 | update TABLE_TEST_SLICE_MOD_${record.tableNameSuffix}TABLE_TEST_SLICE_MOD_${example.tableNameSuffix}TABLE_TEST_SLICE_MOD_ 168 | 169 | 170 | ID = #{record.id,jdbcType=BIGINT}, 171 | 172 | 173 | SLICE_MOD_ID = #{record.sliceModId,jdbcType=BIGINT}, 174 | 175 | 176 | JACKSON_ID1 = #{record.jacksonId1,jdbcType=BIGINT}, 177 | 178 | 179 | JACKSON_ID2 = #{record.jacksonId2,jdbcType=VARCHAR}, 180 | 181 | 182 | JACKSON_TIME = #{record.jacksonTime,jdbcType=TIMESTAMP}, 183 | 184 | 185 | COULD_SUM_COL = #{record.couldSumCol,jdbcType=INTEGER}, 186 | 187 | VERSION = VERSION + 1, 188 | 189 | ${record.updateSql}, 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | update TABLE_TEST_SLICE_MOD_${record.tableNameSuffix}TABLE_TEST_SLICE_MOD_${example.tableNameSuffix}TABLE_TEST_SLICE_MOD_ 198 | set ID = #{record.id,jdbcType=BIGINT}, 199 | SLICE_MOD_ID = #{record.sliceModId,jdbcType=BIGINT}, 200 | JACKSON_ID1 = #{record.jacksonId1,jdbcType=BIGINT}, 201 | JACKSON_ID2 = #{record.jacksonId2,jdbcType=VARCHAR}, 202 | JACKSON_TIME = #{record.jacksonTime,jdbcType=TIMESTAMP}, 203 | COULD_SUM_COL = #{record.couldSumCol,jdbcType=INTEGER}, 204 | VERSION = VERSION + 1 205 | 206 | 207 | 208 | 209 | 210 | update TABLE_TEST_SLICE_MOD_${tableNameSuffix} 211 | 212 | 213 | SLICE_MOD_ID = #{sliceModId,jdbcType=BIGINT}, 214 | 215 | 216 | JACKSON_ID1 = #{jacksonId1,jdbcType=BIGINT}, 217 | 218 | 219 | JACKSON_ID2 = #{jacksonId2,jdbcType=VARCHAR}, 220 | 221 | 222 | JACKSON_TIME = #{jacksonTime,jdbcType=TIMESTAMP}, 223 | 224 | 225 | COULD_SUM_COL = #{couldSumCol,jdbcType=INTEGER}, 226 | 227 | VERSION = VERSION + 1, 228 | 229 | where ID = #{id,jdbcType=BIGINT} 230 | 231 | 232 | update TABLE_TEST_SLICE_MOD_${tableNameSuffix} 233 | set SLICE_MOD_ID = #{sliceModId,jdbcType=BIGINT}, 234 | JACKSON_ID1 = #{jacksonId1,jdbcType=BIGINT}, 235 | JACKSON_ID2 = #{jacksonId2,jdbcType=VARCHAR}, 236 | JACKSON_TIME = #{jacksonTime,jdbcType=TIMESTAMP}, 237 | COULD_SUM_COL = #{couldSumCol,jdbcType=INTEGER}, 238 | VERSION = VERSION + 1 239 | where ID = #{id,jdbcType=BIGINT} 240 | 241 | 255 | 261 | 262 | 263 | 264 | insert into TABLE_TEST_SLICE_MOD_${tableNameSuffix} (SLICE_MOD_ID, JACKSON_ID1, JACKSON_ID2, 265 | JACKSON_TIME, COULD_SUM_COL, VERSION 266 | ) 267 | values 268 | 269 | (#{item.sliceModId,jdbcType=BIGINT}, #{item.jacksonId1,jdbcType=BIGINT}, #{item.jacksonId2,jdbcType=VARCHAR}, 270 | #{item.jacksonTime,jdbcType=TIMESTAMP}, #{item.couldSumCol,jdbcType=INTEGER}, 0 271 | ) 272 | 273 | 274 | 275 | 281 | 287 | 293 | 299 | -------------------------------------------------------------------------------- /src/main/java/com/github/oceanc/mybatis3/generator/plugin/SliceTablePlugin.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin; 2 | 3 | import org.mybatis.generator.api.IntrospectedColumn; 4 | import org.mybatis.generator.api.IntrospectedTable; 5 | import org.mybatis.generator.api.PluginAdapter; 6 | import org.mybatis.generator.api.dom.java.*; 7 | import org.mybatis.generator.api.dom.xml.Attribute; 8 | import org.mybatis.generator.api.dom.xml.TextElement; 9 | import org.mybatis.generator.api.dom.xml.XmlElement; 10 | import org.mybatis.generator.config.TableConfiguration; 11 | 12 | import java.util.Arrays; 13 | import java.util.List; 14 | 15 | /** 16 | * Created by chengyang 17 | */ 18 | public class SliceTablePlugin extends PluginAdapter { 19 | 20 | @Override 21 | public boolean validate(List warnings) { 22 | return true; 23 | } 24 | 25 | @Override 26 | public void initialized(IntrospectedTable introspectedTable) { 27 | if (needPartition(introspectedTable)) { 28 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 29 | String modValue = introspectedTable.getTableConfigurationProperty(MOD_VALUE); 30 | String month = introspectedTable.getTableConfigurationProperty(TIME_VALUE); 31 | if ((modValue != null && !"".equals(modValue)) || (month != null && !"".equals(month))) { 32 | String baseName = tableName.substring(0, tableName.lastIndexOf(UNDERLINE)); 33 | introspectedTable.setSqlMapAliasedFullyQualifiedRuntimeTableName(baseName + SUFFIX); 34 | introspectedTable.setSqlMapFullyQualifiedRuntimeTableName(baseName + SUFFIX); 35 | } 36 | } 37 | } 38 | 39 | @Override 40 | public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 41 | if (needPartition(introspectedTable)) { 42 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 43 | String relColumn = introspectedTable.getTableConfigurationProperty(REL_COLUMN); 44 | String modValue = introspectedTable.getTableConfigurationProperty(MOD_VALUE); 45 | String month = introspectedTable.getTableConfigurationProperty(TIME_VALUE); 46 | String fieldName = convertColumnName(relColumn, introspectedTable.getTableConfiguration()); 47 | 48 | if (modValue != null && !"".equals(modValue)) { 49 | FullyQualifiedJavaType ptype = STRING_TYPE; 50 | // 确定参数类型 51 | for (InnerClass innerClass : topLevelClass.getInnerClasses()) { 52 | if (FullyQualifiedJavaType.getGeneratedCriteriaInstance().equals(innerClass.getType())) { 53 | for (Method method : innerClass.getMethods()) { 54 | String FN = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); 55 | if (method.getName().equals("and" + FN + "EqualTo")) { 56 | Parameter parm = method.getParameters().get(0); 57 | ptype = parm.getType(); 58 | } 59 | } 60 | } 61 | } 62 | String[] expression = new String[12]; 63 | expression[0] = "if (" + fieldName + " != null ) {"; 64 | expression[1] = "long nan = 0;"; 65 | expression[2] = "StringBuilder sb = new StringBuilder(\"0\");"; 66 | expression[3] = "for (char c : String.valueOf(" + fieldName + ").toCharArray()) {"; 67 | expression[4] = "if (Character.isDigit(c)) sb.append(c);"; 68 | expression[5] = "else nan += c;"; 69 | expression[6] = "}"; 70 | expression[7] = "long lid = new BigDecimal(sb.toString()).longValue();"; 71 | expression[8] = "if(nan > 0) lid += " + modValue + " + nan;"; 72 | expression[9] = "this." + SUFFIX_FIELD + " = (Math.abs(lid) % " + modValue + ") + \"\";"; 73 | expression[10] = "}"; 74 | expression[11] = "return this;"; 75 | Method method = makePartitionMethod(ptype, topLevelClass.getType(), fieldName, tableName, expression); 76 | topLevelClass.addImportedType(BIGDECIMAL_TYPE); 77 | topLevelClass.addMethod(method); 78 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add method " + method.getName() + "."); 79 | // PluginUtils.addProperty(SUFFIX_FIELD, topLevelClass, this.getContext(), tableName); 80 | } else if (month != null && !"".equals(month)) { 81 | String[] expression = new String[7]; 82 | expression[0] = "if (" + fieldName + " != null ) {"; 83 | expression[1] = "Calendar calendar = Calendar.getInstance();"; 84 | expression[2] = "calendar.setTimeInMillis(" + fieldName + ".getTime());"; 85 | expression[3] = "int m = calendar.get(Calendar.MONTH) + 1;"; 86 | expression[4] = "this." + SUFFIX_FIELD + " = calendar.get(Calendar.YEAR) + (m < 10 ? \"0\" + m : \"\" + m);"; 87 | expression[5] = "}"; 88 | expression[6] = "return this;"; 89 | Method method = makePartitionMethod(DATE_TYPE, topLevelClass.getType(), fieldName, tableName, expression); 90 | topLevelClass.addImportedType(CALENDAR_TYPE); 91 | topLevelClass.addMethod(method); 92 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " add method " + method.getName() + "."); 93 | // PluginUtils.addProperty(SUFFIX_FIELD, topLevelClass, this.getContext(), tableName); 94 | } 95 | PluginUtils.addProperty(SUFFIX_FIELD, STRING_TYPE, topLevelClass, this.getContext(), tableName); 96 | } 97 | return true; 98 | } 99 | 100 | @Override 101 | public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 102 | if (needPartition(introspectedTable)) { 103 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 104 | PluginUtils.addProperty(SUFFIX_FIELD, STRING_TYPE, topLevelClass, this.getContext(), tableName); 105 | } 106 | return true; 107 | } 108 | 109 | @Override 110 | public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { 111 | if (needPartition(introspectedTable)) { 112 | String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime(); 113 | PluginUtils.addProperty(SUFFIX_FIELD, STRING_TYPE, topLevelClass, this.getContext(), tableName); 114 | } 115 | return true; 116 | } 117 | 118 | @Override 119 | public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { 120 | if (needPartition(introspectedTable)) { 121 | String relColumn = introspectedTable.getTableConfigurationProperty(REL_COLUMN); 122 | 123 | if (introspectedColumn.getActualColumnName().equals(relColumn)) { 124 | String modValue = introspectedTable.getTableConfigurationProperty(MOD_VALUE); 125 | String month = introspectedTable.getTableConfigurationProperty(TIME_VALUE); 126 | String field = introspectedColumn.getJavaProperty(); 127 | if (modValue != null && !"".equals(modValue)) { 128 | if (!isPrime(Long.parseLong(modValue))) { 129 | System.err.printf("modValue should be a prime number!!!!!!"); 130 | //throw new IllegalArgumentException("modValue not a prime number!!!!!!"); 131 | } 132 | String[] expression = new String[11]; 133 | expression[0] = "if (this." + field + " != null ) {"; 134 | expression[1] = "long nan = 0;"; 135 | expression[2] = "StringBuilder sb = new StringBuilder(\"0\");"; 136 | expression[3] = "for (char c : String.valueOf(" + field + ").toCharArray()) {"; 137 | expression[4] = "if (Character.isDigit(c)) sb.append(c);"; 138 | expression[5] = "else nan += c;"; 139 | expression[6] = "}"; 140 | expression[7] = "long lid = new BigDecimal(sb.toString()).longValue();"; 141 | expression[8] = "if(nan > 0) lid += " + modValue + " + nan;"; 142 | expression[9] = "this." + SUFFIX_FIELD + " = (Math.abs(lid) % " + modValue + ") + \"\";"; 143 | expression[10] = "}"; 144 | method.addBodyLines(Arrays.asList(expression)); 145 | topLevelClass.addImportedType(BIGDECIMAL_TYPE); 146 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " modify method " + method.getName() + " for update field " + SUFFIX_FIELD); 147 | } else if (month != null && !"".equals(month)) { 148 | int mc = Integer.parseInt(month); 149 | if (mc < 1 || mc > 12) { 150 | System.err.printf("month value should in [1-12]!!!!!!"); 151 | throw new IllegalArgumentException("month value should in [1-12]!!!!!!"); 152 | } 153 | String[] expression = new String[6]; 154 | expression[0] = "if (this." + field + " != null ) {"; 155 | expression[1] = "Calendar calendar = Calendar.getInstance();"; 156 | expression[2] = "calendar.setTimeInMillis(" + field + ".getTime());"; 157 | expression[3] = "int m = calendar.get(Calendar.MONTH) + 1;"; 158 | expression[4] = "this." + SUFFIX_FIELD + " = calendar.get(Calendar.YEAR) + (m < 10 ? \"0\" + m : \"\" + m);"; 159 | expression[5] = "}"; 160 | method.addBodyLines(Arrays.asList(expression)); 161 | topLevelClass.addImportedType(CALENDAR_TYPE); 162 | System.out.println("-----------------" + topLevelClass.getType().getShortName() + " modify method " + method.getName() + " for update field " + SUFFIX_FIELD); 163 | } 164 | } 165 | } 166 | return true; 167 | } 168 | 169 | @Override 170 | public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 171 | return this.dynamicTableName(element, introspectedTable); 172 | } 173 | 174 | @Override 175 | public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 176 | return this.dynamicTableName(element, introspectedTable); 177 | } 178 | 179 | @Override 180 | public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 181 | System.out.println("-----------------" + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + " replace parameter type to " + introspectedTable.getBaseRecordType() + " of SelectByPrimaryKey in sql xml"); 182 | return this.replaceParamType(element, introspectedTable); 183 | } 184 | 185 | @Override 186 | public boolean sqlMapDeleteByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { 187 | System.out.println("-----------------" + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + " replace parameter type to " + introspectedTable.getBaseRecordType() + " of DeleteByPrimaryKey in sql xml"); 188 | return this.replaceParamType(element, introspectedTable); 189 | } 190 | 191 | @Override 192 | public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { 193 | System.out.println("-----------------" + interfaze.getType().getShortName() + " replace parameter type to " + introspectedTable.getBaseRecordType() + " of SelectByPrimaryKey in client class' method " + method.getName()); 194 | return this.replaceParamType(method, introspectedTable); 195 | } 196 | 197 | @Override 198 | public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { 199 | System.out.println("-----------------" + interfaze.getType().getShortName() + " replace parameter type to " + introspectedTable.getBaseRecordType() + " of DeleteByPrimaryKey in client class' method " + method.getName()); 200 | return this.replaceParamType(method, introspectedTable); 201 | } 202 | 203 | private Method makePartitionMethod(FullyQualifiedJavaType paramType, FullyQualifiedJavaType returnType, String fieldName, String tableName, String[] exp) { 204 | String methodName = PARTITION_FACTOR + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); 205 | Method method = new Method(); 206 | method.setName(methodName); 207 | method.setVisibility(JavaVisibility.PUBLIC); 208 | method.setReturnType(returnType); 209 | method.addBodyLines(Arrays.asList(exp)); 210 | method.addParameter(new Parameter(paramType, fieldName)); 211 | PluginUtils.addDoc(this.getContext(), method, tableName); 212 | return method; 213 | } 214 | 215 | protected static boolean needPartition(IntrospectedTable introspectedTable) { 216 | String relColumn = introspectedTable.getTableConfigurationProperty(REL_COLUMN); 217 | return relColumn != null && !"".equals(relColumn); 218 | } 219 | 220 | private String convertColumnName(String column, TableConfiguration configuration) { 221 | String name = camelcase(column.split(UNDERLINE)); 222 | if (configuration.getColumnOverride(column) != null) { 223 | name = configuration.getColumnOverride(column).getJavaProperty(); 224 | } else if (configuration.getColumnRenamingRule() != null) { 225 | String search = configuration.getColumnRenamingRule().getSearchString(); 226 | String replace = configuration.getColumnRenamingRule().getReplaceString(); 227 | name = column.replaceAll(search, replace); 228 | } 229 | return name; 230 | } 231 | 232 | private static String camelcase(String[] words) { 233 | StringBuilder sb = new StringBuilder(words[0].toLowerCase()); 234 | for (int i = 1; i < words.length; i++) { 235 | sb.append(words[i].substring(0, 1).toUpperCase()); 236 | if (words[i].length() > 1) { 237 | sb.append(words[i].substring(1, words[i].length()).toLowerCase()); 238 | } 239 | } 240 | return sb.toString(); 241 | } 242 | 243 | private boolean replaceParamType(Method method, IntrospectedTable introspectedTable) { 244 | if (needPartition(introspectedTable)) { 245 | for (Parameter parameter : method.getParameters()) { 246 | try { 247 | String classType = introspectedTable.getBaseRecordType(); 248 | java.lang.reflect.Field field = parameter.getClass().getDeclaredField("name"); 249 | field.setAccessible(true); 250 | field.set(parameter, "record"); 251 | 252 | field = parameter.getClass().getDeclaredField("type"); 253 | field.setAccessible(true); 254 | field.set(parameter, new FullyQualifiedJavaType(classType)); 255 | } catch (NoSuchFieldException e) { 256 | System.err.println("replace parameter type error" + e); 257 | } catch (IllegalAccessException e) { 258 | System.err.println("replace parameter type error" + e); 259 | } 260 | } 261 | } 262 | return true; 263 | } 264 | 265 | private boolean replaceParamType(XmlElement element, IntrospectedTable introspectedTable) { 266 | if (needPartition(introspectedTable)) { 267 | for (Attribute attribute : element.getAttributes()) { 268 | if (SQL_MAP_PARAMETER_TYPE.equals(attribute.getName())) { 269 | try { 270 | String classType = introspectedTable.getBaseRecordType(); 271 | java.lang.reflect.Field field = attribute.getClass().getDeclaredField("value"); 272 | field.setAccessible(true); 273 | field.set(attribute, classType); 274 | } catch (NoSuchFieldException e) { 275 | System.err.println("replace parameter type error" + e); 276 | } catch (IllegalAccessException e) { 277 | System.err.println("replace parameter type error" + e); 278 | } 279 | } 280 | } 281 | } 282 | return true; 283 | } 284 | 285 | private boolean dynamicTableName(XmlElement element, IntrospectedTable introspectedTable) { 286 | if (needPartition(introspectedTable)) { 287 | TextElement sqlhead = (TextElement) element.getElements().get(0); 288 | try { 289 | String dynamicTableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime(); 290 | String baseName = dynamicTableName.substring(0, dynamicTableName.lastIndexOf(UNDERLINE)); 291 | 292 | String sfx = ""; 293 | sfx += "" + baseName + "_${record." + SUFFIX_FIELD + "}"; 294 | sfx += "" + baseName + "_${example." + SUFFIX_FIELD + "}"; 295 | sfx += "" + baseName + "_ "; 296 | sfx += ""; 297 | 298 | java.lang.reflect.Field field = sqlhead.getClass().getDeclaredField("content"); 299 | field.setAccessible(true); 300 | field.set(sqlhead, "update " + sfx); 301 | 302 | System.out.println("-----------------" + baseName + "generate dynamic table name base on {} in sql xml"); 303 | } catch (NoSuchFieldException e) { 304 | System.err.println("generate dynamic table name error" + e); 305 | } catch (IllegalAccessException e) { 306 | System.err.println("generate dynamic table name error" + e); 307 | } 308 | } 309 | return true; 310 | } 311 | 312 | private static boolean isPrime(long N) { 313 | if (N < 2) return false; 314 | for (int i = 2; i * i <= N; i++) { 315 | if (N % i == 0) return false; 316 | } 317 | return true; 318 | } 319 | 320 | private final static String REL_COLUMN = "sliceColumn"; 321 | private final static String MOD_VALUE = "sliceMod"; 322 | private final static String TIME_VALUE = "sliceMonth"; 323 | private final static String SUFFIX_FIELD = "tableNameSuffix"; 324 | 325 | private final static FullyQualifiedJavaType STRING_TYPE = new FullyQualifiedJavaType("java.lang.String"); 326 | private final static FullyQualifiedJavaType DATE_TYPE = new FullyQualifiedJavaType("java.util.Date"); 327 | private final static FullyQualifiedJavaType CALENDAR_TYPE = new FullyQualifiedJavaType("java.util.Calendar"); 328 | private final static FullyQualifiedJavaType BIGDECIMAL_TYPE = new FullyQualifiedJavaType("java.math.BigDecimal"); 329 | 330 | private final static String UNDERLINE = "_"; 331 | private final static String PARTITION_FACTOR = "partitionFactor"; 332 | private final static String SUFFIX = "_${" + SUFFIX_FIELD + "}"; 333 | 334 | private final static String SQL_MAP_PARAMETER_TYPE = "parameterType"; 335 | 336 | } 337 | -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/model/TableTestSliceMonthExample.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | public class TableTestSliceMonthExample { 9 | protected String orderByClause; 10 | 11 | protected boolean distinct; 12 | 13 | protected List oredCriteria; 14 | 15 | private String tableNameSuffix; 16 | 17 | private String sumCol; 18 | 19 | private Integer offset; 20 | 21 | private Integer limit; 22 | 23 | public TableTestSliceMonthExample() { 24 | oredCriteria = new ArrayList(); 25 | } 26 | 27 | public void setOrderByClause(String orderByClause) { 28 | this.orderByClause = orderByClause; 29 | } 30 | 31 | public String getOrderByClause() { 32 | return orderByClause; 33 | } 34 | 35 | public void setDistinct(boolean distinct) { 36 | this.distinct = distinct; 37 | } 38 | 39 | public boolean isDistinct() { 40 | return distinct; 41 | } 42 | 43 | public List getOredCriteria() { 44 | return oredCriteria; 45 | } 46 | 47 | public void or(Criteria criteria) { 48 | oredCriteria.add(criteria); 49 | } 50 | 51 | public Criteria or() { 52 | Criteria criteria = createCriteriaInternal(); 53 | oredCriteria.add(criteria); 54 | return criteria; 55 | } 56 | 57 | public Criteria createCriteria() { 58 | Criteria criteria = createCriteriaInternal(); 59 | if (oredCriteria.size() == 0) { 60 | oredCriteria.add(criteria); 61 | } 62 | return criteria; 63 | } 64 | 65 | protected Criteria createCriteriaInternal() { 66 | Criteria criteria = new Criteria(); 67 | return criteria; 68 | } 69 | 70 | public void clear() { 71 | oredCriteria.clear(); 72 | orderByClause = null; 73 | distinct = false; 74 | this.tableNameSuffix = null; 75 | this.sumCol = null; 76 | this.offset = null; 77 | this.limit = null; 78 | } 79 | 80 | public TableTestSliceMonthExample partitionFactorSliceMonthId(Date sliceMonthId) { 81 | if (sliceMonthId != null ) { 82 | Calendar calendar = Calendar.getInstance(); 83 | calendar.setTimeInMillis(sliceMonthId.getTime()); 84 | int m = calendar.get(Calendar.MONTH) + 1; 85 | this.tableNameSuffix = calendar.get(Calendar.YEAR) + (m < 10 ? "0" + m : "" + m); 86 | } 87 | return this; 88 | } 89 | 90 | public String getTableNameSuffix() { 91 | return this.tableNameSuffix; 92 | } 93 | 94 | public void setTableNameSuffix(String tableNameSuffix) { 95 | this.tableNameSuffix = tableNameSuffix; 96 | } 97 | 98 | public String getSumCol() { 99 | return this.sumCol; 100 | } 101 | 102 | public void setSumCol(String sumCol) { 103 | this.sumCol = sumCol; 104 | } 105 | 106 | public TableTestSliceMonthExample sumId() { 107 | this.sumCol="ID"; 108 | return this; 109 | } 110 | 111 | public TableTestSliceMonthExample sumSliceMonthId() { 112 | this.sumCol="SLICE_MONTH_ID"; 113 | return this; 114 | } 115 | 116 | public TableTestSliceMonthExample sumJacksonId1() { 117 | this.sumCol="JACKSON_ID1"; 118 | return this; 119 | } 120 | 121 | public TableTestSliceMonthExample sumVersion() { 122 | this.sumCol="VERSION"; 123 | return this; 124 | } 125 | 126 | public TableTestSliceMonthExample sumJacksonId2() { 127 | this.sumCol="JACKSON_ID2"; 128 | return this; 129 | } 130 | 131 | public TableTestSliceMonthExample sumJacksonTime() { 132 | this.sumCol="JACKSON_TIME"; 133 | return this; 134 | } 135 | 136 | public TableTestSliceMonthExample sumCouldSumCol() { 137 | this.sumCol="COULD_SUM_COL"; 138 | return this; 139 | } 140 | 141 | public Integer getOffset() { 142 | return this.offset; 143 | } 144 | 145 | public void setOffset(Integer offset) { 146 | this.offset = offset; 147 | } 148 | 149 | public Integer getLimit() { 150 | return this.limit; 151 | } 152 | 153 | public void setLimit(Integer limit) { 154 | this.limit = limit; 155 | } 156 | 157 | public TableTestSliceMonthExample page(int offset, int limit) { 158 | this.offset = offset; 159 | this.limit = limit; 160 | return this; 161 | } 162 | 163 | protected abstract static class GeneratedCriteria { 164 | protected List criteria; 165 | 166 | protected GeneratedCriteria() { 167 | super(); 168 | criteria = new ArrayList(); 169 | } 170 | 171 | public boolean isValid() { 172 | return criteria.size() > 0; 173 | } 174 | 175 | public List getAllCriteria() { 176 | return criteria; 177 | } 178 | 179 | public List getCriteria() { 180 | return criteria; 181 | } 182 | 183 | protected void addCriterion(String condition) { 184 | if (condition == null) { 185 | throw new RuntimeException("Value for condition cannot be null"); 186 | } 187 | criteria.add(new Criterion(condition)); 188 | } 189 | 190 | protected void addCriterion(String condition, Object value, String property) { 191 | if (value == null) { 192 | throw new RuntimeException("Value for " + property + " cannot be null"); 193 | } 194 | criteria.add(new Criterion(condition, value)); 195 | } 196 | 197 | protected void addCriterion(String condition, Object value1, Object value2, String property) { 198 | if (value1 == null || value2 == null) { 199 | throw new RuntimeException("Between values for " + property + " cannot be null"); 200 | } 201 | criteria.add(new Criterion(condition, value1, value2)); 202 | } 203 | 204 | public Criteria andIdIsNull() { 205 | addCriterion("ID is null"); 206 | return (Criteria) this; 207 | } 208 | 209 | public Criteria andIdIsNotNull() { 210 | addCriterion("ID is not null"); 211 | return (Criteria) this; 212 | } 213 | 214 | public Criteria andIdEqualTo(Long value) { 215 | addCriterion("ID =", value, "id"); 216 | return (Criteria) this; 217 | } 218 | 219 | public Criteria andIdNotEqualTo(Long value) { 220 | addCriterion("ID <>", value, "id"); 221 | return (Criteria) this; 222 | } 223 | 224 | public Criteria andIdGreaterThan(Long value) { 225 | addCriterion("ID >", value, "id"); 226 | return (Criteria) this; 227 | } 228 | 229 | public Criteria andIdGreaterThanOrEqualTo(Long value) { 230 | addCriterion("ID >=", value, "id"); 231 | return (Criteria) this; 232 | } 233 | 234 | public Criteria andIdLessThan(Long value) { 235 | addCriterion("ID <", value, "id"); 236 | return (Criteria) this; 237 | } 238 | 239 | public Criteria andIdLessThanOrEqualTo(Long value) { 240 | addCriterion("ID <=", value, "id"); 241 | return (Criteria) this; 242 | } 243 | 244 | public Criteria andIdIn(List values) { 245 | addCriterion("ID in", values, "id"); 246 | return (Criteria) this; 247 | } 248 | 249 | public Criteria andIdNotIn(List values) { 250 | addCriterion("ID not in", values, "id"); 251 | return (Criteria) this; 252 | } 253 | 254 | public Criteria andIdBetween(Long value1, Long value2) { 255 | addCriterion("ID between", value1, value2, "id"); 256 | return (Criteria) this; 257 | } 258 | 259 | public Criteria andIdNotBetween(Long value1, Long value2) { 260 | addCriterion("ID not between", value1, value2, "id"); 261 | return (Criteria) this; 262 | } 263 | 264 | public Criteria andSliceMonthIdIsNull() { 265 | addCriterion("SLICE_MONTH_ID is null"); 266 | return (Criteria) this; 267 | } 268 | 269 | public Criteria andSliceMonthIdIsNotNull() { 270 | addCriterion("SLICE_MONTH_ID is not null"); 271 | return (Criteria) this; 272 | } 273 | 274 | public Criteria andSliceMonthIdEqualTo(Date value) { 275 | addCriterion("SLICE_MONTH_ID =", value, "sliceMonthId"); 276 | return (Criteria) this; 277 | } 278 | 279 | public Criteria andSliceMonthIdNotEqualTo(Date value) { 280 | addCriterion("SLICE_MONTH_ID <>", value, "sliceMonthId"); 281 | return (Criteria) this; 282 | } 283 | 284 | public Criteria andSliceMonthIdGreaterThan(Date value) { 285 | addCriterion("SLICE_MONTH_ID >", value, "sliceMonthId"); 286 | return (Criteria) this; 287 | } 288 | 289 | public Criteria andSliceMonthIdGreaterThanOrEqualTo(Date value) { 290 | addCriterion("SLICE_MONTH_ID >=", value, "sliceMonthId"); 291 | return (Criteria) this; 292 | } 293 | 294 | public Criteria andSliceMonthIdLessThan(Date value) { 295 | addCriterion("SLICE_MONTH_ID <", value, "sliceMonthId"); 296 | return (Criteria) this; 297 | } 298 | 299 | public Criteria andSliceMonthIdLessThanOrEqualTo(Date value) { 300 | addCriterion("SLICE_MONTH_ID <=", value, "sliceMonthId"); 301 | return (Criteria) this; 302 | } 303 | 304 | public Criteria andSliceMonthIdIn(List values) { 305 | addCriterion("SLICE_MONTH_ID in", values, "sliceMonthId"); 306 | return (Criteria) this; 307 | } 308 | 309 | public Criteria andSliceMonthIdNotIn(List values) { 310 | addCriterion("SLICE_MONTH_ID not in", values, "sliceMonthId"); 311 | return (Criteria) this; 312 | } 313 | 314 | public Criteria andSliceMonthIdBetween(Date value1, Date value2) { 315 | addCriterion("SLICE_MONTH_ID between", value1, value2, "sliceMonthId"); 316 | return (Criteria) this; 317 | } 318 | 319 | public Criteria andSliceMonthIdNotBetween(Date value1, Date value2) { 320 | addCriterion("SLICE_MONTH_ID not between", value1, value2, "sliceMonthId"); 321 | return (Criteria) this; 322 | } 323 | 324 | public Criteria andJacksonId1IsNull() { 325 | addCriterion("JACKSON_ID1 is null"); 326 | return (Criteria) this; 327 | } 328 | 329 | public Criteria andJacksonId1IsNotNull() { 330 | addCriterion("JACKSON_ID1 is not null"); 331 | return (Criteria) this; 332 | } 333 | 334 | public Criteria andJacksonId1EqualTo(Long value) { 335 | addCriterion("JACKSON_ID1 =", value, "jacksonId1"); 336 | return (Criteria) this; 337 | } 338 | 339 | public Criteria andJacksonId1NotEqualTo(Long value) { 340 | addCriterion("JACKSON_ID1 <>", value, "jacksonId1"); 341 | return (Criteria) this; 342 | } 343 | 344 | public Criteria andJacksonId1GreaterThan(Long value) { 345 | addCriterion("JACKSON_ID1 >", value, "jacksonId1"); 346 | return (Criteria) this; 347 | } 348 | 349 | public Criteria andJacksonId1GreaterThanOrEqualTo(Long value) { 350 | addCriterion("JACKSON_ID1 >=", value, "jacksonId1"); 351 | return (Criteria) this; 352 | } 353 | 354 | public Criteria andJacksonId1LessThan(Long value) { 355 | addCriterion("JACKSON_ID1 <", value, "jacksonId1"); 356 | return (Criteria) this; 357 | } 358 | 359 | public Criteria andJacksonId1LessThanOrEqualTo(Long value) { 360 | addCriterion("JACKSON_ID1 <=", value, "jacksonId1"); 361 | return (Criteria) this; 362 | } 363 | 364 | public Criteria andJacksonId1In(List values) { 365 | addCriterion("JACKSON_ID1 in", values, "jacksonId1"); 366 | return (Criteria) this; 367 | } 368 | 369 | public Criteria andJacksonId1NotIn(List values) { 370 | addCriterion("JACKSON_ID1 not in", values, "jacksonId1"); 371 | return (Criteria) this; 372 | } 373 | 374 | public Criteria andJacksonId1Between(Long value1, Long value2) { 375 | addCriterion("JACKSON_ID1 between", value1, value2, "jacksonId1"); 376 | return (Criteria) this; 377 | } 378 | 379 | public Criteria andJacksonId1NotBetween(Long value1, Long value2) { 380 | addCriterion("JACKSON_ID1 not between", value1, value2, "jacksonId1"); 381 | return (Criteria) this; 382 | } 383 | 384 | public Criteria andVersionIsNull() { 385 | addCriterion("VERSION is null"); 386 | return (Criteria) this; 387 | } 388 | 389 | public Criteria andVersionIsNotNull() { 390 | addCriterion("VERSION is not null"); 391 | return (Criteria) this; 392 | } 393 | 394 | public Criteria andVersionEqualTo(Long value) { 395 | addCriterion("VERSION =", value, "version"); 396 | return (Criteria) this; 397 | } 398 | 399 | public Criteria andVersionNotEqualTo(Long value) { 400 | addCriterion("VERSION <>", value, "version"); 401 | return (Criteria) this; 402 | } 403 | 404 | public Criteria andVersionGreaterThan(Long value) { 405 | addCriterion("VERSION >", value, "version"); 406 | return (Criteria) this; 407 | } 408 | 409 | public Criteria andVersionGreaterThanOrEqualTo(Long value) { 410 | addCriterion("VERSION >=", value, "version"); 411 | return (Criteria) this; 412 | } 413 | 414 | public Criteria andVersionLessThan(Long value) { 415 | addCriterion("VERSION <", value, "version"); 416 | return (Criteria) this; 417 | } 418 | 419 | public Criteria andVersionLessThanOrEqualTo(Long value) { 420 | addCriterion("VERSION <=", value, "version"); 421 | return (Criteria) this; 422 | } 423 | 424 | public Criteria andVersionIn(List values) { 425 | addCriterion("VERSION in", values, "version"); 426 | return (Criteria) this; 427 | } 428 | 429 | public Criteria andVersionNotIn(List values) { 430 | addCriterion("VERSION not in", values, "version"); 431 | return (Criteria) this; 432 | } 433 | 434 | public Criteria andVersionBetween(Long value1, Long value2) { 435 | addCriterion("VERSION between", value1, value2, "version"); 436 | return (Criteria) this; 437 | } 438 | 439 | public Criteria andVersionNotBetween(Long value1, Long value2) { 440 | addCriterion("VERSION not between", value1, value2, "version"); 441 | return (Criteria) this; 442 | } 443 | 444 | public Criteria andJacksonId2IsNull() { 445 | addCriterion("JACKSON_ID2 is null"); 446 | return (Criteria) this; 447 | } 448 | 449 | public Criteria andJacksonId2IsNotNull() { 450 | addCriterion("JACKSON_ID2 is not null"); 451 | return (Criteria) this; 452 | } 453 | 454 | public Criteria andJacksonId2EqualTo(String value) { 455 | addCriterion("JACKSON_ID2 =", value, "jacksonId2"); 456 | return (Criteria) this; 457 | } 458 | 459 | public Criteria andJacksonId2NotEqualTo(String value) { 460 | addCriterion("JACKSON_ID2 <>", value, "jacksonId2"); 461 | return (Criteria) this; 462 | } 463 | 464 | public Criteria andJacksonId2GreaterThan(String value) { 465 | addCriterion("JACKSON_ID2 >", value, "jacksonId2"); 466 | return (Criteria) this; 467 | } 468 | 469 | public Criteria andJacksonId2GreaterThanOrEqualTo(String value) { 470 | addCriterion("JACKSON_ID2 >=", value, "jacksonId2"); 471 | return (Criteria) this; 472 | } 473 | 474 | public Criteria andJacksonId2LessThan(String value) { 475 | addCriterion("JACKSON_ID2 <", value, "jacksonId2"); 476 | return (Criteria) this; 477 | } 478 | 479 | public Criteria andJacksonId2LessThanOrEqualTo(String value) { 480 | addCriterion("JACKSON_ID2 <=", value, "jacksonId2"); 481 | return (Criteria) this; 482 | } 483 | 484 | public Criteria andJacksonId2Like(String value) { 485 | addCriterion("JACKSON_ID2 like", value, "jacksonId2"); 486 | return (Criteria) this; 487 | } 488 | 489 | public Criteria andJacksonId2NotLike(String value) { 490 | addCriterion("JACKSON_ID2 not like", value, "jacksonId2"); 491 | return (Criteria) this; 492 | } 493 | 494 | public Criteria andJacksonId2In(List values) { 495 | addCriterion("JACKSON_ID2 in", values, "jacksonId2"); 496 | return (Criteria) this; 497 | } 498 | 499 | public Criteria andJacksonId2NotIn(List values) { 500 | addCriterion("JACKSON_ID2 not in", values, "jacksonId2"); 501 | return (Criteria) this; 502 | } 503 | 504 | public Criteria andJacksonId2Between(String value1, String value2) { 505 | addCriterion("JACKSON_ID2 between", value1, value2, "jacksonId2"); 506 | return (Criteria) this; 507 | } 508 | 509 | public Criteria andJacksonId2NotBetween(String value1, String value2) { 510 | addCriterion("JACKSON_ID2 not between", value1, value2, "jacksonId2"); 511 | return (Criteria) this; 512 | } 513 | 514 | public Criteria andJacksonTimeIsNull() { 515 | addCriterion("JACKSON_TIME is null"); 516 | return (Criteria) this; 517 | } 518 | 519 | public Criteria andJacksonTimeIsNotNull() { 520 | addCriterion("JACKSON_TIME is not null"); 521 | return (Criteria) this; 522 | } 523 | 524 | public Criteria andJacksonTimeEqualTo(Date value) { 525 | addCriterion("JACKSON_TIME =", value, "jacksonTime"); 526 | return (Criteria) this; 527 | } 528 | 529 | public Criteria andJacksonTimeNotEqualTo(Date value) { 530 | addCriterion("JACKSON_TIME <>", value, "jacksonTime"); 531 | return (Criteria) this; 532 | } 533 | 534 | public Criteria andJacksonTimeGreaterThan(Date value) { 535 | addCriterion("JACKSON_TIME >", value, "jacksonTime"); 536 | return (Criteria) this; 537 | } 538 | 539 | public Criteria andJacksonTimeGreaterThanOrEqualTo(Date value) { 540 | addCriterion("JACKSON_TIME >=", value, "jacksonTime"); 541 | return (Criteria) this; 542 | } 543 | 544 | public Criteria andJacksonTimeLessThan(Date value) { 545 | addCriterion("JACKSON_TIME <", value, "jacksonTime"); 546 | return (Criteria) this; 547 | } 548 | 549 | public Criteria andJacksonTimeLessThanOrEqualTo(Date value) { 550 | addCriterion("JACKSON_TIME <=", value, "jacksonTime"); 551 | return (Criteria) this; 552 | } 553 | 554 | public Criteria andJacksonTimeIn(List values) { 555 | addCriterion("JACKSON_TIME in", values, "jacksonTime"); 556 | return (Criteria) this; 557 | } 558 | 559 | public Criteria andJacksonTimeNotIn(List values) { 560 | addCriterion("JACKSON_TIME not in", values, "jacksonTime"); 561 | return (Criteria) this; 562 | } 563 | 564 | public Criteria andJacksonTimeBetween(Date value1, Date value2) { 565 | addCriterion("JACKSON_TIME between", value1, value2, "jacksonTime"); 566 | return (Criteria) this; 567 | } 568 | 569 | public Criteria andJacksonTimeNotBetween(Date value1, Date value2) { 570 | addCriterion("JACKSON_TIME not between", value1, value2, "jacksonTime"); 571 | return (Criteria) this; 572 | } 573 | 574 | public Criteria andCouldSumColIsNull() { 575 | addCriterion("COULD_SUM_COL is null"); 576 | return (Criteria) this; 577 | } 578 | 579 | public Criteria andCouldSumColIsNotNull() { 580 | addCriterion("COULD_SUM_COL is not null"); 581 | return (Criteria) this; 582 | } 583 | 584 | public Criteria andCouldSumColEqualTo(Integer value) { 585 | addCriterion("COULD_SUM_COL =", value, "couldSumCol"); 586 | return (Criteria) this; 587 | } 588 | 589 | public Criteria andCouldSumColNotEqualTo(Integer value) { 590 | addCriterion("COULD_SUM_COL <>", value, "couldSumCol"); 591 | return (Criteria) this; 592 | } 593 | 594 | public Criteria andCouldSumColGreaterThan(Integer value) { 595 | addCriterion("COULD_SUM_COL >", value, "couldSumCol"); 596 | return (Criteria) this; 597 | } 598 | 599 | public Criteria andCouldSumColGreaterThanOrEqualTo(Integer value) { 600 | addCriterion("COULD_SUM_COL >=", value, "couldSumCol"); 601 | return (Criteria) this; 602 | } 603 | 604 | public Criteria andCouldSumColLessThan(Integer value) { 605 | addCriterion("COULD_SUM_COL <", value, "couldSumCol"); 606 | return (Criteria) this; 607 | } 608 | 609 | public Criteria andCouldSumColLessThanOrEqualTo(Integer value) { 610 | addCriterion("COULD_SUM_COL <=", value, "couldSumCol"); 611 | return (Criteria) this; 612 | } 613 | 614 | public Criteria andCouldSumColIn(List values) { 615 | addCriterion("COULD_SUM_COL in", values, "couldSumCol"); 616 | return (Criteria) this; 617 | } 618 | 619 | public Criteria andCouldSumColNotIn(List values) { 620 | addCriterion("COULD_SUM_COL not in", values, "couldSumCol"); 621 | return (Criteria) this; 622 | } 623 | 624 | public Criteria andCouldSumColBetween(Integer value1, Integer value2) { 625 | addCriterion("COULD_SUM_COL between", value1, value2, "couldSumCol"); 626 | return (Criteria) this; 627 | } 628 | 629 | public Criteria andCouldSumColNotBetween(Integer value1, Integer value2) { 630 | addCriterion("COULD_SUM_COL not between", value1, value2, "couldSumCol"); 631 | return (Criteria) this; 632 | } 633 | 634 | public Criteria addConditionSql(String conditionSql) { 635 | addCriterion(conditionSql); 636 | return (Criteria) this; 637 | } 638 | } 639 | 640 | public static class Criteria extends GeneratedCriteria { 641 | 642 | protected Criteria() { 643 | super(); 644 | } 645 | } 646 | 647 | public static class Criterion { 648 | private String condition; 649 | 650 | private Object value; 651 | 652 | private Object secondValue; 653 | 654 | private boolean noValue; 655 | 656 | private boolean singleValue; 657 | 658 | private boolean betweenValue; 659 | 660 | private boolean listValue; 661 | 662 | private String typeHandler; 663 | 664 | public String getCondition() { 665 | return condition; 666 | } 667 | 668 | public Object getValue() { 669 | return value; 670 | } 671 | 672 | public Object getSecondValue() { 673 | return secondValue; 674 | } 675 | 676 | public boolean isNoValue() { 677 | return noValue; 678 | } 679 | 680 | public boolean isSingleValue() { 681 | return singleValue; 682 | } 683 | 684 | public boolean isBetweenValue() { 685 | return betweenValue; 686 | } 687 | 688 | public boolean isListValue() { 689 | return listValue; 690 | } 691 | 692 | public String getTypeHandler() { 693 | return typeHandler; 694 | } 695 | 696 | protected Criterion(String condition) { 697 | super(); 698 | this.condition = condition; 699 | this.typeHandler = null; 700 | this.noValue = true; 701 | } 702 | 703 | protected Criterion(String condition, Object value, String typeHandler) { 704 | super(); 705 | this.condition = condition; 706 | this.value = value; 707 | this.typeHandler = typeHandler; 708 | if (value instanceof List) { 709 | this.listValue = true; 710 | } else { 711 | this.singleValue = true; 712 | } 713 | } 714 | 715 | protected Criterion(String condition, Object value) { 716 | this(condition, value, null); 717 | } 718 | 719 | protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { 720 | super(); 721 | this.condition = condition; 722 | this.value = value; 723 | this.secondValue = secondValue; 724 | this.typeHandler = typeHandler; 725 | this.betweenValue = true; 726 | } 727 | 728 | protected Criterion(String condition, Object value, Object secondValue) { 729 | this(condition, value, secondValue, null); 730 | } 731 | } 732 | } -------------------------------------------------------------------------------- /src/test/java/com/github/oceanc/mybatis3/generator/plugin/model/TableTestSliceModExample.java: -------------------------------------------------------------------------------- 1 | package com.github.oceanc.mybatis3.generator.plugin.model; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | public class TableTestSliceModExample { 9 | protected String orderByClause; 10 | 11 | protected boolean distinct; 12 | 13 | protected List oredCriteria; 14 | 15 | private String tableNameSuffix; 16 | 17 | private String sumCol; 18 | 19 | private Integer offset; 20 | 21 | private Integer limit; 22 | 23 | public TableTestSliceModExample() { 24 | oredCriteria = new ArrayList(); 25 | } 26 | 27 | public void setOrderByClause(String orderByClause) { 28 | this.orderByClause = orderByClause; 29 | } 30 | 31 | public String getOrderByClause() { 32 | return orderByClause; 33 | } 34 | 35 | public void setDistinct(boolean distinct) { 36 | this.distinct = distinct; 37 | } 38 | 39 | public boolean isDistinct() { 40 | return distinct; 41 | } 42 | 43 | public List getOredCriteria() { 44 | return oredCriteria; 45 | } 46 | 47 | public void or(Criteria criteria) { 48 | oredCriteria.add(criteria); 49 | } 50 | 51 | public Criteria or() { 52 | Criteria criteria = createCriteriaInternal(); 53 | oredCriteria.add(criteria); 54 | return criteria; 55 | } 56 | 57 | public Criteria createCriteria() { 58 | Criteria criteria = createCriteriaInternal(); 59 | if (oredCriteria.size() == 0) { 60 | oredCriteria.add(criteria); 61 | } 62 | return criteria; 63 | } 64 | 65 | protected Criteria createCriteriaInternal() { 66 | Criteria criteria = new Criteria(); 67 | return criteria; 68 | } 69 | 70 | public void clear() { 71 | oredCriteria.clear(); 72 | orderByClause = null; 73 | distinct = false; 74 | this.tableNameSuffix = null; 75 | this.sumCol = null; 76 | this.offset = null; 77 | this.limit = null; 78 | } 79 | 80 | public TableTestSliceModExample partitionFactorSliceModId(Long sliceModId) { 81 | if (sliceModId != null ) { 82 | long nan = 0; 83 | StringBuilder sb = new StringBuilder("0"); 84 | for (char c : String.valueOf(sliceModId).toCharArray()) { 85 | if (Character.isDigit(c)) sb.append(c); 86 | else nan += c; 87 | } 88 | long lid = new BigDecimal(sb.toString()).longValue(); 89 | if(nan > 0) lid += 83 + nan; 90 | this.tableNameSuffix = (Math.abs(lid) % 83) + ""; 91 | } 92 | return this; 93 | } 94 | 95 | public String getTableNameSuffix() { 96 | return this.tableNameSuffix; 97 | } 98 | 99 | public void setTableNameSuffix(String tableNameSuffix) { 100 | this.tableNameSuffix = tableNameSuffix; 101 | } 102 | 103 | public String getSumCol() { 104 | return this.sumCol; 105 | } 106 | 107 | public void setSumCol(String sumCol) { 108 | this.sumCol = sumCol; 109 | } 110 | 111 | public TableTestSliceModExample sumId() { 112 | this.sumCol="ID"; 113 | return this; 114 | } 115 | 116 | public TableTestSliceModExample sumSliceModId() { 117 | this.sumCol="SLICE_MOD_ID"; 118 | return this; 119 | } 120 | 121 | public TableTestSliceModExample sumJacksonId1() { 122 | this.sumCol="JACKSON_ID1"; 123 | return this; 124 | } 125 | 126 | public TableTestSliceModExample sumJacksonId2() { 127 | this.sumCol="JACKSON_ID2"; 128 | return this; 129 | } 130 | 131 | public TableTestSliceModExample sumJacksonTime() { 132 | this.sumCol="JACKSON_TIME"; 133 | return this; 134 | } 135 | 136 | public TableTestSliceModExample sumCouldSumCol() { 137 | this.sumCol="COULD_SUM_COL"; 138 | return this; 139 | } 140 | 141 | public TableTestSliceModExample sumVersion() { 142 | this.sumCol="VERSION"; 143 | return this; 144 | } 145 | 146 | public Integer getOffset() { 147 | return this.offset; 148 | } 149 | 150 | public void setOffset(Integer offset) { 151 | this.offset = offset; 152 | } 153 | 154 | public Integer getLimit() { 155 | return this.limit; 156 | } 157 | 158 | public void setLimit(Integer limit) { 159 | this.limit = limit; 160 | } 161 | 162 | public TableTestSliceModExample page(int offset, int limit) { 163 | this.offset = offset; 164 | this.limit = limit; 165 | return this; 166 | } 167 | 168 | protected abstract static class GeneratedCriteria { 169 | protected List criteria; 170 | 171 | protected GeneratedCriteria() { 172 | super(); 173 | criteria = new ArrayList(); 174 | } 175 | 176 | public boolean isValid() { 177 | return criteria.size() > 0; 178 | } 179 | 180 | public List getAllCriteria() { 181 | return criteria; 182 | } 183 | 184 | public List getCriteria() { 185 | return criteria; 186 | } 187 | 188 | protected void addCriterion(String condition) { 189 | if (condition == null) { 190 | throw new RuntimeException("Value for condition cannot be null"); 191 | } 192 | criteria.add(new Criterion(condition)); 193 | } 194 | 195 | protected void addCriterion(String condition, Object value, String property) { 196 | if (value == null) { 197 | throw new RuntimeException("Value for " + property + " cannot be null"); 198 | } 199 | criteria.add(new Criterion(condition, value)); 200 | } 201 | 202 | protected void addCriterion(String condition, Object value1, Object value2, String property) { 203 | if (value1 == null || value2 == null) { 204 | throw new RuntimeException("Between values for " + property + " cannot be null"); 205 | } 206 | criteria.add(new Criterion(condition, value1, value2)); 207 | } 208 | 209 | public Criteria andIdIsNull() { 210 | addCriterion("ID is null"); 211 | return (Criteria) this; 212 | } 213 | 214 | public Criteria andIdIsNotNull() { 215 | addCriterion("ID is not null"); 216 | return (Criteria) this; 217 | } 218 | 219 | public Criteria andIdEqualTo(Long value) { 220 | addCriterion("ID =", value, "id"); 221 | return (Criteria) this; 222 | } 223 | 224 | public Criteria andIdNotEqualTo(Long value) { 225 | addCriterion("ID <>", value, "id"); 226 | return (Criteria) this; 227 | } 228 | 229 | public Criteria andIdGreaterThan(Long value) { 230 | addCriterion("ID >", value, "id"); 231 | return (Criteria) this; 232 | } 233 | 234 | public Criteria andIdGreaterThanOrEqualTo(Long value) { 235 | addCriterion("ID >=", value, "id"); 236 | return (Criteria) this; 237 | } 238 | 239 | public Criteria andIdLessThan(Long value) { 240 | addCriterion("ID <", value, "id"); 241 | return (Criteria) this; 242 | } 243 | 244 | public Criteria andIdLessThanOrEqualTo(Long value) { 245 | addCriterion("ID <=", value, "id"); 246 | return (Criteria) this; 247 | } 248 | 249 | public Criteria andIdIn(List values) { 250 | addCriterion("ID in", values, "id"); 251 | return (Criteria) this; 252 | } 253 | 254 | public Criteria andIdNotIn(List values) { 255 | addCriterion("ID not in", values, "id"); 256 | return (Criteria) this; 257 | } 258 | 259 | public Criteria andIdBetween(Long value1, Long value2) { 260 | addCriterion("ID between", value1, value2, "id"); 261 | return (Criteria) this; 262 | } 263 | 264 | public Criteria andIdNotBetween(Long value1, Long value2) { 265 | addCriterion("ID not between", value1, value2, "id"); 266 | return (Criteria) this; 267 | } 268 | 269 | public Criteria andSliceModIdIsNull() { 270 | addCriterion("SLICE_MOD_ID is null"); 271 | return (Criteria) this; 272 | } 273 | 274 | public Criteria andSliceModIdIsNotNull() { 275 | addCriterion("SLICE_MOD_ID is not null"); 276 | return (Criteria) this; 277 | } 278 | 279 | public Criteria andSliceModIdEqualTo(Long value) { 280 | addCriterion("SLICE_MOD_ID =", value, "sliceModId"); 281 | return (Criteria) this; 282 | } 283 | 284 | public Criteria andSliceModIdNotEqualTo(Long value) { 285 | addCriterion("SLICE_MOD_ID <>", value, "sliceModId"); 286 | return (Criteria) this; 287 | } 288 | 289 | public Criteria andSliceModIdGreaterThan(Long value) { 290 | addCriterion("SLICE_MOD_ID >", value, "sliceModId"); 291 | return (Criteria) this; 292 | } 293 | 294 | public Criteria andSliceModIdGreaterThanOrEqualTo(Long value) { 295 | addCriterion("SLICE_MOD_ID >=", value, "sliceModId"); 296 | return (Criteria) this; 297 | } 298 | 299 | public Criteria andSliceModIdLessThan(Long value) { 300 | addCriterion("SLICE_MOD_ID <", value, "sliceModId"); 301 | return (Criteria) this; 302 | } 303 | 304 | public Criteria andSliceModIdLessThanOrEqualTo(Long value) { 305 | addCriterion("SLICE_MOD_ID <=", value, "sliceModId"); 306 | return (Criteria) this; 307 | } 308 | 309 | public Criteria andSliceModIdIn(List values) { 310 | addCriterion("SLICE_MOD_ID in", values, "sliceModId"); 311 | return (Criteria) this; 312 | } 313 | 314 | public Criteria andSliceModIdNotIn(List values) { 315 | addCriterion("SLICE_MOD_ID not in", values, "sliceModId"); 316 | return (Criteria) this; 317 | } 318 | 319 | public Criteria andSliceModIdBetween(Long value1, Long value2) { 320 | addCriterion("SLICE_MOD_ID between", value1, value2, "sliceModId"); 321 | return (Criteria) this; 322 | } 323 | 324 | public Criteria andSliceModIdNotBetween(Long value1, Long value2) { 325 | addCriterion("SLICE_MOD_ID not between", value1, value2, "sliceModId"); 326 | return (Criteria) this; 327 | } 328 | 329 | public Criteria andJacksonId1IsNull() { 330 | addCriterion("JACKSON_ID1 is null"); 331 | return (Criteria) this; 332 | } 333 | 334 | public Criteria andJacksonId1IsNotNull() { 335 | addCriterion("JACKSON_ID1 is not null"); 336 | return (Criteria) this; 337 | } 338 | 339 | public Criteria andJacksonId1EqualTo(Long value) { 340 | addCriterion("JACKSON_ID1 =", value, "jacksonId1"); 341 | return (Criteria) this; 342 | } 343 | 344 | public Criteria andJacksonId1NotEqualTo(Long value) { 345 | addCriterion("JACKSON_ID1 <>", value, "jacksonId1"); 346 | return (Criteria) this; 347 | } 348 | 349 | public Criteria andJacksonId1GreaterThan(Long value) { 350 | addCriterion("JACKSON_ID1 >", value, "jacksonId1"); 351 | return (Criteria) this; 352 | } 353 | 354 | public Criteria andJacksonId1GreaterThanOrEqualTo(Long value) { 355 | addCriterion("JACKSON_ID1 >=", value, "jacksonId1"); 356 | return (Criteria) this; 357 | } 358 | 359 | public Criteria andJacksonId1LessThan(Long value) { 360 | addCriterion("JACKSON_ID1 <", value, "jacksonId1"); 361 | return (Criteria) this; 362 | } 363 | 364 | public Criteria andJacksonId1LessThanOrEqualTo(Long value) { 365 | addCriterion("JACKSON_ID1 <=", value, "jacksonId1"); 366 | return (Criteria) this; 367 | } 368 | 369 | public Criteria andJacksonId1In(List values) { 370 | addCriterion("JACKSON_ID1 in", values, "jacksonId1"); 371 | return (Criteria) this; 372 | } 373 | 374 | public Criteria andJacksonId1NotIn(List values) { 375 | addCriterion("JACKSON_ID1 not in", values, "jacksonId1"); 376 | return (Criteria) this; 377 | } 378 | 379 | public Criteria andJacksonId1Between(Long value1, Long value2) { 380 | addCriterion("JACKSON_ID1 between", value1, value2, "jacksonId1"); 381 | return (Criteria) this; 382 | } 383 | 384 | public Criteria andJacksonId1NotBetween(Long value1, Long value2) { 385 | addCriterion("JACKSON_ID1 not between", value1, value2, "jacksonId1"); 386 | return (Criteria) this; 387 | } 388 | 389 | public Criteria andJacksonId2IsNull() { 390 | addCriterion("JACKSON_ID2 is null"); 391 | return (Criteria) this; 392 | } 393 | 394 | public Criteria andJacksonId2IsNotNull() { 395 | addCriterion("JACKSON_ID2 is not null"); 396 | return (Criteria) this; 397 | } 398 | 399 | public Criteria andJacksonId2EqualTo(String value) { 400 | addCriterion("JACKSON_ID2 =", value, "jacksonId2"); 401 | return (Criteria) this; 402 | } 403 | 404 | public Criteria andJacksonId2NotEqualTo(String value) { 405 | addCriterion("JACKSON_ID2 <>", value, "jacksonId2"); 406 | return (Criteria) this; 407 | } 408 | 409 | public Criteria andJacksonId2GreaterThan(String value) { 410 | addCriterion("JACKSON_ID2 >", value, "jacksonId2"); 411 | return (Criteria) this; 412 | } 413 | 414 | public Criteria andJacksonId2GreaterThanOrEqualTo(String value) { 415 | addCriterion("JACKSON_ID2 >=", value, "jacksonId2"); 416 | return (Criteria) this; 417 | } 418 | 419 | public Criteria andJacksonId2LessThan(String value) { 420 | addCriterion("JACKSON_ID2 <", value, "jacksonId2"); 421 | return (Criteria) this; 422 | } 423 | 424 | public Criteria andJacksonId2LessThanOrEqualTo(String value) { 425 | addCriterion("JACKSON_ID2 <=", value, "jacksonId2"); 426 | return (Criteria) this; 427 | } 428 | 429 | public Criteria andJacksonId2Like(String value) { 430 | addCriterion("JACKSON_ID2 like", value, "jacksonId2"); 431 | return (Criteria) this; 432 | } 433 | 434 | public Criteria andJacksonId2NotLike(String value) { 435 | addCriterion("JACKSON_ID2 not like", value, "jacksonId2"); 436 | return (Criteria) this; 437 | } 438 | 439 | public Criteria andJacksonId2In(List values) { 440 | addCriterion("JACKSON_ID2 in", values, "jacksonId2"); 441 | return (Criteria) this; 442 | } 443 | 444 | public Criteria andJacksonId2NotIn(List values) { 445 | addCriterion("JACKSON_ID2 not in", values, "jacksonId2"); 446 | return (Criteria) this; 447 | } 448 | 449 | public Criteria andJacksonId2Between(String value1, String value2) { 450 | addCriterion("JACKSON_ID2 between", value1, value2, "jacksonId2"); 451 | return (Criteria) this; 452 | } 453 | 454 | public Criteria andJacksonId2NotBetween(String value1, String value2) { 455 | addCriterion("JACKSON_ID2 not between", value1, value2, "jacksonId2"); 456 | return (Criteria) this; 457 | } 458 | 459 | public Criteria andJacksonTimeIsNull() { 460 | addCriterion("JACKSON_TIME is null"); 461 | return (Criteria) this; 462 | } 463 | 464 | public Criteria andJacksonTimeIsNotNull() { 465 | addCriterion("JACKSON_TIME is not null"); 466 | return (Criteria) this; 467 | } 468 | 469 | public Criteria andJacksonTimeEqualTo(Date value) { 470 | addCriterion("JACKSON_TIME =", value, "jacksonTime"); 471 | return (Criteria) this; 472 | } 473 | 474 | public Criteria andJacksonTimeNotEqualTo(Date value) { 475 | addCriterion("JACKSON_TIME <>", value, "jacksonTime"); 476 | return (Criteria) this; 477 | } 478 | 479 | public Criteria andJacksonTimeGreaterThan(Date value) { 480 | addCriterion("JACKSON_TIME >", value, "jacksonTime"); 481 | return (Criteria) this; 482 | } 483 | 484 | public Criteria andJacksonTimeGreaterThanOrEqualTo(Date value) { 485 | addCriterion("JACKSON_TIME >=", value, "jacksonTime"); 486 | return (Criteria) this; 487 | } 488 | 489 | public Criteria andJacksonTimeLessThan(Date value) { 490 | addCriterion("JACKSON_TIME <", value, "jacksonTime"); 491 | return (Criteria) this; 492 | } 493 | 494 | public Criteria andJacksonTimeLessThanOrEqualTo(Date value) { 495 | addCriterion("JACKSON_TIME <=", value, "jacksonTime"); 496 | return (Criteria) this; 497 | } 498 | 499 | public Criteria andJacksonTimeIn(List values) { 500 | addCriterion("JACKSON_TIME in", values, "jacksonTime"); 501 | return (Criteria) this; 502 | } 503 | 504 | public Criteria andJacksonTimeNotIn(List values) { 505 | addCriterion("JACKSON_TIME not in", values, "jacksonTime"); 506 | return (Criteria) this; 507 | } 508 | 509 | public Criteria andJacksonTimeBetween(Date value1, Date value2) { 510 | addCriterion("JACKSON_TIME between", value1, value2, "jacksonTime"); 511 | return (Criteria) this; 512 | } 513 | 514 | public Criteria andJacksonTimeNotBetween(Date value1, Date value2) { 515 | addCriterion("JACKSON_TIME not between", value1, value2, "jacksonTime"); 516 | return (Criteria) this; 517 | } 518 | 519 | public Criteria andCouldSumColIsNull() { 520 | addCriterion("COULD_SUM_COL is null"); 521 | return (Criteria) this; 522 | } 523 | 524 | public Criteria andCouldSumColIsNotNull() { 525 | addCriterion("COULD_SUM_COL is not null"); 526 | return (Criteria) this; 527 | } 528 | 529 | public Criteria andCouldSumColEqualTo(Integer value) { 530 | addCriterion("COULD_SUM_COL =", value, "couldSumCol"); 531 | return (Criteria) this; 532 | } 533 | 534 | public Criteria andCouldSumColNotEqualTo(Integer value) { 535 | addCriterion("COULD_SUM_COL <>", value, "couldSumCol"); 536 | return (Criteria) this; 537 | } 538 | 539 | public Criteria andCouldSumColGreaterThan(Integer value) { 540 | addCriterion("COULD_SUM_COL >", value, "couldSumCol"); 541 | return (Criteria) this; 542 | } 543 | 544 | public Criteria andCouldSumColGreaterThanOrEqualTo(Integer value) { 545 | addCriterion("COULD_SUM_COL >=", value, "couldSumCol"); 546 | return (Criteria) this; 547 | } 548 | 549 | public Criteria andCouldSumColLessThan(Integer value) { 550 | addCriterion("COULD_SUM_COL <", value, "couldSumCol"); 551 | return (Criteria) this; 552 | } 553 | 554 | public Criteria andCouldSumColLessThanOrEqualTo(Integer value) { 555 | addCriterion("COULD_SUM_COL <=", value, "couldSumCol"); 556 | return (Criteria) this; 557 | } 558 | 559 | public Criteria andCouldSumColIn(List values) { 560 | addCriterion("COULD_SUM_COL in", values, "couldSumCol"); 561 | return (Criteria) this; 562 | } 563 | 564 | public Criteria andCouldSumColNotIn(List values) { 565 | addCriterion("COULD_SUM_COL not in", values, "couldSumCol"); 566 | return (Criteria) this; 567 | } 568 | 569 | public Criteria andCouldSumColBetween(Integer value1, Integer value2) { 570 | addCriterion("COULD_SUM_COL between", value1, value2, "couldSumCol"); 571 | return (Criteria) this; 572 | } 573 | 574 | public Criteria andCouldSumColNotBetween(Integer value1, Integer value2) { 575 | addCriterion("COULD_SUM_COL not between", value1, value2, "couldSumCol"); 576 | return (Criteria) this; 577 | } 578 | 579 | public Criteria andVersionIsNull() { 580 | addCriterion("VERSION is null"); 581 | return (Criteria) this; 582 | } 583 | 584 | public Criteria andVersionIsNotNull() { 585 | addCriterion("VERSION is not null"); 586 | return (Criteria) this; 587 | } 588 | 589 | public Criteria andVersionEqualTo(Long value) { 590 | addCriterion("VERSION =", value, "version"); 591 | return (Criteria) this; 592 | } 593 | 594 | public Criteria andVersionNotEqualTo(Long value) { 595 | addCriterion("VERSION <>", value, "version"); 596 | return (Criteria) this; 597 | } 598 | 599 | public Criteria andVersionGreaterThan(Long value) { 600 | addCriterion("VERSION >", value, "version"); 601 | return (Criteria) this; 602 | } 603 | 604 | public Criteria andVersionGreaterThanOrEqualTo(Long value) { 605 | addCriterion("VERSION >=", value, "version"); 606 | return (Criteria) this; 607 | } 608 | 609 | public Criteria andVersionLessThan(Long value) { 610 | addCriterion("VERSION <", value, "version"); 611 | return (Criteria) this; 612 | } 613 | 614 | public Criteria andVersionLessThanOrEqualTo(Long value) { 615 | addCriterion("VERSION <=", value, "version"); 616 | return (Criteria) this; 617 | } 618 | 619 | public Criteria andVersionIn(List values) { 620 | addCriterion("VERSION in", values, "version"); 621 | return (Criteria) this; 622 | } 623 | 624 | public Criteria andVersionNotIn(List values) { 625 | addCriterion("VERSION not in", values, "version"); 626 | return (Criteria) this; 627 | } 628 | 629 | public Criteria andVersionBetween(Long value1, Long value2) { 630 | addCriterion("VERSION between", value1, value2, "version"); 631 | return (Criteria) this; 632 | } 633 | 634 | public Criteria andVersionNotBetween(Long value1, Long value2) { 635 | addCriterion("VERSION not between", value1, value2, "version"); 636 | return (Criteria) this; 637 | } 638 | 639 | public Criteria addConditionSql(String conditionSql) { 640 | addCriterion(conditionSql); 641 | return (Criteria) this; 642 | } 643 | } 644 | 645 | public static class Criteria extends GeneratedCriteria { 646 | 647 | protected Criteria() { 648 | super(); 649 | } 650 | } 651 | 652 | public static class Criterion { 653 | private String condition; 654 | 655 | private Object value; 656 | 657 | private Object secondValue; 658 | 659 | private boolean noValue; 660 | 661 | private boolean singleValue; 662 | 663 | private boolean betweenValue; 664 | 665 | private boolean listValue; 666 | 667 | private String typeHandler; 668 | 669 | public String getCondition() { 670 | return condition; 671 | } 672 | 673 | public Object getValue() { 674 | return value; 675 | } 676 | 677 | public Object getSecondValue() { 678 | return secondValue; 679 | } 680 | 681 | public boolean isNoValue() { 682 | return noValue; 683 | } 684 | 685 | public boolean isSingleValue() { 686 | return singleValue; 687 | } 688 | 689 | public boolean isBetweenValue() { 690 | return betweenValue; 691 | } 692 | 693 | public boolean isListValue() { 694 | return listValue; 695 | } 696 | 697 | public String getTypeHandler() { 698 | return typeHandler; 699 | } 700 | 701 | protected Criterion(String condition) { 702 | super(); 703 | this.condition = condition; 704 | this.typeHandler = null; 705 | this.noValue = true; 706 | } 707 | 708 | protected Criterion(String condition, Object value, String typeHandler) { 709 | super(); 710 | this.condition = condition; 711 | this.value = value; 712 | this.typeHandler = typeHandler; 713 | if (value instanceof List) { 714 | this.listValue = true; 715 | } else { 716 | this.singleValue = true; 717 | } 718 | } 719 | 720 | protected Criterion(String condition, Object value) { 721 | this(condition, value, null); 722 | } 723 | 724 | protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { 725 | super(); 726 | this.condition = condition; 727 | this.value = value; 728 | this.secondValue = secondValue; 729 | this.typeHandler = typeHandler; 730 | this.betweenValue = true; 731 | } 732 | 733 | protected Criterion(String condition, Object value, Object secondValue) { 734 | this(condition, value, secondValue, null); 735 | } 736 | } 737 | } --------------------------------------------------------------------------------