https://mybatis.org/mybatis-3/zh/index.html
1, Mybatis official website definition
Mybatis configuration |
---|
1. Type aliases
- Type alias sets an abbreviated name for a Java type. It is only used for XML configuration and is intended to reduce redundant fully qualified class name writing. For example:
<typeAliases> <typeAlias alias="Author" type="domain.blog.Author"/> <typeAlias alias="Blog" type="domain.blog.Blog"/> <typeAlias alias="Comment" type="domain.blog.Comment"/> <typeAlias alias="Post" type="domain.blog.Post"/> <typeAlias alias="Section" type="domain.blog.Section"/> <typeAlias alias="Tag" type="domain.blog.Tag"/> </typeAliases>
-
When configured as above, blog can be used in any domain blog. Blog place.
-
You can also specify a package name. MyBatis will search for the required Java beans under the package name, such as:
<typeAliases> <package name="domain.blog"/> </typeAliases>
- Each one is in the package domain The Java Bean in the blog will use the initial lowercase unqualified class name of the bean as its alias without annotation. For example, domain blog. Alias of author is; If there is an annotation, the alias is its annotation value. See the following example:
@Alias("author") public class Author { ... }
- Here are some built-in type aliases for common Java types. They are case insensitive. Note that in order to deal with the naming repetition of the original type, a special naming style is adopted.
Common Java type aliases |
---|
2. Type handlers
-
When MyBatis sets the parameters in the PreparedStatement or takes a value from the result set, it will use the type processor to convert the obtained value into Java type in an appropriate way.
- When inserting data, convert the List set to a string
- When reading data, the string is automatically converted to List
-
You can override an existing type processor or create your own type processor to handle unsupported or nonstandard types. The specific approach is to realize org. Org apache. ibatis. type. Typehandler interface, or inherit a convenient class org apache. ibatis. type. Basetypehandler and optionally map it to a JDBC type. For example:
// ExampleTypeHandler.java @MappedJdbcTypes(JdbcType.VARCHAR) public class ExampleTypeHandler extends BaseTypeHandler<String> { @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { return rs.getString(columnName); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getString(columnIndex); } }
<!-- mybatis-config.xml --> <typeHandlers> <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/> </typeHandlers>
Using the above type processor will overwrite the existing type processor that handles the properties of Java String type and the parameters and results of VARCHAR type. Note that MyBatis will not determine which type to use by detecting the database meta information, so you must indicate that the field is of VARCHAR type in the parameter and result mapping so that it can be bound to the correct type processor. This is because MyBatis does not know the data type until the statement is executed.
Through the generics of the type processor, MyBatis can know the Java types handled by the type processor, but this behavior can be changed in two ways:
- Add a javaType attribute (for example, javaType = "String") on the configuration element (typeHandler element) of the type processor;
- Add a @ MappedTypes annotation on the class of the type processor to specify the list of Java types associated with it. If it is also specified in the javaType attribute, the configuration on the annotation will be ignored.
You can specify the associated JDBC type in two ways:
- Add a jdbcType attribute on the configuration element of the type processor (for example: jdbcType = "VARCHAR");
- Add a @ MappedJdbcTypes annotation on the class of the type processor to specify the list of JDBC types associated with it. If it is also specified in the JDBC type attribute, the configuration on the annotation is ignored.
When deciding which type of processor to use in the ResultMap, the Java type is known (obtained from the result type), but the JDBC type is unknown. Therefore, Mybatis uses a combination of javaType=[Java type] and JDBC type = null to select a type processor. This means that using the @ MappedJdbcTypes annotation limits the scope of the type processor and ensures that the type processor will not take effect in the ResultMap unless explicitly set. If you want to use the type processor implicitly in the ResultMap, you can set the includeNullJdbcType=true of the @ MappedJdbcTypes annotation. However, starting from Mybatis 3.4.0, if a Java type has only one registered type processor, even if includeNullJdbcType=true is not set, this type processor will also be the default processor when ResultMap uses Java types.
Finally, you can ask MyBatis to help you find the type processor:
<!-- mybatis-config.xml --> <typeHandlers> <package name="org.mybatis.example"/> </typeHandlers>
Note that when using the auto discovery function, the JDBC type can only be specified by annotation.
You can create generic type processors that can handle multiple classes. In order to use a generic type processor, you need to add a constructor that accepts the class of the class as a parameter, so MyBatis will pass in a specific class when constructing a type processor instance.
//GenericTypeHandler.java public class GenericTypeHandler<E extends MyObject> extends BaseTypeHandler<E> { private Class<E> type; public GenericTypeHandler(Class<E> type) { if (type == null) throw new IllegalArgumentException("Type argument cannot be null"); this.type = type; } ...
EnumTypeHandler and EnumOrdinalTypeHandler are both generic type processors, which we will discuss in detail in the next section.
3. mappers
Configure xxxmapper XML path
First, we need to tell MyBatis where to find these statements. Java does not provide a good solution for automatically finding resources, so the best way is to directly tell MyBatis where to find the mapping file. You can use resource references relative to the classpath, or fully qualified resource locators (including URL s in the form of file: / / / or class and package names). For example:
It is recommended to specify the package path, and then in POM Add scanning rules to XML to map (automatically find) resources
<!-- Register all the mapper interface implementations in the package as mappers --> <mappers> <package name="org.mybatis.builder"/> </mappers>
<build> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>