Tag Archives: Development issues

Mybatis Add Datas error: ERROR: Field * doesn‘t have a default value

Mybatis will report error: field * doesn’t have a default value after adding data

Article catalog

After mybatis adds data, it will report error: field * Don’t have a default value database table. If the same SQL is inserted into the database through mybatis, it will report an error. Do not insert the not null field, do not insert the nut null default field

How to design a general insert statement method 1: set the default value on the entity class method 2: use the label

Database table

CREATE TABLE `apply_log` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `exception_date` date NOT NULL DEFAULT '0000-00-00' COMMENT '异常日期',
  `apply_date` date NOT NULL DEFAULT '2021-00-00' COMMENT '申请日期',
  `apply_person` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '申请人',
  `apply_person_id` int(10) NOT NULL COMMENT '申请人id',
  `operate_person` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '审批人',
  `operate_person_id` int(10) NOT NULL COMMENT '审批人id',
  `apply_result` tinyint(1) NOT NULL COMMENT '申请结果,1.申请中.2.审批拒绝,3.审批同意',
  `comment` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '备注',
  `apply_id` int(10) NOT NULL COMMENT '绑定申请信息',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='异常申请操作日志'


exception_ The date setting is not empty, and the default value ‘0000-00-00’
Apply_ Date setting is not empty, and the default value ‘2021-00-00’
other fields are not empty, and there is no default value.

Now start inserting data into the database

INSERT INTO apply_log(apply_person) VALUES('yyds');

Execution result

if not null is set in mysql, the default value of data type is 0000-00-00, the default value of int is 0, and the default value of varchar type is empty string

If the same SQL is inserted into the database through mybatis, an error will be reported

Do not insert not null fields

Entity class

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ApplyLog1 {
   private Integer id;
   private String exceptionDate; 
   private String applyDate;
   private String applyPerson;
   private Integer applyPersonId;
   private String operatePerson;
   private Integer operatePersonId;
   private Integer applyResult;
   private String comment ;
   private Integer applyId ;
}

mybatis

    <insert id="insertGao" >
        INSERT INTO apply_log(apply_person) VALUE ('17yyds')
    </insert>

mapper

Integer insertGao();

Execute direct report apply_ person_ ID is not empty. The default value of mybatis does not take effect

Do not insert nut null default field

xml层
    <insert id="insertTwo">
        insert into apply_log(apply_person,apply_person_id,operate_person,operate_person_id,apply_result
                             ,comment,apply_id) values ('1111',1111,'1111',1111,2,'1111',11)
    </insert>
 mapper层
    Integer insertTwo();
        @Test
    public void test8() {
        applyLog1Mapper.insertTwo();
    }

Insertion succeeded
non empty fields with their own default values can be successfully inserted, and the default value is used for relevant fields

How to design general insert statements

