尚硅谷SSM框架整合测试CRUD遇到的问题

项目场景:

学习尚硅谷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解决方法

@Transactional 在一个事物方法中开启新事物,完成对数据库的修改

IDEA向MySQL数据库中添加信息,中文显示问号?的解决方法

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值