• 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

  • 相关阅读:
    app测试的一些较为重要的测试点
    adb工作常用命令
    vue中$attrs $listeners你会用吗?
    Vue中组件通信的常用方式
    Vue中watch 的用法
    VUE中使用 async await 将 axios 异步请求同步化处理
    安装spyder记录
    树莓CM3开机连接WIFI
    树莓派搭建seafile服务器备忘
    关于JAVA数据结构_线性表(通过单向链表实现)的学习
  • 原文地址:https://www.cnblogs.com/sanhao/p/11428298.html
Copyright © 2020-2023  润新知