- 当数据库表中的字段和实体类的属性名不一致时,如何获取值。通过resultMap可以进行相关的映射。
Vo类:
映射文件:
<!-- 根据id查询 --> <select id="findById" parameterType="int" resultMap="UserMap"> select * from t_user where id = #{id} </select> <resultMap type="cn.wh.vo.User" id="UserMap"> <!-- 主键的映射 --> <id column="id" property="id"/> <!-- 其他属性映射 --> <result column="name" property="username"/> <result column="pwd" property="password"/> </resultMap>
测试:
//查询 @Test public void testFindById(){ User user = (User)session.selectOne("cn.wh.mapper.UserMapper.findById",1); System.out.println(user.getId()+"----"+user.getUsername()+"---"+user.getPassword()); }