【mybatis】零碎拾起之参数传递
来源:互联网 发布:截面数据举例 编辑:程序博客网 时间:2024/05/19 14:17
最近项目使用的SSM框架,之前也总结过一些关于Mybatis的东西,现在想继续总结一下,mybatis参数传递的简单知识。
1.传递一个参数
传递一个参数一般情况下直接用就可以,不需要map,不需要实体
public List<PageData> findUserDetail(String id);
<select id="selectUser" parameterType="java.lang.String" resultMap="pd"> select * from t_user where user_id=#{id}</select>
2.传递两个或多个参数
(1)通过序号传递,不使用parameterType
public List<PageData> findUserDetail(String username,String sex);
<select id="selectUser" resultMap="pd"> select * from t_user where user_name=#{0} and sex=#{1}</select>
(2)map传参
Map paramMap=new hashMap();paramMap.put("username","对应具体的参数值");paramMap.put("sex","对应具体的参数值");List<User> userList=xxx.getUserList(paramMap);
public List<User> getUserList(HashMap map);
<select id="getUserList" parameterType="hashmap" resultType="xxx.xxx.user"> select * from t_user where username=#{username} and sex= #{sex} </select>
(3)实体传参
//给用户收藏表赋值 User user=new User(); user.setUserName("www"); user.setSex("女");
List<User> userList=xxx.getUserList(user);
<select id="getUserList" parameterType="xxx.xxx.User" resultType="xxx.xxx.user"> select * from t_user where username=#{username} and sex= #{sex} </select>
(4)@param通过单个参数名传递
Public User selectUser(@param("username")String name,@param(“sex”)String sex);
<select id="getUser" resultType="xxx.xxx.user"> select * from t_user where username=#{username} and sex= #{sex} </select>
3.参数是一个list集合,使用in
public List<XXXBean> getXXXBeanList(List<String> idlist); <select id="getUserList" resultType="xxx.xxx.user"> select * from t_user where user_id in <foreach item="id" index="index" collection="list" open="(" separator="," close=")"> #{id} </foreach> </select>
4.参数既包括单个参数也包括list集合
参数放到map中,然后xml中遍历list
Map<String, Object> map = new HashMap<String, Object>();map.put("idlist", idlist); map.put("sex", "女");
<select id="getUserList" resultType="xxx.xxx.user"> select * from t_user where sex=#{sex} and user_id in <foreach item="id" index="index" collection="list" open="(" separator="," close=")"> #{id} </foreach> </select>
写在后面:
很简单很基础的总结,xml中参数类型都是可以省略不写的,基本上项目中用到的参数传递就这几种了,以后有什么新的方式再补充。 0 0
- 【mybatis】零碎拾起之参数传递
- mybatis 之参数传递
- Mybatis使用之参数传递
- Mybatis介绍之参数传递
- Mybatis介绍之参数传递
- mybatis 参数传递之默认命名
- mybatis查询嵌套之参数传递
- mybatis学习之参数传递方式
- mybatis之sql传递参数映射(六)
- 牛腩新闻发布系统-零碎拾起
- MyBatis-----MyBatis传递数组参数
- mybatis传递多种参数
- mybatis传递参数
- mybatis传递参数
- mybatis多参数传递
- Mybatis参数传递记录
- Mybatis参数传递总结
- MyBatis多参数传递
- 一些关于path环境变量的问题
- spring
- 62:Binary Tree Level Order Traversal
- KNN(二)--近似最近邻算法ANN
- 网页性能优化方案
- 【mybatis】零碎拾起之参数传递
- HTML入门详解+语法详解
- Git常用命令整理
- android Toast运行在主线程中
- JavaScript中Array类型的使用
- Shiro学习总结(二)--Shiro的入门小例子
- KNN(三)--KD树详解及KD树最近邻算法
- C++上机报告3(选择)
- 使用 Java8 Optional 的正确姿势