机器人william hill官网
直播中

刘一

7年用户 91经验值
私信 关注
[问答]

Mybatis整合spring的思路和步骤

1、整合思路
(1)SqlSessionFactory对象应该放到spring容器中作为单例存在。
(2)传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
(3)Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
(4)数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

回帖(1)

康桃花

2020-11-4 11:27:32
2、整合需要的jar包
(1)spring的jar包
(2)Mybatis的jar包
(3)Spring+mybatis的整合包。
(4)Mysql的数据库驱动jar包。
(5)数据库连接池的jar包。
      

3、整合的步骤
第一步:创建一个java工程。
第二步:导入jar包。(上面提到的jar包)
第三步:mybatis的配置文件sqlmapConfig.xml
第四步:编写Spring的配置文件
  1、数据库连接及连接池
  2、事务管理(暂时可以不配置)
  3、sqlsessionFactory对象,配置到spring容器中
  4、mapper代理对象或者是dao实现类配置到spring容器中。
第五步:编写dao或者mapper文件
第六步:测试。
整合之后的配置文件目录:

配置SqlMapConfig.xml


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">











配置 applicationContext.xml
http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans   
举报

更多回帖

×
20
完善资料,
赚取积分