General multi-table query, update operation and upload jar to maven central warehouse based on tk.mybatis:mapper (2)

1. Introduction to version 2.0

    Hello! Ladies and gentlemen, I'm back! Next, let's talk about the 2.0 version of our general multi-table query and update.

    In version 2.0, I extracted the entire general part to form a new project, in order to make it general.

2. Project dependencies

    First, let's take a look at the dependencies of the extracted 2.0 project:

The above are the dependencies required by the 2.0 project, where mapper.version is the version of tk.mybatis:mapper, which is also the most important dependency in this project. As mentioned above, XML should be redundant, and the removal of xml files in this version requires the help of mapper.

3. Key explanation

       Changes in SelectBaseMapper and UpdateBaseMapper.

@RegisterMapper
public interface SelectBaseMapper<T> {
	
	@SelectProvider(type=MySelectProvider.class,method = "dynamicSQL")
	List<T> selectPage(BaseExample baseExample);
}

The main change of SelectBaseMapper here is that it can directly return the object.

@RegisterMapper
public interface UpdateBaseMapper {
	
	@UpdateProvider(type=MyUpdateProvider.class,method="dynamicSQL")
	public int updateObject( UpdateBaseExample baseExample);
}

We used 3 annotations in the above two classes, namely @RegisterMapper, @SelectProvider, @UpdateProvider.

@RegisterMapper:

    This annotation is provided by tk.mybatis:mapper, and its main function is to register this Mapper interface with the general Mapper. There is a requirement when using this annotation that it cannot be scanned by mybatis when scanning the Mapper interface , and we meet this as a separate project.

@SelectProvider

    This annotation is provided to us by mybatis, and its main function is to indicate the content provider of the annotated method. And method means the method when the provider is executed. Here our method is dynamicSQL. This method is a method provided by MapperTemplate for us. This method is only used to initialize ProviderSqlSource ( and the role of ProviderSqlSource is to execute the method modified by the annotation in the type corresponding to the type in the @SelectProvider annotation. Take the SelectBaseMapper above as an example, which is to execute the selectPage method in the MySelectProvider class. Only methods are required here. same name ).

@UpdateProvider

    This annotation is provided by mybatis for us, and its main function is the same as @SelectProvider .

Fourth, how does XML disappear?

   This is the focus of this chapter. In fact, XML does not disappear, but instead of reading from the xml file, it becomes dynamically generated. The principle of search and update is the same here, and I mainly take search as an example. The dynamically generated place is the corresponding annotated method in the type of type in the @SelectProvider we annotated. That is, the selectPage method in MySelectProvider.
public class MySelectProvider extends MapperTemplate{
	//This construction method is required by the parent class MapperTemplate
	public MySelectProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
		super (mapperClass, mapperHelper);
	}
    //Incoming MappedStatement, MappedStatement is equivalent to the encapsulation of the entire <select></select> statement in our XML, including information such as incoming type and return type
 public String selectPage(MappedStatement ms)  
      {  
    	 Class<?> entityClass = getEntityClass(ms);
         //Modify the return value type to the entity type
         setResultType(ms, entityClass);
         StringBuilder sql = new StringBuilder("SELECT ");
         sql.append(MyExampleSqlHelp.getColumn());// Add the search field to sql
         sql.append(MyExampleSqlHelp.isUseAlias()); //Whether to use an alias, if used, add it to sql
         sql.append(MyExampleSqlHelp.isUseLeftJoin()); //Whether the left join query is used, if used, add it to sql
         sql.append(MyExampleSqlHelp.useWhereAndEqualsWhere()); //Whether the greater than, less than, not equal, equals query is used, and whether the equals query is used, and if so, add it to sql
         sql.append(MyExampleSqlHelp.useGreaterThan());//Whether the greater than query is used, if it is used, add it to sql
         sql.append(MyExampleSqlHelp.useLessThan());//Whether the less than query is used, if used, add it to sql
         sql.append(MyExampleSqlHelp.useNotEquals());//Whether the not equal query is used, if it is used, add it to sql
         sql.append(MyExampleSqlHelp.notUseWhere());//Determine whether the query is used again
         sql.append(MyExampleSqlHelp.useIn());//Whether the in query is used, if used, add it to sql
         sql.append(MyExampleSqlHelp.useLike());//Whether the like query is used, if used, add it to sql
         sql.append(MyExampleSqlHelp.useGroup());//Whether the grouping function is used, if so, add it to sql
         sql.append(MyExampleSqlHelp.useOrder());//Whether the sorting function is used, if so, add it to sql
        return sql.toString();  
      }  
}  

Here we have customized a MyExampleSqlHelp class to manage sql in segments and prevent confusion.

/**
	 * Whether to use not equals query
	 * @return
	 */
	public static String useNotEquals() {
		return "<if test =\"@com.example.test.util.ExampleOGNL@useNotEqualsWhere(_parameter)\">\r\n" +
				"					<foreach collection=\"notEqualsWhereKey\" item=\"key\" index=\"index\" separator=\"AND\"> " +
				"						${key} != #{notEqualsWhereValue[${index}]}  " +
				"					</foreach>\r\n" +
				"				</if>\r\n" +
				"			</where>\r\n" +
				"		</if>";
	}

Looking at the useNotEquals() method of the MyExampleSqlHelp class, we can find that the statement here is very similar to that in xml, but we changed our direct judgment in xml to @com.example.test.util.ExampleOGNL@useNotEqualsWhere(_parameter ) Form, this is the form of the OGNL expression in MyBatis, which means that when the judgment condition in the test is executed, it is executed in the useNotEqualsWhere(_parameter) method of the com.example.test.util.ExampleOGNL class.

/**
	 * Whether to use not equals query
	 * @param parameter query parameter
	 */
	public static boolean useNotEqualsWhere(Object parameter) {
		if(parameter != null) {
			BaseExample sbe = (BaseExample)parameter;
			List<String> notEqualsWhere = sbe.getNotEqualsWhereKey();
			if(notEqualsWhere != null && notEqualsWhere.size() >0) {
				return true;
			}
		}
		return false;
	}

A very important point here is that the return value of the method must be of the type expected by its caller. For example, if we execute the test of the <if> tag above, then our return value must be of boolean or Boolean type.

The logic of this method is also very simple, that is, to determine whether the notEqualsWhere attribute exists in the incoming parameter, and whether there is a value.

After the above explanation, we have basically completed the realization of the condition of xml disappearance. At the same time, now our entire project is more concise, and can be used directly as a jar package.

The best thing is that it can be downloaded directly from the maven central repository after it is packaged into a jar package. Well, in the next article I will explain how to upload the jar package to the central repository. Hope you guys like it.

Finally, attach the project github address: https://github.com/satant/pj.mybatis

Maven downloads dependencies. If you want to use it, the version must be above 2.2.0, and you only need to inherit the BaseExampleMapper interface.

<!-- https://mvnrepository.com/artifact/com.github.satant/pj.mybatis -->
        <dependency>
            <groupId>com.github.satant</groupId>
            <artifactId>pj.mybatis</artifactId>
            <version>2.2.0</version>
        </dependency>





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403033&siteId=291194637