接入门的实例,我们知道MyBatis可以使用注解和配置文件实现接口和sql语句的绑定。
那么一个接口方法同时使用注解和xml配置会怎么样。
@Select("select * from user_tb where id=#{id}") User getOneUser(int id);
<select id="getOneUser" resultType="User"> select * from user_tb where id+1=#{id} </select>
如果传入id=12,查出来的User.id=12,说明注解覆盖xml配置,查出来的User.id=11,说明xml配置覆盖注解
结果是:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in com/xh/mybatisLearn/dao/UserMapper.java (best guess) ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.xh.mybatisLearn.dao.UserMapper.getOneUser at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64) at com.xh.mybatisLearn.Test.getSqlSessionFactory(Test.java:29) at com.xh.mybatisLearn.Test.main(Test.java:34)
竟然抛异常啦,去掉任何一个都是可以的。