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.
我喜欢用于指定全局参数(如连接字符串)的XML表示法。我还喜欢Mapper注释。当我试图将两者结合时,我得到了这个异常。
Is there a way to combine the two? I would like to use an XML file for the global configurations, but have mybatis take Mapper interfaces into account.
有办法把两者结合起来吗?我希望对全局配置使用XML文件,但要考虑到mbatis接口。
The problem is that SqlSessionFactoryBuilder().build() takes either a Reader (which I want to use to pass the XML config), or a Configuration object (which I see has the addMappers()
method that can help me) - but I don't understand how to combine the two.
问题是,SqlSessionFactoryBuilder().build()将读取一个阅读器(我想使用它来传递XML配置),或者一个配置对象(我看到它有addMappers()方法可以帮助我)——但是我不知道如何将两者结合起来。
9
factory.getConfiguration().addMapper(...);
factory.getConfiguration().addMapper(…);
6
When u create the mapper interface with the abstract methods having the exact method signature as the sql in the xml.
当您使用抽象方法创建mapper接口时,该抽象方法在xml中具有与sql相同的方法签名。
For eg. This was the namespace for the dao.xml which contained the actual query.
如。这是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>
It will be mapped in the interface com.mybatis.dao.EntityMapperInterface
它将被映射到接口com. myatis.dao.entitymapperinterface中
public interface EntityMapperInterface {
public List<Employee> selectEmployeeWithId(Long id);
Mybatis-config file
Mybatis-config文件
<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,
如何从Action类/Servlet调用它?当您初始化SqlSession时,
EntityMapperInterface emi = session.getMapper(EntityMapperInterface.class);
List eList = emi.selectEmployeeWithId(1);
0
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.
我有相同的问题,是因为mybatis映射文件中的名称空间和mapper接口的包不匹配。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/11/24/f7828d9232913d966350a5f713fef4ca.html。