Mybatis传多个参数(三种解决方案)

Mybatis分页插件 - PageHelpe http://git.oschina.net/free/Mybatis_PageHelper

极其方便的使用Mybatis单表的增删改查 http://git.oschina.net/free/Mapper

Mybatis示例 http://blog.csdn.net/column/details/mybatis-sample.html

MyBatis官方文档 http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html

##第一种方案

DAO层的函数方法

public User selectUser(String name,String area);

对应的Mapper.xml

1<select id="selectUser" resultMap="BaseResultMap"> 2 select * from user_user_t where user_name = #{0} and user_area=#{1} 3</select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。

##第二种方案 此方法采用Map传多参数.

Dao层的函数方法

public User selectUser(Map paramMap);

对应的Mapper.xml

1<select id="selectUser" resultMap="BaseResultMap"> 2 select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} 3</select>

Service层调用

1private User xxxSelectUser(){ 2 Map paramMap=new hashMap(); 3 paramMap.put(“userName”,”对应具体的参数值”); 4 paramMap.put(“userArea”,”对应具体的参数值”); 5 User user=xxx. selectUser(paramMap); 6}

个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。

##第三种方案

Dao层的函数方法

public User selectUser(@param(“userName”)String name,@param(“userArea”)String area);

对应的Mapper.xml

1<select id="selectUser" resultMap="BaseResultMap"> 2 select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} 3</select>

个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。

##mybatis if标签判断的问题

细节可以参考XML代码:

1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.lypaydb.mapper.Order_DayMapper"> 5 <resultMap type="com.lypaydb.pojo.Order_Day" id="odMap"> 6 <id property="id" column="id" /> 7 <result property="date" column="date" /> 8 <result property="total" column="total" /> 9 <result property="aisle" column="aisle" /> 10 <result property="operators" column="operators" /> 11 <result property="channelid" column="channelid" /> 12 <result property="appid" column="appid" /> 13 <result property="paycnt" column="paycnt" /> 14 </resultMap> 15 16 <!-- /*sql --> 17 <select id="findod" parameterType="java.util.Map" resultMap="odMap"> 18 select * from order_day where 1 = 1 19 <if test="${start} != null and ${start != ''}"> 20 and date >= #{start} 21 </if> 22 <if test="${end} != null and ${end} != ''"> 23 and #{end} > date 24 </if> 25 <if test="appid != null and appid != ''"> 26 and appid=${appid} 27 </if> 28 <if test="operators != null and operators != ''"> 29 and operators=${operators} 30 </if> 31 limit ${Page.startPos},${Page.pageSize}; 32 </select> 33 34 <select id="getAllCount" parameterType="java.util.Map" resultType="java.lang.Integer"> 35 select count(*) from order_day where 1=1 36 <if test="${start} != null and ${start != ''}"> 37 and date >= #{start} 38 </if> 39 <if test="${end} != null and ${end} != ''"> 40 and #{end} > date 41 </if> 42 <if test="appid != null and appid != ''"> 43 and appid=${appid} 44 </if> 45 <if test="operators != null and operators != ''"> 46 and operators=${operators} 47 </if> 48 </select> 49 <!-- sql*/ --> 50</mapper>

这里是正确的写法:POJO

1<select id="findod" parameterType="map" resultMap="odMap"> 2 select * from order_day where 1 = 1 3 <if test="start != null and start != ''"> 4 and date >= #{start} 5 </if> 6 <if test="end != null and end != ''"> 7 and #{end} > date 8 </if> 9 <if test="appid != null and appid != ''"> 10 and appid=${appid} 11 </if> 12 <if test="operators != null and operators != ''"> 13 and operators=${operators} 14 </if> 15 limit ${Page.startPos},${Page.pageSize}; 16</select>

MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)

##foreach的使用

1<insert id="insertUserList"> 2 INSERT INTO user(username,password) 3 VALUES 4 <foreach collection="userList" item="user" separator=","> 5 (#{user.username},#{user.password}) 6 </foreach> 7</insert>

对应的接口:

int insertUserList(@Param("userList")List<User> list);
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )