首先应确认在porn文件中添加jdbc连接和mysql依赖并导入依赖
其次确认配置文件中正确配置了数据库url,username,password,Driver
以下是可能遇到的问题:
1,不允许公共密钥检索
There was an unexpected error (type=Internal Server Error, status=500).
Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
解决办法:在配置文件的url地址中加上
allowPublicKeyRetrieval=true 并用&连接
(实际上后来发现不加这个也可以)
2,用户'root'@'localhost'的访问被拒绝
There was an unexpected error (type=Internal Server Error, status=500).
Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
解决办法:mysql数据库密码不对,这里需要的是MYSQL数据库的密码,默认用户名root,如果自己设置过密码,就要找到对应密码(我是自己修改过,所以不是root)
3,未知数据库“test”
There was an unexpected error (type=Internal Server Error, status=500).
Failed to obtain JDBC Connection; nested exception is java.sql.SQLSyntaxErrorException: Unknown database 'test'
解决办法:说明test可能没有创建,替换为创建好的库
可使用show databases;命令查看,也可以登录WorkBench查看。
4,SQL语法错误
StatementCallback; bad SQL grammar [CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) NOT NULL, `user_password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) )NEGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;]; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NEGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1' at line 6
解决办法:仔细检查SQL语句,可能是单词,空格,分号,括号,引号,顺序等原因。
比如上面错误信息中可以看出:是在第6行'NEGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1'这一段中有错误,那就仔细检查,错误是第一个单词拼错了,正确的拼写为ENGINE
5,没找到结果
Incorrect result size: expected 1, actual 0
解决办法:在方法中捕捉异常
6,无法识别时区
nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解决办法:在配置文件的url中加入
serverTimezone=UTC 并用&连接
7,创建名称为'userController'的bean时出错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [E:\IdeaProjects\MyBatisDemo\target\classes\com\sgy\demo\dao\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory';
解决办法:一般是复制代码后导致,要仔细检查各文件的导包,包括代码、配置文件中的导包
8,不能直接注入
Could not autowire. No beans of 'xxx' type found
解决办法:在mapper文件上加@Repository注解,这是从spring2.0新增的一个注解,用于简化 Spring 的开发,实现数据访问
9,无法创建xml文件
创建xml时只能找到XML Configuration File,不能创建普通xml文件
解决办法:
新建的时候,选择第五步定义的模板即可
10,配置多数据源
The database returned no natively generated identity value
解决办法:表的主键默认int,并且需要自增,多个表都要设置
11,配置多数据源
java.sql.SQLSyntaxErrorException: Unknown column 'city_introduce' in 'field list'
解决办法:以驼峰命名的数据库字段,执行的时候会在字段中加下划线,所以,在配置文件中增加下面这一条即可
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
还有其他错误情况,请评论中补充,我看到会尽快回复