美文网首页
ssm基本整合中的数据库连接错误

ssm基本整合中的数据库连接错误

作者: Kitlen | 来源:发表于2017-05-12 13:17 被阅读0次

一开始是这个错误:

[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????"

这个是因为配置文件:本来是整型的,结果变成string类型,导致转换错误。所以把空格去掉就好

出现错误:

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是数据库用户的名称,我写成了表名,连接的时候并没有用上表名,只有查询的时候才用得上。

java连接mysql数据库的语句是:协议:子协议:数据源标识

协议:在JDBC中总是以jdbc开始

子协议:是桥连接的驱动程序或是数据库管理系统名称。

数据源标识:标记找到数据库来源的地址与连接端口。

例如:(MySql的连接URL)

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk ;

useUnicode=true:表示使用Unicode字符集。如果characterEncoding设置为

gb2312或GBK,本参数必须设置为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,然后提示下面的错误:我才知道原来数据库用户名称写错!!!

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

相关文章

  • ssm基本整合中的数据库连接错误

    一开始是这个错误: [org.springframework.context.support.GenericApp...

  • SSM整合 基础篇

    SSM整合 需求:通过SSM整合做出对前端基本查询的jsp响应 1 数据库数据 数据库数据如下 创建父工程ssm_...

  • java SSM框架整合实战

    这次的ssm整合比较详细,会从单独的mybatis连接数据库开始,基本会分为以下几个步骤: 1.单独使用mybat...

  • org.springframework.jdbc.Uncateg

    ssm连接数据库错误 使用工具:eclipse 数据库:mysql 报错信息:org.springframewor...

  • org.springframework.jdbc.Uncateg

    ssm连接数据库错误 使用工具:eclipse 数据库:mysql 报错信息:org.springframewor...

  • SSM整合

    SSM整合 目标 熟练整合SSM 使用SSM环境进行增删改查 项目结构 一、创建数据库 CREATE DATABA...

  • SSM整合多数据源

    Spring+SpringMVC+MyBaits 整合多数据源。数据库:MySQL+Mariadb。SSM基本配置...

  • SSM框架-整合

    SSM整合 搭建整合环境 1. 搭建整合环境 创建相应的数据库 创建Maven工程并导入相应坐标创建ssm_par...

  • SSM基本整合

    工程结构如图所示: 1.Spring系列配置文件 spring-core.xml spring-mvc.xml s...

  • 【Spring Boot】简单实践

    lombok 使用Spring Boot整合SSM工程;需要使用到数据库数据。 将数据库表数据导入到数据库中(sp...

网友评论

      本文标题:ssm基本整合中的数据库连接错误

      本文链接:https://www.haomeiwen.com/subject/dfvztxtx.html