1. 程式人生 > >MyBatis-進階

MyBatis-進階

都是 contain brush statement sta sin illegal 實現 lar

接入門的實例,我們知道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)

竟然拋異常啦,去掉任何一個都是可以的。

MyBatis-進階