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

先看源码

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

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

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

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

继续查看错误信息

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

扫描二维码关注公众号,回复: 4941253 查看本文章

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

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

下面做个总结,分别列出使用注解与不使用注解时,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);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_36134369/article/details/84349575
今日推荐