基于XML +注释的MyBatis配置
是否可以在应用程序中同时使用基于XML + Annotation的MyBatis配置。
我问这个的原因是因为,在我的应用程序中,我使用的是基于注释的方法。 但是在其中一个场景中,我需要使用一个IN子句,它可以使用
基于XML的配置。
但是,当我启动我的应用程序时,它似乎无法识别我的基于注释的映射器并且给我一个Type interface is not known to the MapperRegistry
exceptionType interface is not known to the MapperRegistry
因此,我想知道是否可以在应用程序中同时使用基于XML + Annotation的MyBatis配置。 请建议。
它可以同时具有基于XML + Annotation的配置
对于基于xml的配置:
从您的spring上下文文件开始。在上下文文件中添加以下行
你的mybatis-config.xml应该是这样的:
和src / com / yourcomp / domain /中的userMapper.xml可能是这样的
现在在你的DAO层你可能有类如下:
public class UserDAO{ private SqlSessionFactory sqlSessionFactory; public UserDAO() { } public UserDAO(SqlSessionFactory sqlSessionFactory ) { this.sqlSessionFactory = sqlSessionFactory; } public String getUserById(Integer userId) { SqlSession session = sqlSessionFactory.openSession(); //int success = -100; String name=null; try { name = (String)session.selectOne("com.yourcomp.domain.User.getUserById",userId); }catch(Exception e){ }finally { session.close(); } return name; } }
现在,要使用基于注释的配置,请添加以下内容:
您可能有如下界面:
public interface MyMapper { @Select(value="select something from some_table") public String getSomeValue(@Param("someParam") String someParam); }
现在将以下内容添加到您的上下文文件中:
希望这可以帮助 :)
以下标签也可用于基于注释的方法。
在这种情况下,您不需要两个映射文件。