在做spring与mybatis整合的时候遇到如下错误: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))大概意思是数据库的用户名或者密码不正确。但是用SQLyog工具又可以登录,说明用户名和密码都是正确的。 jdbc.properties文件: driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/testdb username=root password=1234 EmployeeMapper.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "http:///dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.vd.dao.EmployeeMapper"> <!-- 查询 --> <select id="getEmployeeList" resultType="Employee"> select * from employee </select> <!-- 增加 --> <insert id="add" parameterType="Employee"> insert into employee (sn,name,gender) values (#{sn},#{name},#{gender}) </insert> <!-- 删除 --> <delete id="delete" parameterType="Employee"> delete from employee where id=#{id} </delete> <!-- 修改 --> <delete id="update" parameterType="Employee"> update employee set sn=#{sn},name=#{name},gender=#{gender} where id=#{id} </delete> </mapper> mybatis-config.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "http:///dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="cn.vd.pojo" /> </typeAliases> </configuration> 下面是applicationContext-mybatis.xml文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:aop="http://www./schema/aop" xmlns:p="http://www./schema/p" xmlns:tx="http://www./schema/tx" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.2.xsd http://www./schema/aop http://www./schema/aop/spring-aop-3.2.xsd http://www./schema/tx http://www./schema/tx/spring-tx-3.2.xsd http://www./schema/context http://www./schema/context/spring-context-3.2.xsd"> <!-- .properties文件读取配置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- JNDI获取数据源(dbcp连接池) commons-dbcp.jar下org.apache.commons.dbcp.BasicDataSource.class--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton"> <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </bean> <!-- 事务管理 spring-jdbc-3.2.13.RELEASE.jar下org.springframework.jdbc.datasource.DataSourceTransactionManager 2种方式调用事务1、注解 @Transactional 2、AOP --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置mybatis sqlSessionFactory 属性注入 mybatis-spring-1.2.0.jar下org.mybatis.spring.SqlSessionFactoryBean--> <bean id="sqlSessionFactory" class=" org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:cn/vd/dao/*.xml"/> </bean> <!-- 配置SqlSessionTemplate 属性注入 mybatis-spring-1.2.0.jar下org.mybatis.spring.SqlSessionTemplate--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 构造器注入 --> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- 把SqlSessionTemplate注入到业务实现,这里是测试类 --> <bean id="employeeDaoTest" class="cn.vd.test.EmployeeDaoTest"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> </beans> 下面是jar包: mybatis的jar用mybatis单独测试通过,spring也单独测试通过。 没问题吧! 测试类: public class EmployeeDaoTest { Logger logger=Logger.getLogger(EmployeeDaoTest.class); private SqlSessionTemplate sqlSessionTemplate; private ApplicationContext ctx=null; private EmployeeDaoTest eDaoTest; @Test public void getEmployeeTest() { ctx=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml"); eDaoTest=(EmployeeDaoTest) ctx.getBean("employeeDaoTest"); List<Employee> empList=eDaoTest.sqlSessionTemplate.selectList("cn.vd.dao.EmployeeMapper.getEmployeeList"); for (Employee emp : empList) { logger.debug("id:"+emp.getId()+"---"+"name:"+emp.getName()); } } @Test public void add() { ctx=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml"); eDaoTest=(EmployeeDaoTest) ctx.getBean("employeeDaoTest"); Employee emp=new Employee(); emp.setSn("LAOYING"); emp.setName("老鹰"); emp.setGender("女"); eDaoTest.sqlSessionTemplate.insert("cn.vd.dao.EmployeeMapper.add", emp); } } 再看看下面: http://www./schema/beans/spring-beans-3.2.xsd http://www./schema/aop http://www./schema/aop/spring-aop-3.2.xsd http://www./schema/tx http://www./schema/tx/spring-tx-3.2.xsd http://www./schema/context http://www./schema/context/spring-context-3.2.xsd"> 和spring的jar包版本好像也没什么问题。为了这个问题我头疼了一天,最终还是怀疑这里的版本和jar的版本不匹配。修改如下: http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/aop http://www./schema/aop/spring-aop-3.0.xsd http://www./schema/tx http://www./schema/tx/spring-tx-3.0.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd"> 尼玛,居然测试通过了,过了,了。的确是版本不匹配。
|
|