网赌平台哪个信誉好ssm基本整合中的数据库连接错误

 4、补充一下原来遇到的坑:Spring中的事务失效,事务不回滚原因

  正常情况就是注解扫描的问题,

  原来失效情况是这样的:

  spring中的applicationContext.xml中的扫描 

 <context:component-scan base-package="com.ssm.*">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
 </context:component-scan>

 这里扫描了service接口以及实现,但是springmvc扫描又把service接口和实现扫描了一遍,导致了springmvc接管了service接口,正常的访问和其他操作都没问题,当需要事务回滚就不好用了。   
 总结: 在这里建议大家在spring xml文件配置中需要注意扫描的范围. 
   1、spring负责扫描全局范围的注解   
    2、mybatis注解扫描 dao的包    
   3、springmvc需要扫描 controller 
  千万不要扩大范围,很容易带来一些费解的错误.

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

3、Spring @Autowired注入名称正常是任意的命名,测试如下:

  spring自动对应找到了,service接口的实现类

     
  网赌平台哪个信誉好 1

     
  网赌平台哪个信誉好 2

 

   有人可能怀疑是 注解的问题@Autowired
和@Resource不一样,测试过,是一样的!!具体这两个注解的区别大家可以参考
:

   
https://www.cnblogs.com/smileLuckBoy/p/5801678.html

我修改了之后就有另外的问题了:其实我只是修改了127.0.0.1IP地址和root,然后提示下面的错误:我才知道原来数据库用户名称写错!!!

1、Spring中的applicationContext.xml配置错误导致的异常

  异常信息:

    org.apache.ibatis.binding.BindingException:
Invalid bound statement (not found):
com.ssm.service.BTestService.getPhoneKey
    at
org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:223)
    at
org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
    at
org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59)
    at
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    at
com.sun.proxy.$Proxy17.getPhoneKey(Unknown Source)

  如果是service接口调用出现此错误信息,注意这里不是myabtis接口mapper.java调用出现了此错误

  导致异常的错误配置如下:

  注意不要配置成 
com.ssm.dao.*
这种配置方式扫描的是dao下的子包,最好配置为精准扫描包

  网赌平台哪个信誉好 3

  为什么
说这里被mybatis重复扫描了呢?如图所示这里的 service在debug下查看是org.apache.ibatis.binding.MapperProxy@******

  网赌平台哪个信誉好 4

  如果配置扫描是这种com.ssm.dao.*,会报如下错误

  Caused by:
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.ssm.dao.AppUserPhonekeyMapper
        com.ssm.service.BTestServiceImpl.appUserPhonekeyMapper;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type   [com.ssm.dao.AppUserPhonekeyMapper] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531)
at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295)

网赌平台哪个信誉好 5

 2、以为是Spring中bean命名导致的错误

  异常信息如下:

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name ‘beanTestController’: Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.ssm.service.TBestService
com.ssm.controller.BeanTestController.tBestService; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.ssm.service.TBestService] is defined:
expected single matching bean but found 2:
TBestServiceImpl,TBestService

Caused by:
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.ssm.service.TBestService
com.ssm.controller.BeanTestController.tBestService; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.ssm.service.TBestService] is defined:
expected single matching bean but found 2:
TBestServiceImpl,TBestService
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531)

Caused by:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.ssm.service.TBestService] is defined:
expected single matching bean but found 2:
TBestServiceImpl,TBestService

  配置如下:

           
 网赌平台哪个信誉好 6

    这里的spring核心配置文件中的mybatis扫描配置还是最大范围的,包含了service接口,这里controller中配置如下

           
 网赌平台哪个信誉好 7

    注意:上面的异常是不能找到注入的名称,命名规则如下(myabtis应该是这样的!,spring中@Autowired注入名称正常是任意的命名),如果controller写成了 
 

网赌平台哪个信誉好,         private TBestService
TBestService  这种写法启动不报错,但是这时候的TBestService
在DEBUG模式下查看还是第一种里面的MapperProxy
,运行时还是会报第一种错

         
  网赌平台哪个信誉好 8

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

gb2312或GBK,本参数必须设置为true 。characterEncoding=gbk:字符编码方式

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

出现错误:

1.因为localhost或127.0.0.1写成其他的地址了,2.模式写错:db_zs1这个后面是1而不是L,3.username是数据库用户的名称,我写成了表名,连接的时候并没有用上表名,只有查询的时候才用得上。

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

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

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图