Case:
The database table
Person
has three columns: theid、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:
- By sql statement, using
as
aliases , 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
resultMap
Mapping 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.