来自深渊的祝福 2019-01-14 12:15 采纳率: 100%
浏览 3678
已采纳

为什么我用springboot连接mysql始终不成功?

之前按照教程写不能成功,现在直接复制别人完整的代码还是不行
一些配置
application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

UserMapper.java

@Mapper
public interface UserMapper {

    @Select("select * from user")
    List<User> findAll();

    @Insert("insert into user(username,password)values(#{username},#{password}")
    public int add(User user);
}


UserSrevice.java

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> findAll(){


        return userMapper.findAll();
    }

    public int add(User user){

        return userMapper.add(user);

    }
}



每次刷新页面
图片说明

错误提示

2019-01-14 11:51:15.363 ERROR 752 --- [nio-8080-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: Unknown character set index for field '255' received from server.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910) ~[mysql-connector-java-5.0.3.jar:na]
    at com.mysql.jdbc.Connection.getCharsetNameForIndex(Connection.java:3396) ~[mysql-connector-java-5.0.3.jar:na]
    at com.mysql.jdbc.Field.<init>(Field.java:191) ~[mysql-connector-java-5.0.3.jar:na]

    ERROR 752 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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 java.sql.SQLException: Unknown character set index for field '255' received from server.
### The error may exist in com/polaris/springboot_mybatis_jsp/mapper/UserMapper.java (best guess)
### The error may involve com.polaris.springboot_mybatis_jsp.mapper.UserMapper.findAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Unknown character set index for field '255' received from server.] with root cause

其中会指向这两行

(UserService.java:18)
return userMapper.findAll();

(UserController.java:24)
List<User> list = userService.findAll();

求大神解答

  • 写回答

1条回答 默认 最新

  • L星火燎原 2019-01-14 14:25
    关注

    原因是MySQL版本高,mysql的jar版本低造成mysql与jar的编码错乱引起的,你可以看看你的版本是多少?详细可以参考哈这个
    https://blog.csdn.net/qq_38674645/article/details/81105266

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料