1. 使用别名查询
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.itheima.domain.User">
select id as userId, username as userName, birthday as userBirthday, sex as userSex, address as userAddress from user;
</select>
注意:mysql在windows系统中不区分大小写!
2. 使用resultMap
resultMap 标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。
在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。
<!--
建立User实体和数据库表的对应关系
type属性:指定实体类的全限定类名
id属性:给定一个唯一标识,是给查询 select 标签引用用的。
id标签:用于指定主键字段
result标签:用于指定非主键字段
property属性:用于指定实体类属性名称
column属性:用于指定数据库列名
-->
<resultMap type="com.itheima.domain.User" id="userMap">
<id property="userId" column="id" />
<result property="userName" column="username" />
<result property="userSex" column="sex" />
<result property="userAddress" column="address" />
<result property="userBirthday" column="birthday" />
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>
浙公网安备 33010602011771号