mybatis: Using mapper interfaces with XML config for global parameters

后端 未结 3 1222
野趣味
野趣味 2021-02-04 11:05

I like the XML notation for specifying global parameters such as connection strings. I also like Mapper annotations. When I try to combine the two, I get this exception.

相关标签:
3条回答
  • 2021-02-04 11:16

    When u create the mapper interface with the abstract methods having the exact method signature as the sql in the xml.

    For eg. This was the namespace for the dao.xml which contained the actual query.

    <mapper namespace=" com.mybatis.dao.EntityMapperInterface">
        <select id="selectEmployeeWithId" parameterType="Long"
            resultType="com.mybatis.domain.Employee">
            select id,name from employee where 1=1
            <if test="_parameter != null"> 
                AND id=#{id} 
            </if>
            order by id
        </select>
    

    It will be mapped in the interface com.mybatis.dao.EntityMapperInterface

    public interface EntityMapperInterface {
        public List<Employee> selectEmployeeWithId(Long id);
    

    Mybatis-config file

    <mappers>
        <mapper resource="com/mybatis/mappers/EntityMapper.xml" />
    </mappers>
    

    How do u call it from the Action class/Servlet? When u have the SqlSession initialized,

    EntityMapperInterface emi = session.getMapper(EntityMapperInterface.class);
    List eList = emi.selectEmployeeWithId(1);
    
    0 讨论(0)
  • 2021-02-04 11:33

    factory.getConfiguration().addMapper(...);

    0 讨论(0)
  • 2021-02-04 11:35

    I had the same issue and was because the name space in mybatis mapper file and the package of the mapper interface were not matching.

    0 讨论(0)
提交回复
热议问题