5.Mybatis-- resolve inconsistencies attribute names and field names (aliases + resultMap)

We look at the fields in the database

 

 

 

Create a new project (before we copy) field test is inconsistent with the database entity class

 

 

 

 

 Take a look, the entity class attribute is not with the name of the database table is not the same

Well, we inquire about:

The problem: null

Why is empty?

Because the type of processor, the name of the same attribute name field when he was automatically remapped, and now is not the same, he can not find it.

Solution 1: aliases (a little too simple)

 

 

 Solution 2: resultMap : the result set mapping

 

 

 code show as below:

<select id="getUserById" resultMap="hi">
    select * from mybatis.user where id = #{id}
</select>
<ResultMap id = "hi" type = "User"> <-! I return type is User Type ->
    <result column="id" property="id"></result>
    <result column="name" property="name"></result>
    <result column="pwd" property="passsword"></result>
</resultMap>
View Code

 

Wan Qian good read! ! !

 

 

 

Guess you like

Origin www.cnblogs.com/tuyaojiao/p/12347047.html