接着上一篇Mybatis基于xml配置
这一篇讨论的是关于注解配置
一、创建接口StudenetMapper.java
二、写测试类
方法一:我们不用再写studentMapper.xml(可以看到接口studentMapper通过注解写的sql语句和在studentMapper.xml中写的增删改查sql语句是一样的),所以也不用在mybatis-configuration.xml对其进行注册;但是我们在测试代码里要添加一行代码session.getConfiguration().addMapper(StudentMapper.class);进行注册;接口StudentMapper是否需要注册呢?也不需要,否则它就会报如下错误:
方法二(不用写session.getConfiguration().addMapper(StudentMapper.class)):我们可以空写studentMapper.xml,然后在mybatis-configuration.xml进行注册也是可以运行的;值得注意的是此时namespace的值不再是studentMapper,而是Mapper.StudentMapper,对应的是StudentMapper这个接口