weixin_41328649
来自深渊的祝福
2019-01-14 12:15

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

  • spring
  • intellij-idea

之前按照教程写不能成功,现在直接复制别人完整的代码还是不行
一些配置
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条回答