publicinterfaceUserDao{/**
* 获取单个用户
* @param id
* @return
*/public User getAllUser(int id);}
4. 定义操作user表的sql映射文件
<!--配置映射UserMapper文件--><!--namespace:UserDao接口的全路径--><mappernamespace="com.hong.dao.UserDao"><selectid="getAllUser"resultType="com.hong.entity.User">
select * from user where id=${id}
</select></mapper>
<!--配置映射UserMapper文件--><!--namespace:UserDao接口的全路径--><mappernamespace="com.hong.dao.ClassOneDao"><!--resultMap:创建属性与字段的关系;type:java实体类的类型--><resultMapid="ClassMap"type="ClassOne"><!--id:表示主键;column:表示字段;property:实体类的类型--><idcolumn="c_id"property="cid"></id><!--result:表示其他字段--><resultcolumn="c_name"property="cname"></result><!--association:多对一的映射;property:实体类中的字段名;javaType:java实体类中的路径 --><associationproperty="teacher"javaType="Teacher"><idcolumn="t_id"property="tid"></id><resultcolumn="t_name"property="tname"></result></association><!--collection:聚集查询;ofType:集合泛型--><collectionproperty="students"ofType="Student"><idcolumn="s_id"property="sid"></id><resultcolumn="s_name"property="sname"></result></collection></resultMap><selectid="getClassOne"resultMap="ClassMap">
select * from class c,teacher t,student s
where c.c_id=s.class_id
and c.teacher_id=t.t_id
and c.c_id=${cid}
</select></mapper>
2. 实体的属性和数据库表的字段不一致
(1)为查询的结果起别名(与属性名一致)
<!--为查询的结果起别名--><select id="findOrderById" resultType="com.hong.entity.0rder">select order_id as orderId , order_no orderNo , order_price orderPrice from orders where order_id=#{oid}</select>