我的爬坑日记丨使用mybatis时,方法中的参数为类时使用@Param("")导致Parameter 'xxx' not found

先看源码

下图为接口中定义的抽象方法,Employee为自己编写的实体类,我用到了@Param("xxx")注解

下图为xml映射文件中对应的查询语句,这条语句的作用是对向employee表中查询符合字段state、name的数据条数

下面是测试方法以及对应的报错信息

错误信息告诉我参数列表中的state找不到。但是我之前也的xml映射文件也写过类似的操作,通过${xxx}设置要获取的属性,mybatis会自动获取到传进来的对象的属性值。怎么现在就不可以了呢。

继续查看错误信息

错误信息告诉我可用的参数有employee。这不就是我之前设置的@Param("xxx")注解吗。于是我将xml映射文件中的语句修改如下,修改地方已圈出。

运行测试方法,运行正常,查出结果

从上可以看出,如果对对象是用了注解,要获取对象的属性值,应当使用 【注解名.属性名】 的方式。而当不使用注解时,可以直接通过【属性名】获取到对象的值。

下面做个总结,分别列出使用注解与不使用注解时,xml映射文件中sql的写法

下面为对类参数使用了注解的写法

   //IEmployeeDao.java
     /**
     * 获取符合表单条件的数据条数
     * @param employee
     * @return
     */
    int getCountForForm(@Param("employee") Employee employee);
   //IEmployeeDao.xml
    <select id="getCountForForm" parameterType="obj.Employee"    resultType="java.lang.Integer">
        select count(*)
      from employee
      <where>
          <if test="employee.state >= 0">
              employee.state = #{employee.state}
          </if>
          <if test="employee.name != null">
              and employee.name like concat('%',#{employee.name},'%')
          </if>
      </where>
    </select>
    //IEmployeeDaoTest.java
    public class IEmployeeDaoTest {
        IEmployeeDao mapper = null;
        SqlSession sqlSession = null;
        @Before
        public void setUp() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream stream = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
            sqlSession = factory.openSession();
            mapper = sqlSession.getMapper(IEmployeeDao.class);
        }
        @After
        public void closeConnection(){
            sqlSession.commit();
            sqlSession.close();
        }
        @Test
        public void updateById(){
            Employee employee = mapper.getEmployeeById(1);
            employee.setName("饶");
            System.out.println(employee);
            int x = mapper.updateById(employee);
            System.out.println(x);
        }
    }
    

下面是对类参数不使用注解

   //IEmployeeDao.java
     /**
     * 获取符合表单条件的数据条数
     * @param employee
     * @return
     */
    int getCountForForm(Employee employee);
   //IEmployeeDao.xml
    <select id="getCountForForm" parameterType="obj.Employee"    resultType="java.lang.Integer">
        select count(*)
      from employee
      <where>
          <if test="state >= 0">
              employee.state = #{state}
          </if>
          <if test="name != null">
              and employee.name like concat('%',#{name},'%')
          </if>
      </where>
    </select>
    //IEmployeeDaoTest.java
    public class IEmployeeDaoTest {
        IEmployeeDao mapper = null;
        SqlSession sqlSession = null;
        @Before
        public void setUp() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream stream = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
            sqlSession = factory.openSession();
            mapper = sqlSession.getMapper(IEmployeeDao.class);
        }
        @After
        public void closeConnection(){
            sqlSession.commit();
            sqlSession.close();
        }
        @Test
        public void updateById(){
            Employee employee = mapper.getEmployeeById(1);
            employee.setName("饶");
            System.out.println(employee);
            int x = mapper.updateById(employee);
            System.out.println(x);
        }
    }

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值