MyBatis+MySQL 传入参数

来源:互联网 发布:linux 标准输出重定向 编辑:程序博客网 时间:2024/05/22 00:18

1.String 单独传参

public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex); 


2,Mapper位置传参数

 #{0}

#{1}


3,Map出入参数

java:

 Map<String,Object> par = new HashMap<>();
        par.put("userId",userId);
        par.put("orderSn",orderSn);
        return marketCommentDao.getCommentByUid(par);

xml:

 <select id="getOrderCountForPlat"  parameterType="map" >



4,传入数组参数

@RequestMapping(value = "/applyJob", method = RequestMethod.POST,consumes = "application/json")
@ResponseBody
public DataTransferObject applyJob(@RequestBody Integer[] jobIds,HttpServletRequest request) {
Integer userId = SessionUtil.getUserId(request);
   return jobProxy.applyJob(jobIds,userId);
}


5,java对象中mappr属性传参

java中的代码
   private Map<String, Object> params = new HashMap<String, Object>();

xml中代码

 <if test="params != null">
<if test="params.get('excludeId') != null">
or n.id = ${params.get('excludeId')} 
</if>
</if>

6,JsonObject传入参数

java代码:

@RequestMapping(value = "/page",method = RequestMethod.POST)
@ResponseBody
public DataTransferObject page(@RequestBody JSONObject param){

java取参数:  int pageNo = Integer.valueOf(param.get("pageNo").toString());
        int pageSize = Integer.valueOf(param.get("pageSize").toString());

String content = param.optString("content", ""); //推荐
        Integer result = param.optInt("result", 0); //推荐


java设置参数:

 param.put("start", pageNo * pageSize);
        param.put("end", pageSize);

java:判断,取出数组,移除

 if (param.has("attributeItemIds")) {
            JSONArray attributeItemIds = param.getJSONArray("attributeItemIds");
            if (null == attributeItemIds || attributeItemIds.size() == 0) {
                param.remove("attributeItemIds");
            }
        }

entityQO对象传递:

private Integer attributeId;

private JSONArray attributeItemIds;// 兼容web端

xml取出:

1,  <select id="phoneListCommodityCompleteInfo" resultType="com.uei.phone.manager.model.market.CommodityVo" parameterType="com.uei.phone.manager.model.market.CommodityQo" >

2, <select id="listCmomodityCompleteInfo" resultType="hashMap" parameterType="org.json.simple.JSONObject" >


取出与遍历

<if test="catagoryId != null and catagoryId != ''">
    AND b.catagory_id = #{catagoryId}
    </if>
    <if test="catagoryIds != null and catagoryIds.size != 0">
    AND b.catagory_id IN
    <foreach item="catagoryId" index="index" collection="catagoryIds" open="(" separator="," close=")">
        #{catagoryId}
  </foreach>
    </if>



0 0
原创粉丝点击