MyBatis传参数四种方法
来源:互联网 发布:证件制作软件手机版 编辑:程序博客网 时间:2024/06/16 12:26
第一种方式:注解也是开发中最常见的一种方式(明显看出自己所传递的参数,)
DAO类中的方法函数
Public User selectUser(@param("userName")Stringname,@param("userArea")String area);
对应的Mapper.xml语句
<select id=" selectUser" resultMap="BaseResultMap"> select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} </select> 也可以是这样 <select id=" selectUser" resultMap="BaseResultMap"> select * from user_user_t where user_name = #{userName} and user_area=#{userArea} </select>
根据你所拥有的Mybatis版本而去 老版本需要添加参数类型
这种方法的好处在于比较直观,只要看到dao方法就知道传了什么参数。
第二种方式:多个参数传值(索引方式)(简便,减少代码量)
DAO类中的方法函数
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
对应Mapper.xml语句
<select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{0} and name = #{1} </select>
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
第三种方式:Map封装多参数
DAO类中的方法函数
public List<XXXBean> getXXXBeanList(HashMap map);
对应Mapper.xml语句
<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean"> select 字段... from XXX where id=#{xxId} code = #{xxCode} </select>
Service层调用
Private User xxxSelectUser(){Map paramMap=new hashMap();paramMap.put(“userName”,”对应具体的参数值”);paramMap.put(“userArea”,”对应具体的参数值”);User user=xxx. selectUser(paramMap);}其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。
第四种方式:List封装in
DAO类中的方法函数
public List<XXXBean> getXXXBeanList(List<String> list);对应的Mapper.xml文件
<select id="getXXXBeanList" resultType="XXBean"> select 字段... from XXX where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')
0 0
- MyBatis传参数四种方法
- Mybatis传多个参数(四种解决方案)
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- MyBatis接受参数的两种方法
- Mybatis传递参数的几种方法
- MyBatis传多个参数的3种方法
- Mybatis方法参数一
- MyBatis多参数传递的四种方式
- C#中方法的四种参数
- C++参数传递的四种方法
- php四种参数传递方法
- 四种传递参数的方法
- jmeter参数化的四种方法
- jmeter参数化的四种方法
- 简单制作属于自己的二维码
- 前端面试知识点总结
- JSON和JSONP的区别
- 796A
- 捕鱼来了2017系列游戏
- MyBatis传参数四种方法
- 4.26课堂作业 --转换构造函数
- 【LeetCode】 回溯系列
- 八皇后算法
- Problem F: 分数类的模板数组类
- 线段树
- Windows通过 iis创建FTP服务
- 【ACM】P1000、P10001、P1002、P1003代码演示
- BW 库存管理之processkey