Details of MyBatis input mapping and output mapping examples

  • 2020-06-07 04:26:35
  • OfStack

What is MyBatis?

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual parameter setting and result set fetching. MyBatis can use simple XML or annotations for configuration and native Map to map interfaces and Java's POJOs(Plain Old Java Objects, plain Java objects) to records in the database.

As we know, ES21en.xml is where we configure the sql statement that operates on the database. Each sql statement corresponds to a method, and each method has its own input/output parameter type. So how are these types configured? Today we will begin our study.

The input mapping

The first is input types, which can be divided into the following types: 1. Basic types. pojo object type. 3.pojo wraps objects. The so-called wrapped object can be understood as an object containing another object.

A simple type

Simple input type, relatively simple. When passing in a parameter, configure the parameter type directly into parameterType, as follows:


 <select id="findUserById" parameterType="int" resultType="cn.itcast.pojo.User">
 SELECT * FROM user where id=#{id}
 </select>

The contents of the placeholder or splice in the sql statement can be written at will and have no practical significance. But the advice is better known by name. Make it easy to read.

pojo object

If you pass in 1 object, the situation is slightly different. In addition to passing in the type needs to be the type of the object (you can write the full path to the class directly or configure the alias of the class by configuring the alias in sqlConfig.xml). It is also important to note that the placeholder for the sql statement also corresponds to the property value of our object. The code is as follows:


<select id="findUserByUserName" parameterType="cn.itcast.pojo.User" resultType="cn.itcast.pojo.User">
 select * from user where username like'%${username}%'
</select>

Here username must correspond to an attribute in the User class. That is, there must be an username attribute in the User class. And we need to assign username to the user object when passing in parameters. Used by the mybatis framework to parse.

Methods for configuring aliases in ES60en.xml:


<!--  Custom alias  -->
 <typeAliases>
 <!--  For a single pojo names : type:pojo The full path name of , alias: The alias  -->
<!-- <typeAlias type="cn.itcast.pojo.User" alias="user"/> -->
 <!--  Use package scanning to place the entire package under the pojo names  
   An alias is a class name ,  Case insensitive ,  Suggestions in accordance with the java I'm going to write the naming convention 
 -->
 <package name="cn.itcast.pojo"/>
 </typeAliases>

pojo wraps objects

What if the passed object is an object that contains another object, that is, a wrapper object? Change the value in the placeholder or connecter to an object.

The first is the code of the pojo object. We can see that the entire user object is taken as one attribute value of the queryVo object, and its field name is user:


 package cn.itcast.pojo;
public class QueryVo {
 private User user;
 public User getUser() {
 return user;
 }
 public void setUser(User user) {
 this.user = user;
 }
}

Then we have our mapping file, where in ${user.userneme}, user is named 1 with the user attribute in our QueryVo class, and username corresponds to the username field in the User class:


<select id="findUserByVo" parameterType="cn.itcast.pojo.QueryVo" resultType="cn.itcast.pojo.User">
 select * from user where username like'%${user.username}%' and sex =#{user.sex}
</select>

The output mapping

The configuration of the output map and the input map are based on 1. The first is simple types. There is only one case in mybatis that returns a simple type, and that is when our sql statement returns only one value. You can return a simple type.

And then there's our pojo type. Our mybatis can wrap the returned data into an pojo object, but the object property of pojo must correspond to the field name 1 of our return value. So you can encapsulate it. If no 1 still wants to encapsulate, then you need to specify its correspondence. The specific code and comments are as follows:


<!--  Here, userListResultMap The corresponding is resultMap The only 1 identifier  -->
 <select id="findUserByVo" parameterType="cn.itcast.pojo.QueryVo" resultMap="userListResultMap">
 select * from user where username like'%${user.username}%' and sex =#{user.sex}
 </select>
 <!--  Here, cn.itcast.pojo.User Represents the User The class is the type of object that is ultimately generated; userListResultMap Is this resultMap The only 1 identifier  -->
 <resultMap type="cn.itcast.pojo.User" id="userListResultMap">
 <!--  Here, id_ The data field name retrieved from the database is the primary key; while id It is our User Properties in an object  -->
 <id column="id_" property="id"/>
 <!--  Here, result Refers to other fields  -->
 <result column="userneme_" property="username"/>
 </resultMap>

Related articles: