我们只需要编写Dao接口并且按照mybatis要求编写两个配置文件—IUserDao.xml和SqlMapConfig.xml,就可以实现Mybatis的开发。这远比用jdbc编写来实现功能方便多了(使用注解之后,将变得更为简单,只需要编写一个mybatis配置文件就够了)。
所以本文我们来讲解基于注解的Mybatis开发
- 移除xml的映射配置(IUserDao.xml)
- 在持久层接口中添加注解
public interface IUserDao {
@Select("select *from user")
List<User> findAll();
}
- 修改SqlMapConfig.xml
<!-- 告知mybatis映射配置的位置 -->
<mappers>
<mapper class="com.review.dao.IUserDao"/>
</mappers>
更多Mybatis注解高级用法见:https://blog.csdn.net/weixin_43514899/article/details/107865739