Mybatis [Result-solve the problem of inconsistency between attribute names and field names]

5. Result- Solve the problem of inconsistency between attribute names and field names

1. Problem

database fields

Create a new project and test the inconsistency of the entity class fields before copying

There was a problem with the test

Solution:

alias

  <select id="getUserById" parameterType="int" resultType="com.jiang.pojo.User">
        select id,name,pwd as password  from mybatis.user where id= #{id}
    </select>

2、resultMap

result set mapping

id name pwd

id name password
<!--结果集映射-->
<resultMap id="UserMap" type="User">
    <!--column数据库中的字段,property实体类中的属性-->
    <result column="id" property="id"></result>
    <result column="name" property="name"></result>
    <result column="pwd" property="password"></result>
</resultMap>

<select id="getUserList" resultMap="UserMap">
    select * from USER
</select>

The resultMap element is the most important and powerful element in MyBatis.

The design idea of ​​ResultMap is to achieve zero configuration for simple statements, and for more complex statements, only need to describe the relationship between statements.

The great thing about ResultMaps - you don't have to explicitly configure them at all.

If only the world had always been this simple.



https://www.bilibili.com/video/BV1NE411Q7Nx?p=11&spm_id_from=pageDriver

Guess you like

Origin blog.csdn.net/qq_48108092/article/details/124154152