spring boot mybatis向mysql插数据的问题

银点 2018-05-07 04:05:30
1、数据库是mysql 5.7
2、字符串默认是utf8
3、IDE是为idea 2017
简单的增删改查

CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(16) NOT NULL,
`phone` varchar(11) NOT NULL,
`sex` int(11) NOT NULL DEFAULT '-1',
`role` int(11) DEFAULT '0' ,
`passwd` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;



public class User {
private Integer userId;
private String userName;
private String userPassword;
private String userPhone;
private Integer userSex;
private Integer userRole;
……
}

我在mysql客户端工具中直接写sql语句insert 的时候11位手机号是能写进去的。
但是在程序中一执行就报phone超长了。

2018-05-07 15:55:59.177 DEBUG 12912 --- [nio-8080-exec-5] c.s.dinner.mapper.UserMapper.createUser : ==> Preparing: insert into user(name,passwd, phone,sex)values(?,?,?,?)
2018-05-07 15:55:59.177 DEBUG 12912 --- [nio-8080-exec-5] c.s.dinner.mapper.UserMapper.createUser : ==> Parameters: 'test'(String), '123456'(String), '18516693703'(String), 1(Integer)
2018-05-07 15:55:59.193 ERROR 12912 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1
### The error may involve com.senxiang.dinner.mapper.UserMapper.createUser-Inline
### The error occurred while setting parameters
### SQL: insert into user(name,passwd, phone,sex)values(?,?,?,?)
### Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1
; ]; Data truncation: Data too long for column 'phone' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1] with root cause
...全文
1079 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwn_2016 2018-05-07
  • 打赏
  • 举报
回复
insert时指定参数的jdbcType试试
oO临时工Oo 2018-05-07
  • 打赏
  • 举报
回复
mysql客户端工具的编码应该是utf8 java通过jdbc传给mysql的字符集应该可能不是utf8,检察jdbc连接字符串是否有设置字符集 其它检查方案(各方案没有相关性): 1.在DAO层,在Java代码中临时写死电话号码,看能否正常插入数据库,如果正常,说明HTTP传给Java的字符集有问题。 2、将数据库phone字段,临时加长,看能否正常入库。 如正常,说明Java传给jdbc的字符集有问题。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