xml层
    <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into apply_log(exception_date,apply_date,apply_person,apply_person_id,operate_person,operate_person_id,apply_result
        ,comment,apply_id) values (#{applyLog.exceptionDate},#{applyLog.applyDate},#{applyLog.applyPerson},#{applyLog.applyPersonId}
        ,#{applyLog.operatePerson},#{applyLog.operatePersonId},#{applyLog.applyResult},#{applyLog.comment},#{applyLog.applyId})
    </insert>
mapper层
	Integer insert(@Param("applyLog") ApplyLog1 applyLog);

if no default value is set for the attribute of the entity class, the default value of the encapsulation type is null, so an error will be reported during insertion


    @Test
    public void test9() {
        ApplyLog1 applyLog1 = new ApplyLog1();
        applyLog1.setApplyPerson("18yyds");
        applyLog1Mapper.insert(applyLog1);
    }

All inserted values are null

Method 1: set the default value on the entity class

When the new object is, make the entity class set the initial value

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ApplyLog1 {
    private Integer id;
    private String exceptionDate="0000-00-00";
    private String applyDate="2021-00-00";
    private String applyPerson="19yyds";
    private Integer applyPersonId=19;
    private String operatePerson="19yyds";
    private Integer operatePersonId=19;
    private Integer applyResult=2;
    private String comment="19yyds";
    private Integer applyId=89;
}

Perform test

    @Test
    public void test9() {
        ApplyLog1 applyLog1 = new ApplyLog1();
        applyLog1.setApplyPerson("独孤求败");
        applyLog1Mapper.insert(applyLog1);
    }

Insert succeeded and the default value of entity class is used
[ Insert picture description here]( https://img-blog.csdnimg.cn/94ccd730abf64893b8927361163fc4b2.png

Method 2: use labels

Modify the table structure to establish default values for all fields

Create Table

CREATE TABLE `apply_log` (
 `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
 `exception_date` date NOT NULL DEFAULT '0000-00-00' COMMENT '异常日期',
 `apply_date` date NOT NULL DEFAULT '2021-00-00' COMMENT '申请日期',
 `apply_person` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '申请人',
 `apply_person_id` int(10) NOT NULL COMMENT '申请人id',
 `operate_person` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '审批人',
 `operate_person_id` int(10) NOT NULL COMMENT '审批人id',
 `apply_result` tinyint(1) NOT NULL COMMENT '申请结果,1.申请中.2.审批拒绝,3.审批同意',
 `comment` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '备注',
 `apply_id` int(10) NOT NULL COMMENT '绑定申请信息',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='异常申请操作日志'

Entity class

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ApplyLog1 {
   private Integer id;
   private String exceptionDate;
   private String applyDate;
   private String applyPerson;
   private Integer applyPersonId;
   private String operatePerson;
   private Integer operatePersonId;
   private Integer applyResult;
   private String comment;
   private Integer applyId;
}

Mapper, use the tag to remove the redundant “,” characters that may appear at the beginning and end of the fragment

    <insert id="insertThree" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
       insert into apply_log(<include refid="baseColumn"></include>) values (<include refid="baseProperty"></include>)
   </insert>
   <sql id="baseColumn">
       <trim suffixOverrides=",">
           <if test="applyLog.exceptionDate != null">exception_date,</if>
           <if test="applyLog.applyDate != null">apply_date,</if>
           <if test="applyLog.applyPerson != null">apply_person,</if>
           <if test="applyLog.applyPersonId != null">apply_person_id,</if>
           <if test="applyLog.operatePerson != null">operate_person,</if>
           <if test="applyLog.operatePersonId != null">operate_person_id,</if>
           <if test="applyLog.applyResult != null">apply_result,</if>
           <if test="applyLog.comment != null">comment,</if>
           <if test="applyLog.applyId != null">apply_id,</if>
       </trim>
   </sql>

   <sql id="baseProperty">
       <trim suffixOverrides=",">
           <if test="applyLog.exceptionDate != null">#{applyLog.exceptionDate},</if>
           <if test="applyLog.applyDate != null">#{applyLog.applyDate},</if>
           <if test="applyLog.applyPerson != null">#{applyLog.applyPerson},</if>
           <if test="applyLog.applyPersonId != null">#{applyLog.applyPersonId},</if>
           <if test="applyLog.operatePerson != null">#{applyLog.operatePerson},</if>
           <if test="applyLog.operatePersonId != null">#{applyLog.operatePersonId},</if>
           <if test="applyLog.applyResult != null">#{applyLog.applyResult},</if>
           <if test="applyLog.comment != null">#{applyLog.comment},</if>
           <if test="applyLog.applyId != null">#{applyLog.applyId},</if>
       </trim>
   </sql>

Use relevant methods

    @Test
    public void test10() {
        ApplyLog1 applyLog1 = new ApplyLog1();
        applyLog1.setApplyPerson("天下无敌");
        applyLog1Mapper.insertThree(applyLog1);
    }

Successfully inserted

Network agent problem: task: preparekotlinbuildscriptmodel up-to-date

The error code is as follows

> Task :prepareKotlinBuildScriptModel UP-TO-DATE
IOException: https://dl.google.com/android/repository/addons_list-3.xml
java.net.ConnectException: Connection refused: connect
IOException: https://dl.google.com/android/repository/addons_list-2.xml
java.net.ConnectException: Connection refused: connect
IOException: https://dl.google.com/android/repository/addons_list-1.xml
java.net.ConnectException: Connection refused: connect
Failed to download any source lists!

The solution is to find the gradle.properties file under the. Gradle file in the user directory, open and delete the proxy configuration, IP and port with proxy (not just IP)!!!! And ports!!)

Eclipse run main method error: a JNI error has occurred, please check your installation and try again

The magic question

It’s the first time I’ve encountered this kind of error. When creating a new Java project, the test package is named java.test.tt , write the main method to run, and the error will be reported as follows:

First pop up window:

Second pop up window:

Detailed error information:

libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: cHRM chunk does not match sRGB
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: cHRM chunk does not match sRGB
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.test.tt
	at java.lang.ClassLoader.preDefineClass(ClassLoader.java:662)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

solve:

Do not use Java as the beginning of the package name, for example, error demonstration: java.test.tt

Why?

When loading a Java class, you are restricted to throwing an exception if the package name starts with Java. This should also be to ensure the security of classes in JDK when loading classes.

Maven compilation error: package does not exist jar package does not exist

A compilation failure was encountered while using the Maven command MVN package to package. Maven prompted the compilation error: the package does not exist JAR package. After checking, it was the use of non-Maven repository jars in the project, i.e., the use of local jars, which caused Maven to compile without finding jars. The solution is to configure POm.XML to add the path to the local JAR so that Maven can identify and find the local JAR at compile time.

The configuration of the Maven-compiler-plugin in POM.xml is referred to below. The focus is on the configuration of the compilerArguments element. To add a native JAR package, just add a line to the compilerArguments element.

< compilerArguments>
< extdirs> ${project.basedir}/src/main/webapp/WEB-INF/lib< /extdirs>
< /compilerArguments>

The pom.xml build configuration is referenced below.

<build>
		<finalName>test</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<compilerArguments>
					<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
					</compilerArguments>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>true</skipTests>
					<testFailureIgnore>true</testFailureIgnore>
				</configuration>
			</plugin>
		</plugins>
</build>