Spring和Mybatis的整合,主要借助于Spring的依赖注入和控制反转来简化Mybatis的配置,使用两个配置文件【注:此种配置文件网上已经有很多】:
spring.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 9 10 <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/> 11 <!-- 自动扫描 --> 12 <context:component-scan base-package="com.zhu.test.service"></context:component-scan> 13 </beans>
配置很简单,先使用<context:property-placeholder/>标签引入外部资源文件,再采用包扫描的方式加载com.zhu.test.service包下的所有类,因为其中的业务逻辑类使用了Spring注解,Spring会将其封装成bean供使用。
spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.1.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 15 16 <!-- 配置数据源 --> 17 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 18 <!-- <property name="driverClassName" value="${driver}" /> --> 19 <property name="url" value="${url}" /> 20 <property name="username" value="${username}" /> 21 <property name="password" value="${password}" /> 22 <!-- 初始化时建立物理连接的个数 --> 23 <property name="initialSize" value="0" /> 24 <!-- 最大连接池数量 --> 25 <property name="maxActive" value="20" /> 26 <!--minIdle: 最小空闲连接--> 27 <property name="minIdle" value="0" /> 28 <!-- 获取连接时最大等待时间 --> 29 <property name="maxWait" value="60000" /> 30 <!-- 用来检测连接是否有效的sql,要求是一个查询语句 --> 31 <property name="validationQuery" value="${validationQuery}" /> 32 <!-- 是否申请连接时执行validationQuery检测连接是否有效 --> 33 <property name="testOnBorrow" value="false" /> 34 <property name="testOnReturn" value="false" /> 35 <property name="testWhileIdle" value="true" /> 36 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 37 <property name="minEvictableIdleTimeMillis" value="25200000" /> 38 <property name="removeAbandoned" value="true" /> 39 <property name="removeAbandonedTimeout" value="1800" /> 40 <property name="logAbandoned" value="true" /> 41 <property name="filters" value="mergeStat" /> 42 </bean> 43 44 <!-- <context:annotation-config></context:annotation-config> --> 45 46 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 47 <property name="dataSource" ref="dataSource" /> 48 <property name="mapperLocations" value="classpath:com/zhu/test/mapping/*.xml" /> 49 </bean> 50 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 51 <property name="basePackage" value="com.zhu.test.dao" /> 52 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 53 </bean> 54 55 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 56 <property name="dataSource" ref="dataSource" /> 57 </bean> 58 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 59 <tx:attributes> 60 <tx:method name="add*" propagation="REQUIRED" /> 61 <tx:method name="append*" propagation="REQUIRED" /> 62 <tx:method name="insert*" propagation="REQUIRED" /> 63 <tx:method name="save*" propagation="REQUIRED" /> 64 <tx:method name="update*" propagation="REQUIRED" /> 65 <tx:method name="modify*" propagation="REQUIRED" /> 66 <tx:method name="edit*" propagation="REQUIRED" /> 67 <tx:method name="delete*" propagation="REQUIRED" /> 68 <tx:method name="remove*" propagation="REQUIRED" /> 69 <tx:method name="repair" propagation="REQUIRED" /> 70 <tx:method name="delAndRepair" propagation="REQUIRED" /> 71 72 <tx:method name="get*" propagation="SUPPORTS" /> 73 <tx:method name="find*" propagation="SUPPORTS" /> 74 <tx:method name="load*" propagation="SUPPORTS" /> 75 <tx:method name="search*" propagation="SUPPORTS" /> 76 <tx:method name="datagrid*" propagation="SUPPORTS" /> 77 78 <tx:method name="*" propagation="SUPPORTS" /> 79 </tx:attributes> 80 </tx:advice> 81 <aop:config> 82 <aop:pointcut id="transactionPointcut" expression="execution(* com.zhu.test.service..*Impl.*(..))" /> 83 <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> 84 </aop:config> 85 86 </beans>
该文件所做的工作是配置druid数据源,mybatis的Mapper扫描,扫描指定包下的所有mapper,如此便不用逐条加载mapper了,然后是Spring的事务管理。
不过,在配置Spring和Mybatis整合时出现了一些问题,记录之:
①一直报错
说白了,就是没找到com.zhu.test.dao包,这个问题折腾了一天多时间,百思不得解,最后无奈从别处复制粘贴,改一下路径和包名,结果就奇迹般的好了,最后终于找到了问题所在,截图为鉴【下次直接全部替换就不会有这样的问题了】:
②
1 [com.alibaba.druid.pool.DruidDataSource] - create connection error 2 java.sql.SQLException: Access denied for user ‘Dada‘@‘localhost‘ (using password: YES) 3 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) 4 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) 5 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) 6 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) 7 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694) 8 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
很显然,该错误说明没使用资源文件中的配置连接数据库,相反却使用了本机名不带密码访问数据库,关键在于spring.xml文件中的这句<context:property-placeholder location="classpath:jdbc.properties"/>,<context:property-placeholder />有一个system-properties-mode属性,默认为ENVIRONMENT,会先去系统变量中寻找,修改之<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>,值NEVER表示不去寻找系统变量中的值,问题便解决了【参考自http://www.oschina.net/question/873438_234580】。
另外,关于<context:property-placeholder />:
1【来自http://blog.csdn.net/ws_blog/article/details/46986051】 <context:property-placeholder 2 location="属性文件,多个之间逗号分隔" 3 file-encoding="文件编码" 4 ignore-resource-not-found="是否忽略找不到的属性文件" 5 ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常" 6 properties-ref="本地Properties配置" 7 local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反" 8 system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,O VERRIDE类似于ENVIRONMENT" 9 order="顺序" 10/>
另外附上比较详细的讲解,参考自http://blog.csdn.net/Rickesy/article/details/50791534