Mybatis map使用的几种处理情况

后台没有实体对象接收的表

查数据时,使用Map<String,Object> map对象接收
返回值类型为HashMap,

<select id="listInventoryStatusObj" resultType="java.util.HashMap">
		select * from
		ias_inventory_status
	</select>

foreach 中参数介绍:

foreach元素的属性主要有 item,index,collection,open,separator,close。

分别代表意思:

item表示集合中每一个元素进行迭代时的别名,
index用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔 符,
close表示以什么结束

collection参数使用情况:

  1. 当查询的参数只有一个时
    1.1 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
    1.2 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
  2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
    这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称(指定的名称为存储在map中的key)不单单forech中的collection属性是map.key,其它所有属性都是map.key,

1.1插入数据时,参数是一个集合时,parameterType="java.util.List"


<insert id="addObjectByBranch" parameterType="java.util.List">
		insert ignore into ias_asset (
		<include refid="Base_Column_List" />
		)
		values
		<foreach collection="list" item="item" index="index"
			separator=",">
			(
			<include refid="Base_Value_branch" />
			)
		</foreach>
	</insert>
<sql id="Base_Column_List">
		assetNum, assetName, assetModel, assetPrice, assetFactory,
		assetDocumentNum,
		assetBuyDate,
		assetTakePeople, assetRemrk, storeNum,
		dicProfitLossNum, departNum
	</sql>
	<sql id="Base_Value_branch">
		#{item.assetNum},#{item.assetName},#{item.assetModel},#{item.assetPrice},#{item.assetFactory},#{item.assetDocumentNum},#{item.assetBuyDate},
		#{item.assetTakePeople},#{item.assetRemrk},#{item.storeNum},#{item.dicProfitLossNum},#{item.departNum}
	</sql>

<select id="findByIdsMap" resultMap="BaseResultMap">

         Select

         <include refid="Base_Column_List" />

         from jria where ID in
                  <foreach item="item" index="index" collection="list" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 

1.2 参数的类型是Array

<select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 

2. 查询的参数有多个时

Map<String, Object> params = new HashMap<String, Object>(2);

        params.put("name", name);

         params.put("ids", ids);

        mapper.findByIdsMap(params);

<select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="ids" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
   </select> 

3. 插入数据时,参数是一个map对象

<insert id="importAssetToTemportaryTable" parameterType="Map">
		insert ignore into ias_import_asset(
		<include refid="key1" />
		)
		values(
		<include refid="values1" />
		)
	</insert>
	<sql id="key1">
		<trim suffixOverrides=",">
			imUnitName,imAssetRange,imDocumentNum,imAssetName,
			imAssetModel,imAssetPrice,imFactory,imBuyDate,imTakePeople,imRemark,
			<if test="imProfitLoss != null and imProfitLoss!= ''">
				imProfitLoss,
			</if>
			departNum,
		</trim>
	</sql>
	<sql id="values1">
		<trim suffixOverrides=",">
			#{imUnitName},#{imAssetRange},#{imDocumentNum},
			#{imAssetName},#{imAssetModel},#{imAssetPrice},
			#{imFactory},#{imBuyDate},#{imTakePeople},
			#{imRemark},
			<if test="imProfitLoss != null and imProfitLoss!= ''">
				#{imProfitLoss},
			</if>
			#{departNum},
		</trim>
	</sql>

4.1 查找时,条件个数不确定,java后台有实体类包含这些条件时,使用test条件判断

<select id="listExportAssetInfo" resultMap="BaseResultMap">
		select * from ias_asset
		<where>
			<if test="assetNum!=null and assetNum!=''">assetNum=#{assetNum}</if>
			<if test="assetName!=null and assetName!=''">and assetName=#{assetName}</if>
			<if test="assetFactory!=null and assetFactory!=''">and assetFactory=#{assetFactory}</if>
			<if test="assetDocumentNum!=null and assetDocumentNum!=''">and assetDocumentNum=#{assetDocumentNum}</if>
			<if test="assetBuyDate!=null and assetBuyDate!=''">and assetBuyDate=#{assetBuyDate}</if>
			<if test="assetTakePeople!=null and assetTakePeople!=''">and assetTakePeople=#{assetTakePeople}</if>
			<if test="storeNum!=null">and storeNum=#{storeNum}</if>
			<if test="dicProfitLossNum!=null">and dicProfitLossNum=#{dicProfitLossNum}</if>
			<if test="departNum!=null and departNum!=''">and departNum=#{departNum}</if>
		</where>
	</select>

