使用foreach解析输入参数数组
来源:互联网 发布:2016淘宝好评返现违规 编辑:程序博客网 时间:2024/06/06 20:37
背景:
SELECT * FROM `user` WHERE sex = '0' and (id = 1 or id = 2 or id =3)//或者SELECT * FROM `user` WHERE sex = '0' and id IN(1,2,3)
第一步在pojo类的包装类中加入
private List<Integer> ids;
并生成get/set方法
第二部修改mapper.xml文件
<!-- sql片段 建议:对单表进行sql片段的抽取,方便重用 抽取时不包含where --> <sql id="findUserSql"> <if test="userCustomer!=null"> <if test="userCustomer.sex!=null and userCustomer.sex!=''"> and user.sex=#{userCustomer.sex} </if> <if test="userCustomer.address!=null and userCustomer.address!=''"> and user.address like '$%{userCustomer.address}%' </if> <if test="ids!=null"> <!-- collection:指定输入对象的集合属性 item:每一个遍历生成的对象 open:开始遍历串拼接串 close:结束拼接时遍历的串 separator:遍历的两个对象中间需要拼接的串 --> <!-- 使用实现这个串的拼接and(id = 1 or id = 2 or id =3) --> <foreach collection="ids" item="i_id" open="and(" close=")" separator="or"> id = #{i_id} </foreach> </if> </if> </sql>
第二种情况
<!-- 使用实现这个串的拼接 and id IN(1,2,3) --><foreach collection="ids" item="i_id" open="and id IN(" close=")" separator=","> #{i_id}</foreach>
接口不需要改
测试:
@Test public void findUserCount() throws Exception{ SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper usermapper = sqlSession.getMapper(UserMapper.class); //构建一个包装类 UserQueryVo userQueryVo = new UserQueryVo(); //构建一个增强类 UserCustomer userCustomer = new UserCustomer(); userCustomer.setAddress("明月"); //包装类包装这增强类作为查询条件 userQueryVo.setUserCustomer(userCustomer); ArrayList<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); userQueryVo.setIds(ids); List<UserCustomer> userList = usermapper.fingUserList(userQueryVo); sqlSession.close(); for (UserCustomer user : userList) { System.out.println(user); } }
阅读全文
0 0
- 使用foreach解析输入参数数组
- <c:foreach>参数使用
- 使用 foreach 操作数组
- 使用 foreach 操作数组
- 使用 foreach 操作数组
- 使用foreach操作数组
- Mybatis动态Sql的Foreach遍历拼接输入参数中的List或数组
- python_getopt解析命令行输入参数的使用
- python_getopt解析命令行输入参数的使用
- python_getopt解析命令行输入参数的使用
- foreach 解析java二维数组
- 新特性对数组的支持(可变参数的使用、foreach输出的使用)
- 使用foreach语句遍历数组
- 使用foreach遍历三维数组
- java 数组操作1.5后新特性--可变参数以及foreach使用
- PHP 使用list函数each函数遍历数组(实现foreach)解析
- 利用数组,过滤非法字符,在foreach循环检查输入
- 使用foreach,list来遍历数组
- gdb can't used by the error ImportError: No module named site
- 安装linux服务器的环境
- Android中的AlarmManager的使用
- POJ 1175 Starry Night 笔记
- PAT 甲级 1001. A+B Format
- 使用foreach解析输入参数数组
- Tomcat Can't load AMD 64-bit .dll on a IA 32
- spring4整合定时器调度
- C语言-数据结构-插入
- andriod studio 调试时可以调用出模拟器却无法显示调试内容(真机,第三方模拟器)
- Android 视频播放器之JieCaoVideoPlayer的简单实用以及自定义UI
- C++ 智能指针
- 探索并优化组合算法的中的01移位转换法(非递归)
- 关于AlarmManager运用需要注意事项和解决办法