mabatis在查询多列多行的数据如何不把结果集映射到实体类中

当我们只需要查询表中的某些字段,又不想用相应的实体类在映射结果的时候,可以使用下面的方法来封装结果。
首先是mapper中的接口需要用 List<Map<String, Object>>接收

	public List<Map<String, Object>> getVersion(@Param("id") int id);
1

column是数据库查询出来的字段名,property你要存在Map中的Key值,你可以自己取名字

<resultMap id="infoMap4" type="java.util.HashMap">
		<id column="id" property="id" jdbcType="TINYINT" />
		<result column="control_id" property="controlId"
			jdbcType="TINYINT" />
		<result column="version" property="version" jdbcType="VARCHAR" />
		<result column="product_type_id" property="productTypeId"
			jdbcType="TINYINT" />
		<result column="name" property="name" jdbcType="VARCHAR" />
	</resultMap>
	<select id="getVersion" resultMap="infoMap4">
		SELECT
		a.id,a.control_id,a.version,a.product_type_id, b.name from
		op_control_version a left join op_control b on a.control_id=b.id
		where
		enabled=1 and product_type_id=#{id}
	</select>

猜你喜欢

转载自blog.csdn.net/WziH_CSDN/article/details/111048894