关于Sping与MyBatis的整合,通过Spring实例化Bean,然后调用实例对象中的查询方法来执行MyBatis映射文件中的SQL语句,如果能够正确查询出数据库中的数据,我们就认为已经整合好,具体实现流程见下图
第一步,src目录下创建db.properties文件,配置数据库的基本四项
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=root jdbc.maxTotal=30 jdbc.maxIdle=10 <!--最大空闲数--> jdbc.initialSize=5 <!--初始化连接数-->
第二步,src目录下创建Spring的配置文件 ,applicationContext.xml
- 读取db.properties文件的配置
- 配置数据源
- 配置事务管理器并开启了事务注解
- 配置MyBatis工厂与Spring整合
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!--读取db.properties --> <context:property-placeholder location="classpath:db.properties"/> <!