Mybatis和Spring整合(原始Dao)非mapper代理

如果想看看没和spring整合的Mybatis非mapper代理配置,请移驾^_^


一、选择jar包,搭建环境


1、mybatis3.2.7jar

2、spring3.2.0jar

3、mybatisspring的整合包:mybatisspring整合由mybatis提供。


Mybatis和Spring整合(原始Dao)非mapper代理

Mybatis和Spring整合(原始Dao)非mapper代理

Mybatis和Spring整合(原始Dao)非mapper代理

Mybatis和Spring整合(原始Dao)非mapper代理


二、sqlSessionFactory

  说明:applicationContext.xml配置sqlSessionFactory和数据源sqlSessionFactorymybatisspring的整合包下。


Mybatis和Spring整合(原始Dao)非mapper代理

如果有多个数据源,用sqlSessionFactoryBean

Mybatis和Spring整合(原始Dao)非mapper代理


三、在classpath下的sqlmap目录下创建sql映射文件Users.xml:

Mybatis和Spring整合(原始Dao)非mapper代理


四、加载映射文件,SqlMapconfig.xml中加载User.xml

Mybatis和Spring整合(原始Dao)非mapper代理


五、准备DAO层执行SQL(实现类继承SqlSessionDaoSupport

说明:SqlSessionDaoSupport是整合包中的类,

dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。

Mybatis和Spring整合(原始Dao)非mapper代理


六、这里spring声明配置方式,配置daobean

Mybatis和Spring整合(原始Dao)非mapper代理


七、测试一下

Mybatis和Spring整合(原始Dao)非mapper代理