博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring与mybatis的整合
阅读量:4583 次
发布时间:2019-06-09

本文共 7484 字,大约阅读时间需要 24 分钟。

整合的思路

 

SqlSessionFactory对象放到spring容器中作为单例存在。

传统dao的开发方式中,从spring容器中获得sqlsession对象。

Mapper代理形式中,从spring容器中直接获得mapper的代理对象。

数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

 

整合需要的jar包

要实现spring与mybatis的整合,就要首先导入相关的依赖jar包,如下:

    •   Spring的jar包
    •   Mybatis的jar包
    •   Spring与mybatis整合的jar包
    •   Mysql数据库驱动的jar包
    •   Druid数据库连接池的jar包
    •   Spring与Junit集成测试的jar包(spring-test、Junit)
    •   日志的jar包

 

  具体依赖

4.2.4.RELEASE
org.springframework
spring-context
${spring-version}
org.springframework
spring-core
${spring-version}
org.springframework
spring-beans
${spring-version}
org.springframework
spring-expression
${spring-version}
org.springframework
spring-web
${spring-version}
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-aop
${spring-version}
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aspects
${spring-version}
org.springframework
spring-jdbc
${spring-version}
mysql
mysql-connector-java
5.1.38
org.springframework
spring-tx
${spring-version}
com.alibaba
druid
1.0.18
org.mybatis
mybatis
3.2.7
org.mybatis
mybatis-spring
1.2.2
org.slf4j
slf4j-api
1.7.13
org.slf4j
slf4j-log4j12
1.7.5
log4j
log4j
1.2.16
org.springframework
spring-test
${spring-version}
test
junit
junit
4.9
test
org.apache.maven.plugins
maven-compiler-plugin
1.7
1.7
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8080
/

 

整合的步骤

 

  第一步:创建maven的web工程

  第二步:导入整合需要的jar包

  第三步:mybatis的核心配置文件:sqlMapConfig.xml

 

  第四步:spring的核心配置文件:applicationContext.xml的配置

    •     加载db.properties属性配置文件
    •     配置Druid数据库连接池
    •     将SqlSessionFactory的单例对象,配置到spring容器中
    •     将mapper代理对象或者dao接口的实现类对象,配置到spring容器中

 

      至于将mapper代理对象或者dao接口的实现类对象,配置到spring容器中,将在下面进行配置

 

  第五步:编写dao接口、dao实现类或者mapper接口、mapper映射文件

 传统Dao开发方式

  开发方式:dao接口+dao实现类。

  Dao的实现类需要从spring容器中获取sqlSession对象来操作数据库,spring为我们提供了SqlsessionDaoSupport类,我们只需要继承这个类,就可以顺利的获得sqlSession对象。

总结一句话:dao实现类需要继承SqlsessionDaoSupport类,然后再spring容器中配置这个bean。

1 public interface AccountDao {2     public List
findAll();3 }
1 public class AccountDaoImpl extends SqlSessionDaoSupport implements AccountDao {
//需要继承SqlSessionDaoSupport 2 @Override 3 public List
findAll() { 4 SqlSession sqlSession = getSqlSession(); //继承SqlSessionDaoSupport 后直接调用getSqlSession方法 5 List
selectList = sqlSession.selectList("test.abc"); 6 //整合后sqlSession不需要关闭 7 return selectList; 8 } 9 10 }

      mapper.xml

      applicationContext.xml中配置dao

   

 

      基于mapper代理形式开发Dao

          开发mapper接口及mapper映射文件

1 public interface AccountMapper {2     public List
selectAll();3 }

 

          applicationContext.xml中配置mapper代理

   

 

 

 

  第六步:编写Junit测试代码测试

      传统dao方式开发测试

1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration("classpath:applicationContext.xml") 3 public class SpringMybatisTest { 4     @Autowired 5     private AccountDao accountDao; 6      7     @Test 8     public void testName() throws Exception { 9         List
findAll = accountDao.findAll(); 10 for (Account account : findAll) {11 System.out.println(account);12 }13 }14 }

 

 

      基于mapper代理方式测试

1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration("classpath:applicationContext.xml") 3 public class SpringMybatisTest { 4     @Autowired 5     private AccountMapper accountMapper; 6      7     @Test 8     public void testName() throws Exception { 9         List
list = accountMapper.selectAll(); 10 for (Account account : list) {11 System.out.println(account);12 }13 }14 }

 

转载于:https://www.cnblogs.com/jifengblog/p/9240626.html

你可能感兴趣的文章
mongodb
查看>>
LeetCode 46. Permutations
查看>>
jmeter- 性能测试3:聚合报告(Aggregate Report )
查看>>
JavaScript高级程序设计---学习笔记(二)
查看>>
vim 插件的学习
查看>>
Uncaught SyntaxError: Unexpected token ILLEGAL
查看>>
一个预处理定义的问题
查看>>
ANDROID L——Material Design综合应用(Demo)
查看>>
自我介绍以及关于软件工程的问题
查看>>
struts (一)
查看>>
【新番推荐】工作细胞
查看>>
NYOJ 16 矩形嵌套
查看>>
Leetcode中的SQL题目练习(二)
查看>>
dubbo 集群容错源码
查看>>
Collection接口的子接口——Queue接口
查看>>
LINUX安装NGINX
查看>>
服务器启动项目抛错 没有到主机的路由
查看>>
python_85_sys模块
查看>>
第九周动手动脑
查看>>
HDU 1811 Rank of Tetris
查看>>