前言:ResultMap是让XML中SQL语句的返回值可以映射到实体类
正文:
1,实体类
Basic(名字随便取的)
public class Basic { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } }
继承Basic类的Person类
public class Person extends Basic { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2,在mybatis的sql.xml里就可以这样定义ResultMap
<resultMap id="BasicMap" type="com.bf.vo.Basic"> <result column="ID" jdbcType="VARCHAR" property="id" /> </resultMap> <resultMap id="PersonMap" type="com.bf.vo.Person" extends="BasicMap"> <result column="NAME" jdbcType="VARCHAR" property="name" /> </resultMap> <select id="selectExample" resultMap="PersonMap"> SELECT ID, NAME FROM XX_TABLE </select>