Spring与Mybatis配置问题
Spring和Mybatis的整合,主要借助于Spring的依赖注入和控制反转来简化Mybatis的配置,使用两个配置文件【注:此种配置文件网上已经有很多】:
spring.xml:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
<!-- 自动扫描 -->
<context:component-scan base-package="com.zhu.test.service"></context:component-scan>
</beans>
配置很简单,先使用<context:property-placeholder/>标签引入外部资源文件,再采用包扫描的方式加载com.zhu.test.service包下的所有类,因为其中的业务逻辑类使用了Spring注解,Spring会将其封装成bean供使用。
spring-mybatis.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- <property name="driverClassName" value="${driver}" /> -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化时建立物理连接的个数 -->
<property name="initialSize" value="0" />
<!-- 最大连接池数量 -->
<property name="maxActive" value="20" />
<!--minIdle: 最小空闲连接-->
<property name="minIdle" value="0" />
<!-- 获取连接时最大等待时间 -->
<property name="maxWait" value="60000" />
<!-- 用来检测连接是否有效的sql,要求是一个查询语句 -->
<property name="validationQuery" value="${validationQuery}" />
<!-- 是否申请连接时执行validationQuery检测连接是否有效 -->
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="mergeStat" />
</bean> <!-- <context:annotation-config></context:annotation-config> --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/zhu/test/mapping/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhu.test.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.zhu.test.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config> </beans>
该文件所做的工作是配置druid数据源,mybatis的Mapper扫描,扫描指定包下的所有mapper,如此便不用逐条加载mapper了,然后是Spring的事务管理。
不过,在配置Spring和Mybatis整合时出现了一些问题,记录之:
①一直报错

说白了,就是没找到com.zhu.test.dao包,这个问题折腾了一天多时间,百思不得解,最后无奈从别处复制粘贴,改一下路径和包名,结果就奇迹般的好了,最后终于找到了问题所在,截图为鉴【下次直接全部替换就不会有这样的问题了】:

②
[com.alibaba.druid.pool.DruidDataSource] - create connection error
java.sql.SQLException: Access denied for user 'Dada'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
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
location="属性文件,多个之间逗号分隔"
file-encoding="文件编码"
ignore-resource-not-found="是否忽略找不到的属性文件"
ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常"
properties-ref="本地Properties配置"
local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反"
system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,O VERRIDE类似于ENVIRONMENT"
order="顺序"
/>
另外附上比较详细的讲解,参考自http://blog.csdn.net/Rickesy/article/details/50791534
Spring与Mybatis配置问题的更多相关文章
- spring结合Mybatis的框架搭建(一)
一:前沿 2015年新年上班的第二天,第一天就打了一天的酱油哦,只是下午开始搭建自己毕业设计的框架,搭建的是spring+spring mvc+MyBatis的框架.今天遇到了一个问题,结果弄了我一天 ...
- Spring boot+Mybatis+MySQL插入中文乱码
转载:https://www.jianshu.com/p/bd0311a33c16 现象: 搭建spring boot+mybatis+mysql时出现插入mysql的中文出现乱码???. mys ...
- spring整合mybatis使用<context:property-placeholder>时的坑
背景 最近项目要上线,需要开发一个数据迁移程序.程序的主要功能就是将一个数据库里的数据,查询出来经过一系列处理后导入另一个数据库.考虑到开发的方便快捷.自然想到用spring和mybatis整合一下. ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境【转】
项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...
- 基于Spring+SpringMVC+Mybatis的Web系统搭建
系统搭建的配置大同小异,本文在前人的基础上做了些许的改动,重写数据库,增加依据权限的动态菜单的实现,也增加了后台返回json格式数据的配置,详细参见完整源码. 主要的后端架构:Spring+Sprin ...
- Spring和Mybatis整合,配置文件
整合时除了未整合前spring.mybatis的必须包外还需要加入两个包 spring-jdbc-4.2.5.RELEASE.jar mybatis-spring-1.2.5.jar spring-j ...
- IDEA中maven搭建Spring+SpringMVC+mybatis项目
一.介绍 使用IDEA搭建maven web项目,整合框架Spring+SpringMVC+mybatis 项目结构图:
- 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(三)
Spring+SpringMVC MVC呢,现在似乎越来越流行使用SpringMVC框架,我自己用的感觉,是非常好,确实很舒服,配置一开始是麻烦了一点点,但是后续的开发真的是很清爽! SpringMV ...
随机推荐
- SQL子句执行顺序和Join的一点总结
SQL子句执行顺序和Join的一点总结 FROM ON JOIN WHERE GROUP BY WITH CUBE or WITH ROLLUP HAVING SELECT DISTINCT ORDE ...
- Spring-----6、Spring3.0提供的Java配置管理
转载自:http://blog.csdn.net/hekewangzi/article/details/45646279
- 转载 Eclipse下的SSH开发例子
前言 确实好久没有写过了,一直以来把写博文当作自己学习的总结,当作做过的笔记,随时都可以拿出来看看.不过最近习惯了用OneNote和印象笔记,所以就很少在论坛写博文.但是偶尔看到几篇被转载了,也小小的 ...
- auto 和 decltype (C++11 新增)
红色字体为个人推断,可信度自辨. 蓝色字体为重点. auto类型说明符:使用auto时,编译器会分析表达式,并自动推算出变量所属类型.*auto变量必须有初值 原理:编译器通过 初值 来判断auto变 ...
- CDZSC_2015寒假新人(1)——基础 d
Description These days, I am thinking about a question, how can I get a problem as easy as A+B? It i ...
- WINFORM窗体里使用网页控件的一些办法
最近弄一个项目,是个CS的.当然也是有表单之类的,如果将HTML表单搬到窗体上就省事多了. .首先要使用一个控件 WebBrowser 载入页面没使用URL属性,使用了下面这个属性 this.webB ...
- 如何看linux是32位还是64位
查看linux是多少位的几位方法: 查看linux机器是32位还是64位的方法: 方法一: file /sbin/init 或者 file /bin/ls 结果如下:/sbin/init: ELF ...
- iOS项目生成通用Windows应用
WinObjc - 使用iOS项目生成通用Windows应用 Github上一周年的WinObjc项目最近发布了预览版本,终于等到了这一天.WinObjc项目就是Build 2015大会上微软宣布 ...
- 构建混合云:配置Azure site to site VPN连接(3)
9. 那么我们来创建网关,创建网关的时候需要注意,看看你的设备是否支持动态网关,在本示例中的Cisco ASA 5550不支持动态网关,所以我们只能创建静态网关: 该创建会花费一定的时间,稍等即可. ...
- C# Chart 折线图 多条数据展示
private void btn_Click(object sender, EventArgs e) { DBHelper db = new DBHelper(); DataSet ds = db.G ...