本课主要完成MyBatis与Spring框架的集成。
配置数据库文件
在创建MyBatis配置文件之前,我们先配置MySQL数据库配置文件。在eshop项目src目录config.properties包下,新建jdbc.properties配置文件。
jdbc.properties配置文件内容如下: jdbc.username=root jdbc.password=~123456q jdbc.url=jdbc:mysql://192.168.62.90:3306/shop?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.initSize=2 jdbc.maxSize=20 jdbc.minSize=2 jdbc.maxIdle=1 jdbc.maxIdleTime=14400000 jdbc.timeBetweenEvictionRunsMillis=60000 jdbc.poolPreparedStatements=true jdbc.maxOpenPreparedStatements=50 jdbc.removeAbandonedTimeout=180 jdbc.testOnBorrow = false jdbc.testOnReturn = false jdbc.testWhileIdle = true jdbc.validationQuery=select current_timestamp()
注意:配置文件的数据库访问路径与数据库部署有关,请根据数据库部署情况修改。
下面是几个重要配置项的说明:
jdbc.username设置数据库访问账号,一般使用root账号。
jdbc.password设置root账号登录数据库的密码。
jdbc.url设置数据库的访问路径、数据库名、数据库编码方式。allowMultiQueries设置为true,允许同时执行多条SQL语句。
jdbc.driverClassName设置JDBC驱动库。
jdbc.initSize设置初始化时提供的连接数。
jdbc.maxSize设置最大连接数。
jdbc.minSize设置最小连接数。
MyBatis与Spring框架集成依赖包的配置
MyBatis与Spring框架集成需要下面的依赖包:
commons-dbcp2-2.6.0.jar
commons-pool2-2.6.2.jar
mybatis-spring-2.0.1.jar
commons-dbcp2是一个依赖Jakarta commons-pool对象池机制的数据库连接池,Tomcat的数据源使用的就是DBCP。commons-dbcp2-2.6.0.jar包的下载地址为:
http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
下载的文件是压缩包,解压缩后将commons-dbcp2-2.6.0.jar或更高版本复制到eshop项目WEB-INF目录下的lib目录。
commons-pool2是一个开源的公共资源池,commons-dbcp2依赖commons-pool2建立数据库连接池。commons-pool2-2.6.2.jar包的下载地址为:
http://commons.apache.org/proper/commons-pool/download_pool.cgi
下载的文件是压缩包,解压缩后将commons-pool2-2.6.2.jar或更高版本复制到eshop项目WEB-INF目录下的lib目录。
mybatis-spring是MyBatis与Spring框架的集成包。mybatis-spring-2.0.1.jar包下载地址为:
https://repo1.maven.org/maven2/org/mybatis/mybatis-spring/2.0.1/mybatis-spring-2.0.1.jar
下载后复制到eshop项目WEB-INF目录下的lib目录。
配置MyBatis与Spring框架集成配置文件
数据库配置文件和集成依赖包配置完成后,就可以开始配置MyBatis与Spring框架集成配置文件了。在eshop项目src目录config. spring_config包下,新建database.xml。配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:\config\properties\jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxIdle" value="${jdbc.maxSize}"/>
<property name="minIdle" value="${jdbc.minSize}"/>
<property name="initialSize" value="${jdbc.initSize}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}"/>
<property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}"/>
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnBorrow" value="${jdbc.testOnBorrow}"/>
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnReturn" value="${jdbc.testOnReturn}"/>
<!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于
timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->
<property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
<!-- 用来检测连接是否有效的sql,要求是一个查询语句,如果validationQuery为
null,testOnBorrow、testOnReturn、testWhileIdle都不起其作用。 -->
<property name="validationQuery" value="${jdbc.validationQuery}"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/**/*Mapper.xml"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="txManage"/>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.eshop.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>Spring利用propertyConfigurer类 可以读取property数据库配置文件,它将配置文件的key/value键值对转换为${key}形式,在配置文件中可以使用${key}来访问对应的键值。
SqlSessionFactoryBean是MyBatis和Spring框架的集成类,它实现了 Spring 的 FactoryBean 接口。SqlSessionFactory 有一个单独的必须属性,就是 JDBC 的 DataSource,用于连接数据源。mapperLocations 属性用来指定 MyBatis 的 XML映射文件的位置。在上面的配置文件中,SqlSessionFactoryBean类会扫描在 com包下和它的子包中所有的后缀是Mapper.xml的XML映射文件。
MapperScannerConfigurer扫描所有的DAO接口,basePackage给出了扫描路径。
配置文件配置完毕后,需要将该配置文放置到Spring框架的配置文件中,server启动时会加载该配置文件。打开Spring框架的applicationContext.xml配置文件,在<beans>标签之间加入下面的内容:
<import resource="database.xml"/>
至此,MyBatis与Spring框架已集成完毕,重新构建项目,启动server,如果server启动成功则说明集成成功。如果启动失败,可尝试重启eclispe,再次重新构建项目。如果还有问题,可查看控制台给出的错误信息逐一解决。