zoukankan      html  css  js  c++  java
  • mybatis中传入String类型参数的问题

    1. 出现的问题

    需求是想写一个按公司名字查询公司列表的功能,最开始的代码如下 
    Dao层接口如下

    @MyBatisDao
    public interface OfficeDao extends TreeDao<Office> {
        List<Office> findCompanyNameList(String name);
    }

    mybatis的xml代码:

    复制代码
    <select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office">
        SELECT id,name FROM sys_office  where o.del_flag = '1'
           <if test="name!= null and name!= ''">
               AND name LIKE concat('%',#{name},'%')
           </if>
    </select>
    复制代码

    这样写会报错,大体意思是name没有Getter方法。

    2. 解决办法

    2.1 解决办法1

    在接口参数里加上mybatis中的@param注解

    @MyBatisDao
    public interface OfficeDao extends TreeDao<Office> {
        List<Office> findCompanyNameList(@Param("name")String name);
    }
    复制代码
    <select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office">
        SELECT id,name FROM sys_office  where o.del_flag = '1'
           <if test="name!= null and name!= ''">
               AND name LIKE concat('%',#{name},'%')
           </if>
    </select>
    复制代码

    2.2 解决办法2

    在xml的if里用”_parameter” 代表参数

    复制代码
    <select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office">
        SELECT id,name FROM sys_office  where o.del_flag = '1'
           <if test="_parameter!= null and _parameter!= ''">
               AND name LIKE concat('%',#{name},'%')
           </if>
    </select>
    复制代码

    2.3 两种方法区别

    可以看出,_parameter不能区分多个参数,而@param能。所以@param能传多个这样的参数

    原文地址:https://www.cnblogs.com/thiaoqueen/p/9634495.html

  • 相关阅读:
    PLSQL学习笔记 wm_concat
    Oracle ERP 公司间往来的解决方案(转载)
    Excel单元格设成只读
    PLSQL笔记 存在性检查
    PL/SQL编程技巧
    查看Oracle EBS克隆Clone时间
    用wm_concat合并行及merge into更新
    商务英语900句
    外企公司常用英文缩写
    ASP.NET 2.0 XML 系列(5):用XmlReader读取XML文档
  • 原文地址:https://www.cnblogs.com/sanhao/p/11428298.html
Copyright © 2011-2022 走看看