mybatis Resultmap 与 ResultType 区别

来源:互联网 发布:python static method 编辑:程序博客网 时间:2024/05/16 07:25

Resultmap 的写法====目的是为了做映射

<resultMap id="BaseResultMap"type="com.suning.jupiter.common.pojo.storemanager.StoreBO"><!-- <result column="id" property="id" jdbcType="BIGINT" /><result column="storeName" property="store_name" jdbcType="VARCHAR" /><result column="storeType" property="store_type" jdbcType="VARCHAR" /><result column="storePath" property="store_path" jdbcType="VARCHAR" /><result column="storeWhere" property="store_where" jdbcType="VARCHAR" /> -->        <result column="store_name" property="storeName" jdbcType="VARCHAR" /><result column="store_type" property="storeType" jdbcType="VARCHAR" /><result column="store_path" property="storePath" jdbcType="VARCHAR" /><result column="store_where" property="storeWhere" jdbcType="VARCHAR" /></resultMap>

注意:左边是数据库类型字段,右边是类字段,千万不可以做反了。

使用:

<select id="selectStore" parameterType="com.suning.jupiter.common.pojo.storemanager.StoreBO"resultMap="BaseResultMap">SELECTstore_name,store_type,store_path,store_whereFROMstore_manager darWHEREdar.store_name = #{storeName} anddar.store_type = #{storeType}</select>


resultType 只是确定返回类型 是什么,而且 数据库字段就是  map 的 key 的字段:

<select id="selectStore1" parameterType="com.suning.jupiter.common.pojo.storemanager.StoreBO"resultType="java.util.Map">SELECTstore_name,store_type,store_path,store_whereFROMstore_manager darWHEREdar.store_name = #{storeName} anddar.store_type = #{storeType}</select>

@Overridepublic List<StoreBO> getStoreList(Map<String, Object> map) {// TODO Auto-generated method stublog.info("ManagerServiceImpl -> addStore");if (map == null) {log.info("ManagerServiceImpl -> addStore map=null.");return null;}StoreBO storeBO = new StoreBO();storeBO.setStoreName((String)map.get("storeName"));storeBO.setStoreType((String)map.get("storeType"));List<StoreBO> list = managerStoreMapper.selectStore(storeBO);List<Map<String,Object>> listData=managerStoreMapper.selectStore1(storeBO);return list;}
返回的数据:

[{store_path=http://lxl.com, store_name=lxl1, store_type=Docker, store_where=徐庄}]

按理说 resultType 是不能实现 数据库到对象的转变的,因为某字段无法和对象的属性对应起来。所以只是 map和数据库表对应。