数据库的字段名和类的成员变量名不一致的解决办法,sql语句的as,resultMap映射

it2026-09-02  1

案例:

数据库表Person,有三个 列: id、name、city 类Person有三个成员变量:pid、pname、pcity

当数据库的字段名和我们的类的成员变量名不一致,主要通过两种方法实现映射:

通过sql语句,使用 as 起别名,就是给数据库的列名起一个别名,别名与类的成员变量名一致。 select id as pid, name as pname, money as pmoney from person

注意最后一个查询语句不需要逗号

通过resultMap标签进行数据库列与类的成员变量的映射 <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>

resultMap标签进行数据库与实体类的映射 标签id,对应数据库表的主键,,标签result,对应数据库非主键的其他列 属性column对应数据库表的列名,属性property对应实体类

最新回复(0)