ssm基本整合中的數(shù)據(jù)庫連接錯誤

一開始是這個錯誤:

[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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末呻待,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子队腐,更是在濱河造成了極大的恐慌蚕捉,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柴淘,死亡現(xiàn)場離奇詭異迫淹,居然都是意外死亡,警方通過查閱死者的電腦和手機悠就,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門千绪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來充易,“玉大人梗脾,你說我怎么就攤上這事№镅ィ” “怎么了炸茧?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵瑞妇,是天一觀的道長。 經(jīng)常有香客問我梭冠,道長辕狰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任控漠,我火速辦了婚禮蔓倍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘盐捷。我一直安慰自己偶翅,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布碉渡。 她就那樣靜靜地躺著聚谁,像睡著了一般。 火紅的嫁衣襯著肌膚如雪滞诺。 梳的紋絲不亂的頭發(fā)上形导,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音习霹,去河邊找鬼朵耕。 笑死,一個胖子當(dāng)著我的面吹牛淋叶,可吹牛的內(nèi)容都是我干的憔披。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼爸吮,長吁一口氣:“原來是場噩夢啊……” “哼芬膝!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起形娇,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤锰霜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后桐早,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體癣缅,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年哄酝,在試婚紗的時候發(fā)現(xiàn)自己被綠了友存。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡陶衅,死狀恐怖屡立,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情搀军,我是刑警寧澤膨俐,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布勇皇,位于F島的核電站,受9級特大地震影響焚刺,放射性物質(zhì)發(fā)生泄漏敛摘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一乳愉、第九天 我趴在偏房一處隱蔽的房頂上張望兄淫。 院中可真熱鬧,春花似錦蔓姚、人聲如沸拖叙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽薯鳍。三九已至,卻和暖如春挨措,著一層夾襖步出監(jiān)牢的瞬間挖滤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工浅役, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留斩松,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓觉既,卻偏偏與公主長得像惧盹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子瞪讼,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容