View Javadoc
1   /*
2    *    Copyright 2006-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.plugins;
17  
18  import java.util.List;
19  
20  import org.mybatis.generator.api.IntrospectedColumn;
21  import org.mybatis.generator.api.IntrospectedTable;
22  import org.mybatis.generator.api.PluginAdapter;
23  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24  import org.mybatis.generator.api.dom.java.InnerClass;
25  import org.mybatis.generator.api.dom.java.JavaVisibility;
26  import org.mybatis.generator.api.dom.java.Method;
27  import org.mybatis.generator.api.dom.java.Parameter;
28  import org.mybatis.generator.api.dom.java.TopLevelClass;
29  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
30  
31  /**
32   * This plugin demonstrates adding methods to the example class to enable
33   * case-insensitive LIKE searches. It shows how to construct new methods and
34   * add them to an existing class.
35   *
36   * <p>This plugin only adds methods for String fields mapped to a JDBC character
37   * type (CHAR, VARCHAR, etc.)
38   *
39   * @author Jeff Butler
40   *
41   */
42  public class CaseInsensitiveLikePlugin extends PluginAdapter {
43  
44      public CaseInsensitiveLikePlugin() {
45          super();
46      }
47  
48      @Override
49      public boolean validate(List<String> warnings) {
50          return true;
51      }
52  
53      @Override
54      public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
55              IntrospectedTable introspectedTable) {
56  
57          topLevelClass.getInnerClasses().stream()
58          .filter(this::isGeneratedCriteria)
59          .findFirst()
60          .ifPresent(c -> addMethods(introspectedTable, c));
61  
62          return true;
63      }
64  
65      private boolean isGeneratedCriteria(InnerClass innerClass) {
66          return "GeneratedCriteria".equals(innerClass.getType().getShortName()); //$NON-NLS-1$
67      }
68  
69      private void addMethods(IntrospectedTable introspectedTable, InnerClass criteria) {
70          introspectedTable.getNonBLOBColumns().stream()
71          .filter(this::isEligibleColumn)
72          .map(this::toMethod)
73          .forEach(criteria::addMethod);
74      }
75  
76      private boolean isEligibleColumn(IntrospectedColumn introspectedColumn) {
77          return introspectedColumn.isJdbcCharacterColumn()
78                  && introspectedColumn.isStringColumn();
79      }
80  
81      private Method toMethod(IntrospectedColumn introspectedColumn) {
82          StringBuilder sb = new StringBuilder();
83          sb.append(introspectedColumn.getJavaProperty());
84          sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
85          sb.insert(0, "and"); //$NON-NLS-1$
86          sb.append("LikeInsensitive"); //$NON-NLS-1$
87          Method method = new Method(sb.toString());
88          method.setVisibility(JavaVisibility.PUBLIC);
89          method.addParameter(new Parameter(introspectedColumn
90                  .getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$
91  
92          method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
93  
94          sb.setLength(0);
95          sb.append("addCriterion(\"upper("); //$NON-NLS-1$
96          sb.append(MyBatis3FormattingUtilities
97                  .getAliasedActualColumnName(introspectedColumn));
98          sb.append(") like\", value.toUpperCase(), \""); //$NON-NLS-1$
99          sb.append(introspectedColumn.getJavaProperty());
100         sb.append("\");"); //$NON-NLS-1$
101         method.addBodyLine(sb.toString());
102         method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
103         return method;
104     }
105 }