Common problems encountered in myBatis and Spring coding

1. When the front and rear ends are connected, the list cannot be found when the private message list is displayed

Error code:

//List.jsp:
<td><a href="/communications/${receiver}">private letter</a></td>

Error reporting reason:

MessageControl. The method call to return the private message page of the specified user displayed in Java is:

@RequestMapping(value = "/communications/{userName}", method = RequestMethod.GET)
public String message(@PathVariable("userName") String userName, HttpSession session)

The passed in parameter ${receiver} was not recognized. The back-end passed in the communicationList variable, which belongs to the front-end and back-end docking problem

resolvent:

Change ${receiver} to ${com.receiver}

List.jsp:

<c:forEach items="${communicationList}" var="com">
	<tr>													
<td>${com.receiver}</td> 
<td>${com.hasNewMessage}</td>	
<td><a href="/communications/${com.receiver}">private letter</a></td>
	</tr>
	</c:forEach>

2. When sending a message, the message text cannot be correctly transmitted to the back-end processing

Error code:

$("#Submit BTN "). Click (function()) function:
	$.post("/send/") 

Error reporting reason:

The message content message is missing from the passed parameters, and the message text acquisition needs to be added

resolvent:

js to modify the above click function:

var param = { "content": message };
	$.post("/send/" + userName, param, function (result) {
		$("#box").append(message);
	})

3. After the message is sent correctly, there is no new message displayed in the chat box of the private chat page. You need to refresh the page to receive it

Error reporting reason:

You need to add a timer Interval to refresh the message bar through polling

resolvent:

Add a polling function. The pseudo code is:

var timer = setInterval(function () { query() }, 4000);
function query() {
		var param = { "userName": userName };
		$.post("/query", param, function (result) {
			if result == "true"
				Get and display message content;
end
		})
	}

4. The display of new messages in the chat box of the private chat page does not wrap, and each new message is on the same line

Error reporting reason:

There is no line feed function between message intervals. If each message is a separate div, div::after needs to be introduced between message intervals, where the content is line feed

resolvent:

Add a newline representation in CSS

#box > div {
		width: 100%;
	}
	#box>div::before,
	#box>div::after {
		content: '';
		display: table;
		clear: both;
	}

5. The new message in the chat box of the private chat page is not displayed in the mode of left and right dialog boxes, but only text is output

Error code:

$("#Submit BTN "). Click (function()) function
	$.post("/send/" + userName, param, function (result) {
		$("#box").append(message);
	})

Error reporting reason:

The above code is simply added after the box content in the append message. It should be set inside the leftword and rightword classes of the left and right dialog boxes

resolvent:

Step 1:

Add variable

var right = " <div><p class=\"rightword\"> ";
var end = " </p></div> "

Step 2:

$("#Submit BTN "). Click (function()) function to modify the above post function
	$.post("/send/" + userName, param, function (result) {
		$("#box").append(right + message + end);
	})

6. Error code:

Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'xxx' not found. Available parameters are [0, 1, 2, param3, param1, param2]

When multiple parameters are passed in, each parameter needs to be annotated with @ Param in the mapper

//Before modification
int update(String sender, String receiver, boolean hasNewMessage);
//After adding comments
int update(@Param("sender") String sender, @Param("receiver") String receiver, @Param("hasNewMessage") boolean hasNewMessage);

7. Error code

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxController': Unsatisfied dependency expressed through field 'xxxService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxServiceImpl': Unsatisfied dependency expressed through field 'xxxMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxMapper' defined in file [xxx,java]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [xxx.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [xxx.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'BaseResultMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: BaseResultMap

When the query statement returns list < T > Modify resultType to resultMapper in xml

//mapper snippet
List<Communication> listCommunication(@Param("sender") String sender);
<!--xml Before modification-->
  <select id="listCommunication" resultType="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM  <include refid="tb"/>
    WHERE communication_sender = #{sender,jdbcType=VARCHAR}
  </select>
<!--xml After modification-->
  <select id="listCommunication" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM  <include refid="tb"/>
    WHERE communication_sender = #{sender,jdbcType=VARCHAR}
  </select>

3. (additional) correspondence and conversion of common data types of mybatis & Java & SQL

Mybatis javaSQL
integerint OR IntegerINTEGER
longlong OR java.lang.LongBIGINT
shortshort OR java.lang.ShortSMALLINT
floatfloat OR java.lang.FloatFLOAT
doubledouble OR java.lang.DoubleDOUBLE
characterjava.lang.StringCHAR(1)
stringjava.lang.String VARCHAR
byte byte OR java.lang.ByteTINYINT
booleanboolean OR java.lang.BooleanBIT
datejava.util.Date OR java.sql.DateDATE
timejava.util.Date OR java.sql.TimeTIME
timestampjava.util.Date OR java.sql.TimeStampTIMESTAMP

Keywords: Java Mybatis Spring Back-end

Added by rcmehta_14 on Wed, 15 Dec 2021 21:52:00 +0200