4.2 查找,修改时,条件个数不确定,后台没有对应的实体类包含这些,则用map对象封装这些条件。

<update id="updateInventory" parameterType="java.util.HashMap">
		update inventory
		set
		<if test="inventoryStatusId != null">inventoryStatusId=#{inventoryStatusId}</if>
		<if test="inventoryDate != null and '' != inventoryDate">and inventoryDate=#{inventoryDate}</if>
		<if test="userCount != null and '' != userCount">and userCount=#{userCount}</if>
		<if test="departNum != null and '' != departNum">and departNum=#{departNum}</if>
		<if test="assetTakePeople != null and '' != assetTakePeople">and assetTakePeople=#{assetTakePeople}</if>
		<if test="spanAdress != null">and spanAdress=#{spanAdress}</if>
		<if test="storeNum != null ">and spanAdress=#{spanAdress}</if>
		<if test="dicProfitLossNum != null">and dicProfitLossNum=#{dicProfitLossNum}</if>
		where
		assetNum=#{assetNum} and inventoryBatch=#{inventoryBatch}
	</update>

5. 查找部分字段,分局条件进行选择
使用Mybatis 的if标签进行参数的判定,是否需要查出该字段

<select id="getInventoryParameterByCondition" parameterType="java.util.HashMap"
		resultType="Object">
		select
		<if test='inventoryStatusId == "inventoryStatusId"'>inventoryStatusId</if>
		<if test='inventoryDate == "inventoryDate"'>inventoryDate</if>
		<if test='userCount == "userCount"'>userCount</if>
		<if test='departNum == "departNum"'>departNum</if>
		<if test='assetTakePeople == "assetTakePeople"'>assetTakePeople</if>
		<if test='spanAdress == "spanAdress"'>spanAdress</if>
		<if test='storeNum == "storeNum"'>storeNum</if>
		<if test='dicProfitLossNum == "dicProfitLossNum"'>dicProfitLossNum</if>
		from inventory
		where
		inventoryBatch = #{inventoryBatch}and assetNum = #{assetNum}
	</select>

参考文章1
参考文章2

MyBatis获取参数的几种方式有: 1. 使用@Param注解:在Mapper接口的方法参数列表中使用@Param注解,可以为参数取一个名字,方便在SQL语句中引用。 例如: ``` @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(@Param("id") int userId); ``` 2. 使用Map类型参数:在Mapper接口的方法参数列表中使用Map类型参数,可以将所有参数封装到Map中,通过键值对的方式传递参数。 例如: ``` @Select("SELECT * FROM user WHERE id = #{id} AND name = #{name}") User getUserByIdAndName(Map<String, Object> paramMap); ``` 3. 使用POJO类型参数:在Mapper接口的方法参数列表中使用自定义的POJO类型参数,可以将参数封装到一个对象中,通过对象的属性名来引用参数。 例如: ``` @Insert("INSERT INTO user (id, name, age) VALUES (#{id}, #{name}, #{age})") int addUser(User user); ``` 4. 使用数组类型参数:在Mapper接口的方法参数列表中使用数组类型参数,可以将多个参数封装到一个数组中,通过数组下标的方式来引用参数。 例如: ``` @Select("SELECT * FROM user WHERE id IN (#{ids})") List<User> getUsersByIds(@Param("ids") int[] ids); ``` 5. 使用List类型参数:在Mapper接口的方法参数列表中使用List类型参数,可以将多个参数封装到一个List中,通过List中元素的位置来引用参数。 例如: ``` @Select("SELECT * FROM user WHERE id IN (#{ids})") List<User> getUsersByIds(List<Integer> ids); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值