mybatis 传递一个参数,多个参数的应用
2016-12-09 11:08
429 查看
1、传递单个参数注意:当有test条件判断时候,dao层参数必须用注解,否则会报错,public List<Caidan> getcaidan(@Param(value="czyid") String czyid);//参数用注解xml文件配置 <select id="getcaidan" parameterType="java.lang.String" resultType="com.dyaqjy.bean.Caidan"> // parameterType这个属性可以不写 <choose> <when test="czyid=='000_admin'">//用注解cayid也可以写成param1 SELECT * FROM s_caidan where upid='root' </when> <otherwise> SELECT s_caidan.id,s_caidan.title,s_caidan.upid,s_caidan.url, s_caidan.icon,s_quanxian.czyid FROM s_caidan RIGHT OUTER JOIN s_quanxian ON s_quanxian.caidanid = s_caidan.id WHERE s_caidan.upid = 'root' AND s_quanxian.czyid = #{czyid}//用注解cayid也可以写成param1 ORDER BY s_caidan.id ASC </otherwise> </choose> </select>没有参数判断的话,参数可以用注解也可以不用注解,下面几种情况都可以使用①
public List<XXBean> getXXBeanList(String id); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select> 其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字, select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。②
public List<XXBean> getXXBeanList(String id); <select id="getXXXBeanList"resultType="XXBean"> //一个参数的时候,可以不写参数类型属性,直接用#{index},从零开始的 select t.* from tableName t where t.id= #{0} </select>
③
public List<XXBean> getXXBeanList(String id); <select id="getXXXBeanList"resultType="XXBean"> //一个参数额时候,可以不写参数类型属性,直接用#{id} select t.* from tableName t where t.id= #{id} </select> ④ 也可以用最上面的注解,用法都一样,此处不写了2、传递多个参数
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);<select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{0} and name = #{1}</select>由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始3、用注解传递多个参数
public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);xml配置这样写:<select id="getAddrInfo"resultMap="com.xxx.xxx.AddrInfo">SELECT * FROM addr__info where addr_id=#{addrId} and corp_id=#{corpId}</select>以前在<select>语句中要带parameterType的,现在可以不要这样写。4、把所有参数放到一个实体类中,类似于第二种方法public abstract int appliInformationcount(Apply app);
xml配置这样写: <select id="appliInformationcount" parameterType="com.dyaqjy.bean.Apply" resultType="int" >
//parameterType参数写上类的全路径select count(*) FROM s_apply LEFT OUTER JOIN p_area ON s_apply.areaid = p_area.id LEFT OUTER JOIN p_illegal ON s_apply.illegalid = p_illegal.id LEFT OUTER JOIN p_term ON s_apply.termid = p_term.id LEFT OUTER JOIN p_unit ON p_unit.id = s_apply.unitid LEFT OUTER JOIN s_user ON s_user.wxuserid = s_apply.userid where s_apply.termid = #{termid} //通过实体传进来的参数,可以直接使用#{termid} <if test="username!=null and username!=''"> //注意test中的参数直接写变量名,不用加#{},否则不管满不满足条件都为真 and s_user.username = #{username} </if> <if test="ucardid!=null and ucardid!=''"> and s_user.ucardid = #{ucardid} </if> </select>5、传递两个或两个以上的实体参数//dao层的方法参数要用注解public abstract List<Apply> appliInformationSearcher(@Param(value="app")Apply app,@Param(value="pagi")Pagination pagi); <select id="appliInformationSearcher" resultType="com.dyaqjy.bean.Apply" > select s_apply.id, s_apply.userid, s_user.username, s_apply.unitid, p_unit.name unitname, s_apply.termid, p_term.tname termname, s_apply.areaid, p_area.name areaname, s_apply.illegalid, p_illegal.illegalname, s_apply.illegaltype FROM s_apply LEFT OUTER JOIN p_area ON s_apply.areaid = p_area.id LEFT OUTER JOIN p_illegal ON s_apply.illegalid = p_illegal.id LEFT OUTER JOIN p_term ON s_apply.termid = p_term.id LEFT OUTER JOIN p_unit ON p_unit.id = s_apply.unitid LEFT OUTER JOIN s_user ON s_user.wxuserid = s_apply.userid where s_apply.termid = #{param1.termid} <if test="param1.username!=null and param1.username!=''"> //param1代表第一参数,param1.username就是dao方法中第一个参数类中的字段 and s_user.username = #{param1.username} </if> <if test="param1.ucardid!=null and param1.ucardid!=''"> and s_user.ucardid = #{param1.ucardid} </if> order by s_apply.id desc limit #{param2.startRow,jdbcType=INTEGER},#{param2.rows,jdbcType=INTEGER} </select>
相关文章推荐
- 如何启动一个AIR移动应用及向它传递参数
- C#--第四周实验--任务2--定义一个描述坐标点的CPoint类,利用(默认参数值)构造函数传递参数。(控制台应用)
- mybatis 使用一个参数传递数值 sql主要是使用到 in
- Android中用一个应用启动另一个应用并传递参数
- 创建一个pthread线程时如何传递参数
- 一个动态生成表单的checkbox 参数传递解决办法之我解
- datagrid的几点简单应用,功能包括radio的单选,checkbox的多选,鼠标移动到行上颜色变深,链接传递多参数等功能。这里radio和checkbox都使用的是HTML控件。
- C# Visual Studio 2005中在指定文件夹下通过传递参数建立一个文件夹
- xml应用(2): 通过向xsl传递参数+数据岛方式,实现在客户端单个xml的分框架显示
- 在asp.net 中应用POST传递和接收XML文件以及参数.
- 通过URL传递参数的一个有趣的意外
- 指针参数传递内存的一个测试程序
- More Effective C++ 阅读笔记(八)--“抛出一个异常”与“传递一个参数”或“调用一个虚函数”间的差异
- 从一个form(Report)传递多个参数到另外一个form(report)
- JS 参数传递的实际应用代码分析
- 一个小程序引发的讨论(运算优先级、参数传递与调用约定的问题)
- 奇怪问题:使用Request.QueryString传递一个中文参数,结果少了一个字!
- [导入]xml应用(2): 通过向xsl传递参数+数据岛方式,实现在客户端单个xml的分框架显示
- Asp.NET应用thickbox.js传递参数,后台请求不到的问题
- 使用html传递参数的一个简单实例