一開始是這個錯誤:
[org.springframework.context.support.GenericApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cn.hnust.dao.IUserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserDao' defined in file [E:\Users\Wang\workspace\ssm\target\classes\com\cn\hnust\dao\IUserDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'initialSize'; nested exception is java.lang.NumberFormatException: For input string: "0????"
這個是因為配置文件:本來是整型的爵憎,結(jié)果變成string類型艘蹋,導(dǎo)致轉(zhuǎn)換錯誤针炉。所以把空格去掉就好
出現(xiàn)錯誤:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.? Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
### The error may exist in file [E:\Users\Wang\workspace\Test\target\classes\com\cn\hnust\mapping\UserMapper.xml]
### The error may involve com.cn.hnust.dao.IUserDao.selectByPrimaryKey
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy19.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at com.sun.proxy.$Proxy20.selectByPrimaryKey(Unknown Source)
at com.cn.hnust.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:18)
at org.zsl.testmybatis.TestMyBatis.test1(TestMyBatis.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source
這個問題我問了好多人,都說是mybatis配置錯誤译打。上面的錯誤是:
1.因為localhost或127.0.0.1寫成其他的地址了耗拓,2.模式寫錯:db_zs1這個后面是1而不是L,3.username是數(shù)據(jù)庫用戶的名稱奏司,我寫成了表名乔询,連接的時候并沒有用上表名,只有查詢的時候才用得上韵洋。
java連接mysql數(shù)據(jù)庫的語句是:協(xié)議:子協(xié)議:數(shù)據(jù)源標(biāo)識
協(xié)議:在JDBC中總是以jdbc開始
子協(xié)議:是橋連接的驅(qū)動程序或是數(shù)據(jù)庫管理系統(tǒng)名稱竿刁。
數(shù)據(jù)源標(biāo)識:標(biāo)記找到數(shù)據(jù)庫來源的地址與連接端口。
例如:(MySql的連接URL)
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字符集搪缨。如果characterEncoding設(shè)置為
gb2312或GBK食拜,本參數(shù)必須設(shè)置為true 。characterEncoding=gbk:字符編碼方式
正確的代碼:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db_zs1
username=root
password=123456
我修改了之后就有另外的問題了:其實我只是修改了127.0.0.1IP地址和root副编,然后提示下面的錯誤:我才知道原來數(shù)據(jù)庫用戶名稱寫錯8旱椤!痹届!
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.? Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'db_zsl')
### The error may exist in file [E:\Users\Wang\workspace\Test\target\classes\com\cn\hnust\mapping\UserMapper.xml]
### The error may involve com.cn.hnust.dao.IUserDao.selectByPrimaryKey
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'db_zsl')
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy19.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at com.sun.proxy.$Proxy20.selectByPrimaryKey(Unknown Source)
at com.cn.hnust.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:19)
at org.zsl.testmybatis.TestMyBatis.test1(TestMyBatis.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:4