[MyBatis configuration file]

Summary of MyBatis FAQs

<mapper namespace="对应于Dao层的接口名称"></mapper>

Correct format of sql statement

insert

INSERT 
INTO table_name
(column1, column2,...columnN)
VALUES  ( value1, value2,...valueN )

mybatis->insert

The syntax format in xml is similar to
the insert tag in sql : it means insertion. The default return value is the number of rows affected by the database, so in mybatis we do not need to specifically declare the return value type. The
if tag : the judgment condition for filling in the field in test, if If it is satisfied, the sql statement will splice the fields in the if tag, otherwise
the trim tag will not be spliced : this tag is used to splice the fields we need
prefixIt means prefix. For example, in the following insert statement, there is already insert into table_name. Next we need (as a prefix splicing, so here we put (
suffixIt means suffix, with (, we need), so put **)** at the end, and put the database fields we need in the middle
suffixOverridesWhen the judgment condition in the if tag is empty, the redundant suffix content filled in the suffixOverrides tag is removed (this is only the case when the fields in the last one or several if tags do not meet the judgment conditions of the if tag) will appear)
jdbcType : The type of the field in the database

<insert id="对应方法名" parameterType="java中所对应的参数类型">
	insert into table_name
	<trim prefix="(" suffix=")" suffixOverrides=",">
		<if test="后端字段名1 != null">
			数据库字段名称1,
		</if>
		<if test="后端字段名2 != null">
			数据库字段名称2
		</if>
	</trim>
	<trim prefix="values (" suffix=")" suffixOverrieds=",">
		<if test="后端字段名1 != null">
			#{后端字段名1,jdbcType=VARCHAR}
		</if>
		<if test="后端字段名2 != null">
			#{后端字段名2,jdbcType=VARCHAR}
		</if>
	</trim>
</insert>

delete

DELETE 
FROM table_name
where  
table_name.var1='字符串1'
and table_name.var2='字符串2'

mybatis->delete

The default return value of delete is the number of database rows affected, so there is no need to write a return type.
Note :
Delete must clearly determine whether the condition is empty. If it is empty, it may delete all fields in the table.
When there is only one deletion condition, delete cannot be used with the if tag. Use them together, otherwise an error will be reported
. Don’t forget that the connector in the where statement of delete is and. The prefix and suffix in the trim tag are spliced ​​at the front and end of a large section of content contained in the trim tag, so in delete The and cannot be used

<delete id="对应方法名" parameterType="java中所对应的参数类型">
	delete  
	from table_name
	where
	<trim suffixOverrieds="and">
		<if test="后端字段名1 != null">
			数据库字段名称1=#{后端字段名1,jdbcType=VARCHAR} and
		</if>
		<if test="后端字段名2 != null">
			数据库字段名称2=#{后端字段名2,jdbcType=VARCHAR}
		</if>
	</trim>
</delete>

select

SELECT col1,col2,col3
FROM table_name
WHERE 条件
[group by column]
[having 条件]
[order by DESC]
[limit 条件]

mybatis->select

resultMap tag : result set
id: the unique identifier of resultMap, which can be placed in the resultMap attribute in the select statement.
id in resultMap: this contains the primary key of the corresponding table (this)
association in resultMap: this label can be used to nest new Object (that is, an object within an object)
Note:
When our return value contains an object of an object, please note that there may be a problem with the return value of the two-layer resultMap. Only one piece of data is returned.
Note that when we have two pieces of data, the outer layer If the corresponding content in the object is exactly the same, even if the content in the inner object is different, only one piece of data will be returned. Solution
:
1. Make the return value corresponding to the outer object different, and add a primary key to resultMap. But the back-end entity class does not write the attributes corresponding to the primary key field.

<select id="对应方法名" parameterType="java中所对应的参数类型" resultMap="要调用的resultMap的名字">
	select 
	from table_name
	where
	<trim suffixOverrieds="and">
		<if test="后端字段名1 != null">
			数据库字段名称1=#{后端字段名1,jdbcType=VARCHAR} and
		</if>
		<if test="后端字段名2 != null">
			数据库字段名称2=#{后端字段名2,jdbcType=VARCHAR}
		</if>
	</trim>
</select>

<resultMap id="这个resultMap的唯一标识" type="对应的结果类型">
	<id column="" jdbcType=""/>
	<result column="" jdbcType=""/>
	<result column="strVar" jdbcType="VARCHAR" property="url"/>
	<result column="timeVar" jdbcType="TIMESTAMP" property="createTime"/>
	<result column="jsonObjectVar"
	                typeHandler="com.hisense.higw.message.config.json.ObjectJsonHandler"
	                jdbcType="VARCHAR"
                	property="dataPackage"/>      
    <association javaType= "大对象中的小对象"
					property="后台参数名"
					resultMap="这个小对象所对应的resultMap的id"

	/>
</resultMap>
<resultMap id="这个resultMap的唯一标识" type="对应的结果类型">
	<id column="" jdbcType=""/>
	<result column="" jdbcType=""/>  
</resultMap>

Guess you like

Origin blog.csdn.net/ABidMan/article/details/127567629