mybatis Resultmap 与 ResultType 区别

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">
		SELECT
		store_name,
		store_type,
		store_path,
		store_where
		FROM
		store_manager dar
		WHERE
		dar.store_name = #{storeName} and
		dar.store_type = #{storeType}
	</select>


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

<select id="selectStore1" parameterType="com.suning.jupiter.common.pojo.storemanager.StoreBO"
		resultType="java.util.Map">
		SELECT
		store_name,
		store_type,
		store_path,
		store_where
		FROM
		store_manager dar
		WHERE
		dar.store_name = #{storeName} and
		dar.store_type = #{storeType}
	</select>

@Override
	public List<StoreBO> getStoreList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		
		log.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和数据库表对应。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值