MyBatis整合Spring相关错误

来源:互联网 发布:淘宝直播 宝贝讲解 编辑:程序博客网 时间:2024/06/05 08:24

在配置MyBatis+Spring+Mysql过程中真是出现各种问题,多数由于粗心导致

错误1:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.

MySQl服务没有打开


错误2:

Error creating bean with name 'newsDao' defined in file [D:\WebTest\linglee\target\classes\com\linglee\dao\NewsDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': ...

配置文件application.properties中路径mybatis.config-location的值classpath:mybatis-config.xml多加了一个斜杠。


错误3:org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction;

按照网上说的各种调试,依旧行不通,最后多次检查配置文件application.properties,才发现自己把spring.datasource.username写成spring.datasource.user了,无语


错误4:Cannot read SQL script from class path resource [init-schema.sql]

在InitDatabaseTests test测试中有注解@Sql("/init-schema.sql")错误表名因路径问题无法读取该文件,解决该sql文件要放在main中的resource目录下,不是test

错误5:

Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '' for key 'name',关键字name的唯一性没有保持,但是我在插入时统一setName(""),改成setName(string.format("USER%d",i));或者唯一即可


插入成功

原创粉丝点击