Some misunderstandings of resultmap and resulttype

The two return value types resultmap and resulttype in the mapping configuration file of mybatis;

To test the code directly:

<select id="getUser" parameterType="string" resultType="pojo.User">
	select id,username,userpwd from t_users where id=#{id}
</select>

That's right, resulttype here is the full class name of the class, so it executes without any problems;


The result is what we want.

Next we define a <resultMap>:

<resultMap id="user" type="pojo.User" >  
    <id column="id" property="id"  />  
    <result column="username" property="username" />  
    <result column="userpwd" property="userpwd"  />
  </resultMap>

Then we modify the above configuration:

<select id="getUser" parameterType="string" resultMap="user">
	select id,username,userpwd from t_users where id=#{id}
</select>

We changed the resulttype to resultmap and then took the id in <resultMap>; the running result is also normal; it is the same as the one printed above;

Let's take a look at the differences between them:


When you see this kind of error, it means that the resulttype used is assigned to the id in <resultMap>;

  <select id="getUser" parameterType="string" resultType="user" >
		select id,username,userpwd from t_users where id=#{id}
	</select>
How to change the above configuration to work? That is to use an alias: add in mybatis-config.xml
<typeAliases>
	<typeAlias alias="user" type="pojo.User"/>
</typeAliases>

The alias here is the value of resulttype;

The above are just the parts that are easy to not notice when we write;

Note: the type returned by mybatis: it must be the map type, that is, the data is returned in the form of key-value pairs; but when we use resulttype, the values ​​in the map will be taken out and assigned to the properties of the object;

Guess you like

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