Spring整合Mybatis
思路:将Mybatis的SqlSessionFactory交给Spring。
SM整合步骤:
1.jar

2.类-表
3.mybatis配置文件conf.xml
4.通过mapper.xml将类、表建立映射关系
5.spring管理SqlSessionFactory
配置spring配置文件:applicationContext.xml
6.使用Spring-MyBatis整合产物开发程序
目标:通过Spring产生Mybatis最终操作需要的动态mapper对象。
(1)DAO层实现类,继承SqlSessionDaoSupport类,该类提供了一个属性SqlSession
(2)省略第一种方式中的实现类
直接使用MyBatis提供的实现类org.mybatis.spring.mapper.MapperFactoryBean。
(3)批量处理:批量配置实现类
注意:批量产生mapper对在ioc中的id值默认为接口名,接口名=id,接口名首字母小写
注解形式的依赖注入
//实现bean
@Service("studentService")
public class StudentServiceImpl implements IStudentService {
//实现ioc中的注入,自动装配,默认byType
@Autowired
//根据name来注入
@Qualifier("studao")
IStudentDao studentDao;
public IStudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
}
来源:oschina
链接:https://my.oschina.net/u/4261619/blog/4321402