mybatis中Mapper传参问题
-
传递单值
不带If判空处理的//DAO List<SystemIndustry> findALlResult(String industryId); //XML <select id="findALlResult" resultMap="SystemIndustryMap"> select industry_id, industry_name, parent_industry, deal_type, remark, physics_flag, create_user, create_user_name, create_time, update_time from hstypay.system_industry <where> industry_id = #{任意值} </where> </select>
带if 判空处理的
//DAO List<SystemIndustry> findALlResult(String industryId); //XML <select id="findALlResult" resultMap="SystemIndustryMap"> select industry_id, industry_name, parent_industry, deal_type, remark, physics_flag, create_user, create_user_name, create_time, update_time from hstypay.system_industry <where> <if test="_parameter != null"> and industry_id = #{任意值} </if> </where> </select>
或者
//DAO List<SystemIndustry> findALlResult(@param(value = "param1") String industryId); //XML <select id="findALlResult" resultMap="SystemIndustryMap"> select industry_id, industry_name, parent_industry, deal_type, remark, physics_flag, create_user, create_user_name, create_time, update_time from hstypay.system_industry <where> <if test="param1 != null"> and industry_id = #{param1} </if> </where> </select>
-
多值传递
//DAO List<SystemIndustry> findALlResult(String industryId,String industryName); //XML <select id="findALlResult" resultMap="SystemIndustryMap"> select industry_id, industry_name, parent_industry, deal_type, remark, physics_flag, create_user, create_user_name, create_time, update_time from hstypay.system_industry <where> <if test="industryId != null"> and industry_id = #{industryId} </if> <if test="industryName != null and industryName != ''"> and industry_name = #{industryName} </if> </where> </select>
-
Map参数(参数即是Key值)
//DAO List<SystemIndustry> findALlResult(Map map); //XML <select id="findALlResult" resultMap="SystemIndustryMap"> select industry_id, industry_name, parent_industry, deal_type, remark, physics_flag, create_user, create_user_name, create_time, update_time from hstypay.system_industry <where> <if test="industryId != null"> and industry_id = #{industryId} </if> <if test="industryName != null and industryName != ''"> and industry_name = #{industryName} </if> </where> </select>
-
List类型参数
//DAO List<SystemIndustry> findALlResult(List<String> ids); //XML <select id="findALlResult" resultMap="SystemIndustryMap"> select industry_id, industry_name, parent_industry, deal_type, remark, physics_flag, create_user, create_user_name, create_time, update_time from hstypay.system_industry where industry_id IN <foreach collection="list" item="industryId" open="(" separator="," close=")"> <if test="industryId != null"> #{industryId} </if> </foreach> </select>
-
多参数传递
例子: public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId); xml配置这样写: <select id="getAddrInfo" resultMap="com.xxx.xxx.AddrInfo"> SELECT * FROM addr__info where addr_id=#{addrId} and corp_id=#{corpId} </select>