一个梦想做GC的IT民工 2023-12-06 14:40 采纳率: 0%
浏览 18

关于数据库的问题,求解答

各位义父们 遇到瓶颈了求解答 问题如下 刚开始的时候搭建了一个小demo执行一个insert的时候报错了 报错信息为一个字段没有默认值 但是呢这个字段我全局都没写过 不知道他哪来的 代码如下:


@RequestMapping("/demo")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class, timeout = 600)
    public void demo1(){
        List<PdfDto> list = new ArrayList<>();
        for (int a=0 ; a<3 ; a++){
            PdfDto dto = new PdfDto();
            dto.setName("张三"+a);
            dto.setAge(a+""+a);
            dto.setGender(a+"");
            dto.setPhone(a+"");
            dto.setHospital(a+"");
            dto.setDepartment(a+"");
            dto.setNumber(a+"");
            dto.setHospitalDay(new Date());
            dto.setLeaveHospitalDay(new Date());
            dto.setTreatment(a+"");
            dto.setNum(a+"");
            list.add(dto);
        }

        PdfDto pdfDto = new PdfDto();
        for (PdfDto e : list){
            BeanUtils.copyProperties(e, pdfDto);
            pdfDto.setHospitalDay(new Date());
            //此处执行了插入
            pdfMapper.insertSelective(pdfDto);
        }
    }

以下是数据库连接配置文件


spring:
  datasource:
    username: root
    password: password
    url: jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver

以下是mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mybatis.PdfMapper">

    <resultMap id="BaseResultMap" type="com.example.demo.mybatis.dto.PdfDto">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
        <result column="phone" property="phone"/>
        <result column="hospital" property="hospital"/>
        <result column="department" property="department"/>
        <result column="number" property="number"/>
        <result column="hospital_day" property="hospitalDay" jdbcType="TIMESTAMP"/>
        <result column="leave_hospital_day" property="leaveHospitalDay" jdbcType="TIMESTAMP"/>
        <result column="treatment" property="id"/>
    </resultMap>

    <insert id="insertSelective" parameterType="com.example.demo.mybatis.dto.PdfDto">
        insert into pdf_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                Id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="age != null">
                age,
            </if>
            <if test="gender != null">
                gender,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="hospital != null">
                hospital,
            </if>
            <if test="department != null">
                department,
            </if>
            <if test="number != null">
                number,
            </if>
            <if test="hospitalDay != null">
                hospital_day,
            </if>
            <if test="leaveHospitalDay != null">
                leave_hospital_day,
            </if>
            <if test="treatment != null">
                treatment,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="age != null">
                #{age,jdbcType=VARCHAR},
            </if>
            <if test="gender != null">
                #{gender,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                #{phone,jdbcType=VARCHAR},
            </if>
            <if test="hospital != null">
                #{hospital,jdbcType=VARCHAR},
            </if>
            <if test="department != null">
                #{department,jdbcType=VARCHAR},
            </if>
            <if test="number != null">
                #{number,jdbcType=VARCHAR},
            </if>
            <if test="hospitalDay != null">
                #{hospitalDay,jdbcType=TIMESTAMP},
            </if>
            <if test="leaveHospitalDay != null">
                #{leaveHospitalDay,jdbcType=TIMESTAMP},
            </if>
            <if test="treatment != null">
                #{treatment,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
</mapper>


以下为表结构

img


以下是报错

2023-12-06 11:12:06.764 ERROR 2540 --- [nio-8080-exec-7] 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: java.sql.SQLException: Field 'diagnose_code' doesn't have a default value
### The error may exist in file [D:\javaProduct\demo\target\classes\mapper\PdfMapper.xml]
### The error may involve com.example.demo.mybatis.PdfMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: insert into pdf_data          ( name,                                           age,                                           gender,                                           phone,                                           hospital,                                           department,                                           number,                                           hospital_day,                                           leave_hospital_day,                                           treatment )           values ( ?,                                           ?,                                           ?,                                           ?,                                           ?,                                           ?,                                           ?,                                           ?,                                           ?,                                           ? )
### Cause: java.sql.SQLException: Field 'diagnose_code' doesn't have a default value
; Field 'diagnose_code' doesn't have a default value; nested exception is java.sql.SQLException: Field 'diagnose_code' doesn't have a default value] with root cause

java.sql.SQLException: Field 'diagnose_code' doesn't have a default value
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:371) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44) ~[HikariCP-4.0.3.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:181) ~[mybatis-3.5.9.jar:3.5.9]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_352]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_352]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_352]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_352]
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427) ~[mybatis-spring-2.0.7.jar:2.0.7]
    at com.sun.proxy.$Proxy59.insert(Unknown Source) ~[na:na]
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272) ~[mybatis-spring-2.0.7.jar:2.0.7]
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145) ~[mybatis-3.5.9.jar:3.5.9]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86) ~[mybatis-3.5.9.jar:3.5.9]
    at com.sun.proxy.$Proxy60.insertSelective(Unknown Source) ~[na:na]
    at com.example.demo.mybatis.PdfController.demo1(PdfController.java:56) ~[classes/:na]
    

报错diagnose_code字段没有默认值 就很神奇 后来发现是因为 数据库连接哪里库名写错了应该是idemo写成了mysql虽然不报错了但是是为什么库名写成mysql能连接成功而且使用DBeaver连接测试库名写demo和mysql都能连上同一个库是为什么,而且为什么配置文件中连接了mysql这个错误的配置会出现一个不存在的字段报错呢 求各位义父解答一下

  • 写回答

3条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-06 16:40
    关注

    【以下回答由 GPT 生成】

    由于没有提供PdfDto的实体类代码和PdfMapper的实体类代码,我们无法分析具体的问题所在。但是根据您提供的代码和错误信息来看,可能出现的问题如下:
    
    1. PdfDto实体类中没有设置@Id和@GeneratedValue注解来定义主键。
    2. PdfDto实体类中某个字段没有设置默认值,并且数据库表的该字段也没有设置默认值。
    
    解决方案:
    1. 确保PdfDto实体类中有正确的主键定义,并且主键设置了自增长。
    2. 确保PdfDto实体类中所有字段都有默认值,或者在数据库表中为该字段设置默认值。
    
    另外,您提供的代码中并没有包含PdfMapper的相关代码,因此无法确切判断问题所在。如果上述解决方案不起作用,建议您提供PdfDto和PdfMapper的相关代码,以便我们更好地帮助您解决问题。
    

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 12月6日