很:使用映射的接口与XML配置,为全球参数
题
我喜欢XML符号用于指定全球参数,例如连串。我也像映射器注释。当我试图把两者结合起来,我得到 这一例外.
有没有办法把两者结合起来?我想用XML文件的全球结构,但是已经实际采取的映射的接口考虑在内。
问题是,SqlSessionFactoryBuilder().建立()需要一个读者(其中我要用于通过XML配置),或构成对象(其中我看到的 addMappers()
方法,可以帮助我)-但是我不理解如何将二者结合起来。
解决方案
factory.getConfiguration().addMapper(...);
其他提示
当你创造的 映射的接口 与抽象的方法具有确切的方法的签名作为sql在xml。
为。这是名字空间的dao.xml 其中包含实际查询。
<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>
它将映射在 口com.实际上.道。EntityMapperInterface
public interface EntityMapperInterface {
public List<Employee> selectEmployeeWithId(Long id);
很配置文件
<mappers>
<mapper resource="com/mybatis/mappers/EntityMapper.xml" />
</mappers>
您是如何称呼它的行动类/Servlet?当妳有探的初始化,
EntityMapperInterface emi = session.getMapper(EntityMapperInterface.class);
List eList = emi.selectEmployeeWithId(1);
我有同样的问题,是因为名字中的空间很映射器文件和包裹的映射的接口并不匹配。
不隶属于 StackOverflow