后台没有实体对象接收的表
查数据时,使用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 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
1.2 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array - 当查询的参数有多个时,例如 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>