mybatis input mapping and output mapping

Posted by bogins on Mon, 06 May 2019 22:55:03 +0200

1. parameterType (input type)
(1) Pass simple types (such as int, which we can use directly)

public Student getStudentById(Integer studentId);
<select id="getStudentById" parameterType="int" resultType="student">
		select * from tblstudent where studentId = #{studentId}
	</select>


(2) Passing pojo objects
   

public Student getStudentByS(Student student);
<select id="getStudentByS" parameterType="student" resultType="student">
		select * from tblstudent where studentId = #{studentId}
	</select>


(3) Transfer the pojo wrapper object
Query conditions may be comprehensive query conditions, including not only user query conditions but also other query conditions (for example, when querying user information, the user purchases commodity information is also used as query conditions), at this time, the input parameters can be transmitted using the packaging object.
Packaging object: One property in the Pojo class is another pojo.

public Student getStudentByBean(StudentBean sb);
<select id="getStudentByBean" parameterType="studentBean" resultType="student">
		<!-- select * from tblstudent where studentId = #{student.studentId} -->
		select * from tblstudent where studentId = #{studentId}
	</select>

2. resultType (output type)
(1) Output simple type (e.g. inquiry about the number of student information records, the required output type is int type)

public int selectUserCount();
<select id="selectUserCount" resultType="int">
		select count(1) from tblstudent 
	</select>

(Whether it's an output pojo single object or a list list, the type specified by resultType in mapper.xml is the same. Below)

(2) Output pojo objects

public Student getSByName(String studentName);
<select id="getSByName" parameterType="String" resultType="student">
		 <!-- select * from tblstudent where studentName LIKE #{studentName} -->
		 <!--select * from tblstudent where studentName LIKE "%${value}%"	-->	 		  
	</select>


(3) Output pojo list

/**
	 * Fuzzy Inquiry of Student Information Based on Student Name
	 * @param studentName
	 * @return
	 */
	public List<Student> getStudentByName(String studentName);
<!-- Fuzzy Inquiry of Student Information Based on Student Name -->
	<select id="getStudentByName" parameterType="String" resultType="student">
		 <!-- select * from tblstudent where studentName LIKE "%"#{studentName}"%"	 -->
		 <!-- select * from tblstudent where studentName LIKE #{studentName}	 -->
		<!--  select * from tblstudent where studentName LIKE "%${value}%"	  -->
		 <!-- select * from tblstudent where studentName LIKE concat(concat('%',#{studentName}),'%') -->		 
	select * from tblstudent where studentName LIKE concat('%',#{studentName},'%') 		 
	</select>

3.resultMap
resultType can specify that the query result is mapped to pojo, but the mapping can be successful only if the attribute name of POJO and the column name of sql query are consistent.
If the sql query field name is inconsistent with the property name of pojo, the field name and the property name can be mapped as a corresponding relationship through resultMap, which essentially also needs to map the query result to the POJO object.
resultMap can map query results to complex types of pojo, such as one-to-one query and one-to-many query by including POJO and list in the query result mapping object.
 

package cn.xxs.mapper;

import java.util.List;

import cn.xxs.bean.StudentBean;
import cn.xxs.pojo.Student;

public interface UserMapper {
	
	/**
	 * Insert Student Data
	 * @param user
	 */	
	public int addStudent(Student student);
	/**
	 * Query by Student Number
	 * @param id
	 * @return
	 */
	public Student selectStudentById(Integer studentId);
	/**
	 * Multi-Conditional Query of User Information
	 * @param student
	 * @return
	 */
	public List<Student> selectStudent(Student student);
}

 

<?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">
<!-- namespace name  #{}: dot bits, equivalent to jdbc?-->
<mapper namespace="cn.xxs.mapper.UserMapper">

<!-- sql fragment -->
<sql id="student_sql">
	studentId,studentName,age,studentNo,birthDay
</sql>
	<!-- id: sql Unique Identification of Chinese Statements
	    parameterType :Data Types Involved
		resultType:The data type of the result returned
	 -->	
	<!-- Insert Student Data -->	
	<select id="addStudent" parameterType="int" resultType="student">
		insert into tblstudent(studentId,studentName,age,studentNo,birthDay)
		values(#{studentId},#{studentName},#{age},#{studentNo},#{birthDay})
	</select>
	
	<resultMap type="student" id="student_map">
		<id column="studentId" property="id"/>
		<result column="studentName" property="studentName"/>
		<result column="age" property="age"/>
		<result column="studentNo" property="studentNo"/>
		<result column="birthDay" property="birthDay"/>
	</resultMap>
	
	<select id="selectStudentById" parameterType="int" resultType="student" resultMap="student_map">
		select 
		<include refid="student_sql"></include>
		from tblstudent 
		where studentId = #{id}
	</select>
	<select id="selectStudent" parameterType="int" resultType="student" resultMap="student_map">
		select 
		<include refid="student_sql"></include>
		from tblstudent 
		<where>	
			<if test="studentName != null and studentName != '' ">
				and studentName LIKE concat('%',#{studentName},'%') 
			</if>
			<if test="age != null and age != '' ">
				 and age LIKE concat('%',#{age},'%') 
			</if>	
		</where>			 
	</select>
	
</mapper>

 

Topics: SQL Mybatis xml Attribute