今天学习mybatis基础时遇到的问题
main函数
System.out.println(mapper.addStudent(new Student().setSid(10).setName("leeleo").setSex("男")));
mapper
int addStudent(Student student);
xml文件
<insert id="addStudent"> insert into student(sid, name, sex) values(#{sid}, #{name}, #{sex}) </insert>
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.sql.SQLException: Data truncated for column 'sex' at row 1
### The error may exist in entity/TestMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into student(sid, name, sex) values(?, ?, ?)
调用时产生异常,sex无法读取,之前也碰到过。原因是mysql里设置的sex是枚举类型,在项目中调用时只需输入对应的序号1,2即可
System.out.println(mapper.addStudent(new Student().setSid(10).setName("leeleo").setSex("1")));
即可正常读入数据库
第二天一早发现问题在于连接sql时没有在xml里配置好导致传过去的数值大于枚举的范围
配置:
jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8