Spring3 development error record

  •  Mybits is greater than less than sign problem

【Log】

Mapped Statements collection does not contain value for ***

[ View loading log]

ERROR -org.apache.ibatis.parsing.XPathParser - Error creating document instance.  Cause: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.

The content of the element must consist of well-formed character data or markup.

【the reason】

Write >= and <= in sql not to compile

【Solution】

Escape method

Greater than: >

Less than: <

Greater than or equal to: >=

Less than or equal to: <=

【related information】

When using mybatis, our sql is written in the xml mapping file. If there are some special characters in the written sql, it will be escaped when parsing the xml file, but we do not want it to be escaped, so we have to use <![CDATA[ ]]> to solve.

<![CDATA[ ]]> What is it, this is XML syntax. All content inside CDATA will be ignored by the parser.

If the text contains a lot of "<" characters <= and "&" characters-just like program code, then it is best to put them all in the CDATA part.

But there is a problem that is that <if test=""> </if> <where> </where> <choose> </choose> <trim> </trim> etc. These tags will not be parsed, so we only Put statements with special characters in <![CDATA[ ]]> to minimize the scope of <![CDATA[ ]]>.

Example:

<!-- Get the number of items-->

<select id="getCount" parameterType="Map" resultType="long">

select count(*) from orders o,user u,linkman l where o.flag=1

and o.uid=u.uid and l.lid=o.uid

<if test="fdate!=null">

<![CDATA[and fdate <= #{fdate}]]>

</if>

</select>

——————————————————————————————————————————————————————

  • Null pointer problem

[Log] java.lang.NullPointerException

[Reason] The object is a null value, and it is used to judge the non-empty condition (reference method, assignment to other objects, etc.)

[Solution] plus judgment

if(user==null){

}else{

}

[Related knowledge] The reason for this abnormal operation is mostly because the empty object is called, and there is no content in the empty object, so when the method of the empty object (such as size(), etc.) is called, a null pointer exception will occur. The following are A good programming habit that is often used can avoid this problem as much as possible

  1. When using equals(), use known objects.equals (unknown objects) 
  2. Use String.ValueOf() instead of .toString()
  3. Avoid returning a null pointer from the method, but return an empty collection or an empty array. Collections.EMPTY_LIST, Collections.EMPTY_SET, Collections.EMPTY_MAP

——————————————————————————————————————————————————————

  • Data out of bounds

【日志】java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

[Cause] There are no elements in the list; but trying to take the first element, it leads to out of bounds

【related information】

        When adding data to the database, it is necessary to determine whether there are records in the database, and the return result of the determination is usually a List. When the List is empty, you need to pay special attention to calling its method. For example, calling get() will report an out-of-bounds exception.

Of course, you can also think of other situations. After judging that the array or collection is empty, be careful not to take the content, otherwise the above error will be reported.

——————————————————————————————————————————————————————

  • Send empty data to oracle, need to specify the type

【日志】 Error setting null for parameter #4 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: Invalid column type: 1111

[Cause] When mybatis is connected to oracle, the fourth parameter passed a null value, and jdbcType needs to be set. The parameter type cannot be resolved when parsing the parameter. Specify the default type OTHER.

[Related knowledge] mybits specification, the parameter needs to set the type, such as #{id,jdbcType=VARCHAR}

——————————————————————————————————————————————————————

Guess you like

Origin blog.csdn.net/qq_36766417/article/details/105783006