mybatis 封装Map数据方式
第一种方式常用的bean
<select id="list" resultType="com.yitai.tms.weixin.domain.WeChatModel">
select * from tms_order
</select>
返回来一个 WeChatModel对象的list
对应Dao @Mapper 的接口 List<WeChatModel> list(Map<String, Object> map);
第二种 返回一个list<String> 字符串列表,虽然查询三个字段,但是只返回第一个字段列表名称,也就是说在单个参数为list的时候,可以用这种方式
对应Dao 的@Mapper接口 List searchCustomer(String customer);
<select id="searchCustomer" resultType="String">
select PROJECT_ID,PROJECT_NAME,PRODUCT_NAME from tms_project where
CUSTOMER_ID in(select customer_id from tms_customer where CUSTOMER_NAME = #{customer}) group by PROJECT_NAME
</select>
第三种方式,也是比较简单的方式,主要是返回结果, 是HaspMap 或者是Map都行
<!-- 获取线路 --> parameterType="java.util.Map" 这是参数类型,可以不写
<select id="searchLine" parameterType="java.util.Map" resultType="java.util.HashMap">
select id,line_name from tms_line where PROJECT_ID='273'
</select>
<!-- 获取温控 -->
<select id="searchTemperature" parameterType ="java.util.Map" resultType="java.util.Map">
select id,project_id,min_temp,max_temp from tms_temperature where project_id ='273'
</select>
对应Dao @Mapper接品方法 ,配置里写的参数是Map.接口参数是String 不影响结果
List<Map<String, Object>> searchLine(String id);
List<Map<String, Object>> searchTemperature(String id);
另外参数的传递方式也有多种
1 在Dao @Mapper接口里 基于注解
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
where id = #{id} and name = #{code}
2 Map封装多参数: parameterType 参数类型可以加上
public List<XXXBean> getXXXBeanList(HashMap map);
<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
where id=#{xxId} code = #{xxCode}
3 List封装in 如果传数据组的话,直接转换成list处理要方便
public List<XXXBean> getXXXBeanList(List<String> list);
<select id="getXXXBeanList" resultType="XXBean">
select 字段... from XXX where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item}
</foreach>
</select>
其它参数结合 Map 基本可以实现复杂参数传递,
另外如果用bean封装的话,好处是可以及时更新缓存,如果返回Map形式的话,更新缓存可能就有些麻烦,没有尝试过如何更新,或者说更新是不是与bean一样,有待验证
第四种,把MAP转化成bean
<resultMap id="BaseResultMap" type="com.xiaolyuh.domain.model.Person">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="address" property="address" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id, name, age, address
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List"/>
from person
where id = #{id,jdbcType=BIGINT}
</select>