Mybatis label learning

Mybatis is very simple, directly first SQL, but there are more knowledge points, every time it is used and checked now, the purpose of writing this article is to summarize, and secondly, it is a memo.

Overview

Insert picture description here

sql

<sql>Node, most people's impression of this label is limited to: it is used to contain the fields of the database, so that it can be used to replace all field *symbols to improve the execution speed:

<sql id="Base_Column_List" >
	id, `name`, `desc`, date_format(updatetime, '%Y-%m-%d %H:%i:%s') as updatetime, updateby, isactive
</sql>

In the definition of mybatis, <sql>nodes are used to define reusable SQL code segments, which can be included in other statements using <include>nodes.

include

With sqltags, the reuse of SQL fragments can be realized:

<sql id="totalSearchSql">
	/* 分配状态 */
	<if test="isDistrib != null and is_distrib !=''">
		and ly.is_distrib = #{isDistrib}
	</if>
</sql>

Insert picture description here
And can use #{}placeholder parameterization (the parameters here are not passed in when calling), and different attribute values ​​change through the included instances.

All of the above is to realize the reuse of SQL fragments in the same mapper.xml file. The tag id with the same name can be defined in different mapper.xml files .

Advanced: Reuse SQL fragments in different mapper.xml files?
Add the namespace identifier when importing, such as <include refid="org.aaa.dao.AntelopeCheckDao.totalSearchSql"/>(which org.aaa.dao.AntelopeCheckDaois the command space, which is defined on the namespace attribute of the mapper tag)

The test SQL fragment defined in CheckDao.xml:

<select id="testInclude" parameterType="Map" resultType="Map">
	select * from rpt_rhino_record_lingyang ly where 1=1 <include refid="org.aaa.dao.AntelopeCheckDao.totalSearchSql"></include> limit 1
</select>

Corresponding mapper interface:

Map<String, Object> testInclude(Map<String, Object> paramMap);

The property tag can be used in the include tag to specify custom attributes?

Guess you like

Origin blog.csdn.net/lonelymanontheway/article/details/108886820