合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
使用ResultMap建立映射 **1.表结构** ![](https://img.kancloud.cn/dd/11/dd11ad4663ff60358e78775d37a1b92b_754x235.png) **2.实体类** ~~~ package com.nobb.domain; public class User { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } } ~~~ **3.在UserMapper.xml中配置 ResultMap** ~~~ <!--id:取名 type:对应的实体可以使用别名--> <resultMap id="userMapper" type="User"> <!--映射id属性,映射主键--> <!--property:实体类的属性--> <!--column:表的字段--> <id property="id" column="u_id"></id> <!-- 映射非id属性-result --> <result property="name" column="u_name"></result> <result property="password" column="u_password"></result> </resultMap> ~~~ **4.UserMapper.java 新增查询方法** ~~~ //新增实体属性与列名不对应的查询方法 List<User> findAll1(); ~~~ **5.UserMapper.xml 查询语句** ~~~ <!--实体属性与列名不对应的处理办法,用resultMap建立映射--> <select id="findAll1" resultMap="userMapper"> select * from t_user </select> ~~~ **6.查询测试** ~~~ //列名不对应的查询方法 @Test public void testFindAll1(){ UserMapper mapper = session.getMapper(UserMapper.class); List<User> users = mapper.findAll1(); System.out.println(users); } ~~~