项目场景:
学习尚硅谷SSM框架整合时,采用spring整合的spring-test直接来测试CRUD遇到的问题,在视频的P11
MapperTest中的测试方法
//@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {
@Autowired
DepartmentMapper departmentMapper;
@org.junit.Test
public void testCRUD(){
System.out.println(departmentMapper);
// 1. 测试插入部门
departmentMapper.insertSelective(new Department(null, "技术部"));
departmentMapper.insertSelective(new Department(null, "开发部"));
}
}
问题一描述
错误提示“: java.sql.SQLException: Field ‘dept_id’ doesn’t have a default value”
@Override
org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: java.sql.SQLException: Field 'dept_id' doesn't have a default value
### The error may exist in file [D:\JAVA\Code\IDEA\SSM-CRUD\target\classes\mapper\DepartmentMapper.xml]
### The error may involve com.atguigu.crud.dao.DepartmentMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: insert into tbl_dept ( dept_name ) values ( ? )
### Cause: java.sql.SQLException: Field 'dept_id' doesn't have a default value
; Field 'dept_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'dept_id' doesn't have a default value
原因及解决方案:
提示:Field ‘dept_id’ doesn’t have a default value,它的意思是字段“dept_id”没有默认值,这个问题的解决方案是,在SQL语句中加上该字段,将它设置为自增列
注意:这里会报错
cannot change column 'dept_id' :used in a foreign key
,是因为已经设置dept_id为外键,需要删除外键,设置自增,再添加外键就可以成功
运行成功,没有报错
问题二描述
虽然idea中没有错,但是MySQL表中为null
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5aac4250 [wrapping: com.mysql.cj.jdbc.ConnectionImpl@1338fb5]] will be managed by Spring
==> Preparing: insert into tbl_dept ( dept_name ) values ( ? )
==> Parameters: 技术部(String)
<== Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@52851b44]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@52851b44] from current transaction
原因及解决方案:
上网搜索后发现原因是在方法@Transactional注解,这个注解把当前方法中的作为一个事物,只有当方法都完成时,才会进行提交操作。
//@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {
@Autowired
DepartmentMapper departmentMapper;
/**
* 测试DepartmentMapper
*/
public void testCRUD(){
// //1、创建SpringIOC容器
// ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
// //2、从容器中获取mapper
// DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);
System.out.println(departmentMapper);
// 1. 测试插入部门
departmentMapper.insertSelective(new Department(null, "技术部"));
departmentMapper.insertSelective(new Department(null, "开发部"));
}
}
将@Transactional注释掉,再次运行,此次执行没有报错
问题三描述
dept_name显示“???”
原因及解决方案:
确定dept表字符集是UTF-8的格式
上网搜索后,发现需要把dbconfig.properties的url后面添加
?characterEncoding=UTF-8
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud?characterEncoding=UTF-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
最后运行,表中dept_name一列添加了数据
参考链接:
MyBatis错误提示:Error updating database. Cause: java.sql.SQLException: Field ‘id’ doesn’t have a defaul
cannot change column ‘c_id’ :used in a foreign key解决方法