标签:封装 public jdb char 调用 rom sel type fun
先看一段mybatis的代码
<resultMap id="BaseResultMap" type="com.example.tsfunproj.entity.BaseDataMap">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="MAP_CODE" property="mapCode" jdbcType="VARCHAR"/>
<result column="MAP_NAME" property="mapName" jdbcType="VARCHAR"/>
<result column="CREATE_TIME" property="createTime" jdbcType="DATE"/>
<result column="CREATOR" property="creator" jdbcType="VARCHAR"/>
<result column="MODITY_TIME" property="modityTime" jdbcType="DATE"/>
<result column="MODIFIER" property="modifier" jdbcType="VARCHAR"/>
</resultMap>
代码段1 ,
<select id="getBaseDataMapByMapCode" parameterType="String" resultMap="BaseResultMap">
select ID,MAP_CODE,MAP_NAME,CREATE_TIME,CREATOR,MODITY_TIME,MODIFIER from BASE_DATA_MAP
where MAP_CODE = #{mapCode,jdbcType=VARCHAR}
</select>
代码段2 ,
<select id="getBaseDataMapByMapCode" parameterType="String" resultType="BaseDataMap">
select ID,MAP_CODE,MAP_NAME,CREATE_TIME,CREATOR,MODITY_TIME,MODIFIER from BASE_DATA_MAP
where MAP_CODE = #{mapCode,jdbcType=VARCHAR}
</select>
其中代码段1,是返回值resultMap,代码段2,返回值resultType。
这里,当你调用getBaseDataMapByMapCode,查询值的时候,resultType,是获取不到值的,只能够获取到id的值
为什么会这样呢?
public class BaseDataMap {
private int id;
private String mapCode;
private String mapName;
private Date createTime;
private String creator;
private String modityTime;
private String modifier;
}
因为我们的pojo的成员属性名字,跟数据库列名不能够一一对应,这时候resultType就不起作用了。
① resultType
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中
② resultMap
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
所以,这里要使用resultMap将数据库名和pojo名字对应起来的才可以查询到数据,
一个小bug,却花了我20分钟去检查,记录一下。
resultType和resultMap区别,对一个数据库查询方法的分析
标签:封装 public jdb char 调用 rom sel type fun
原文地址:https://www.cnblogs.com/Koaler/p/12306390.html