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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/158666.html
標籤:Java
