resultMap 标签
用法:当从数据库中查询的字段与要返回结果集字段不一致时候,将查询的数据映射为相应的结果集
- id(resultMap) :标签的唯一标识
- id(cloumn):数据库对应数据的唯一主键标识
- result: 对应数据库中其他字段
- jdbcType: 数据库字段对应的类型
- property:所要映射的实体类对应的字段
<select id="QryResult" ResultMap="BaseResultMap">
SELECT * FROM T_RESULT_USER
</select>
//从数据库中查询,返回结果为ResultEntity
<resultMap id="BaseResultMap" type="com.shop.entity.ResultEntity">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="COMMODIY_NAME" jdbcType="VARCHAR" property="Name" />
<result column="COMMODIY_Price" jdbcType="BIGINT" property="Price" />
</resultMap>
public class ResultEntity{
private int id;
private int Price;
private string Name;
<!---省略getter和setter方法---->
}
一对一关联查询
- association:映射的pojo类中字段类型为其他类的时候用关联查询
//从数据库中查询,返回结果为ResultEntity
<resultMap id="BaseResultMap" type="com.shop.entity.ResultEntity">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="COMMODIY_NAME" jdbcType="VARCHAR" property="Name" />
<result column="COMMODIY_Price" jdbcType="BIGINT" property="Price" />
<association javaType="user" property="user">
<id column="USER_ID" jdbcType="BIGINT" property="id" />
<result column="USER_NAME" jdbcType="VARCHAR" property="UserName" />
<result column="USER_ADRESS" jdbcType="VARCHAR" property="Adress" />
</association>
</resultMap>
public class ResultEntity{
private int id;
private int Price;
private string Name;
private User user; <!---多了这个---->
<!---省略getter和setter方法---->
}
public class user{
private int id;
private string UserName;
private string Adress;
<!---省略getter和setter方法---->
}
一对多关联查询
collection: 映射的pojo类中字段类型为其他类集合的时候用关联查询
//从数据库中查询,返回结果为ResultEntity
<resultMap id="BaseResultMap" type="com.shop.entity.ResultEntity">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="COMMODIY_NAME" jdbcType="VARCHAR" property="Name" />
<result column="COMMODIY_Price" jdbcType="BIGINT" property="Price" />
<collection javaType="list" property="users" ofType="user">
<id column="USER_ID" jdbcType="BIGINT" property="id" />
<result column="USER_NAME" jdbcType="VARCHAR" property="UserName" />
<result column="USER_ADRESS" jdbcType="VARCHAR" property="Adress" />
</collection>
</resultMap>
public class ResultEntity{
private string id;
private string Name;
private string Price;
private List<user> Users; <!---换成了这个---->
<!---省略getter和setter方法---->
}