The solution to the inconsistency between the field name of the database and the member variable name of the class, the as, resultMap mapping of the sql statement

Case:

The database table Personhas three columns: the id、name、city
class Person has three member variables:pid、pname、pcity

When the database field name is inconsistent with the member variable name of our class, there are two main ways to achieve the mapping:

  1. By sql statement, using asaliases , it is to give the column names from a database alias, the same alias name member variables and class.
	select 
		id as pid,
		name as pname,
		money as pmoney
	from person

Note that the last query statement does not require a comma

  1. resultMapMapping database columns and class member variables through labels
	<resultMap id="personMap" type="person">
         <id column="id" property="pid"/>
         <result  column="name" property="pname"/>
         <result  column="city" property="pcity"/>
    </resultMap>
    <select id="findAll" resultMap="personMap">
        select
            id,name,city
        from ss_company
    </select>

The resultMap tag maps the database and the entity class.
Tag id, corresponds to the primary key of the database table, and the tag result, corresponds to other columns of the database that are not primary keys. The
attribute column corresponds to the column name of the database table, and the attribute property corresponds to the entity class.

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/109229652