Several postures of parameter transfer of SpringBoot series Mybatis
In the daily development of mybatis, how do the parameters defined in the mapper interface map with the parameters in xml? In addition to our commonly used @Param
annotations, what are the other ways?
- What happens to the default scene without annotations?
- What should I do if the interface parameter type is Map/POJO?
This article will mainly introduce several mapping binding methods between the parameters defined in the mapper interface and the placeholders in xml in the daily development of mybatis
<!-- more -->
I. Environment Configuration
We use SpringBoot + Mybatis + MySql to build an instance demo
- springboot: 2.2.0.RELEASE
- mysql: 5.7.22
1. Project configuration
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
Core dependencies mybatis-spring-boot-starter
, as for version selection, go to the mvn repository and find the latest
Another thing that cannot be obtained is the db configuration information.appliaction.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password:
2. Database tables
database for testing
CREATE TABLE `money` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
`money` int(26) NOT NULL DEFAULT '0' COMMENT '钱',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8mb4;
II. Parameter passing
Next, let's take a look at several postures of the parameters in the Mapper interface and the parameters in the xml file; about the construction of the mybatis project, I will skip it here. The key information is as follows
database entity object
@Data
public class MoneyPo {
private Integer id;
private String name;
private Long money;
private Integer isDeleted;
private Timestamp createAt;
private Timestamp updateAt;
private Integer cnt;
}
mapper interface
@Mapper
public interface MoneyMapper {
}
xml file, under the resource folder, the directory level is exactly the same as the package path of the mapper interface (follow the default binding relationship between the Mapper interface and the xml file. For details, see the Mapper interface of the SpringBoot series Mybatis and the Sql binding several postures )
<?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">
<mapper namespace="com.git.hui.boot.mybatis.mapper.MoneyMapper">
<resultMap id="BaseResultMap" type="com.git.hui.boot.mybatis.entity.MoneyPo">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="money" property="money" jdbcType="INTEGER"/>
<result column="is_deleted" property="isDeleted" jdbcType="TINYINT"/>
<result column="create_at" property="createAt" jdbcType="TIMESTAMP"/>
<result column="update_at" property="updateAt" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="money_po">
id, name, money, is_deleted, create_at, update_at
</sql>
</mapper>
1. @Param annotation
Add @Param
annotations to the parameters of the interface, and specify the parameter names passed to xml internally
A simple case is as follows
int addMoney(@Param("id") int id, @Param("money") int money);
Focus on the parameters above
- Pass
@Param
to specify the parameter name when passed to xml
The sql in the corresponding xml file is as follows, used #{}
to implement parameter binding
<update id="addMoney" parameterType="java.util.Map">
update money set money=money+#{money} where id=#{id}
</update>
2. Single parameter
Next, let's take a look at @Param
how parameters should be specified in xml in the default scenario when annotations are not used; because the actual results of single parameters and multiple parameters are inconsistent, they will be explained separately here.
In the single-parameter scenario, the parameter name in xml can be indicated by any value
The mapper interface is defined as follows
/**
* 单个参数时,默认可以直接通过参数名来表示,实际上#{}中用任意一个值都可以,没有任何限制,都表示的是这个唯一的参数
* @param id
* @return
*/
MoneyPo findById(int id);
/**
* 演示xml中的 #{} 为一个匹配补上的字符串,也可以正确的实现参数替换
* @param id
* @return
*/
MoneyPo findByIdV2(int id);
The corresponding xml file content is as follows
<select id="findById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="money_po"/>
from money where id=#{id}
</select>
<select id="findByIdV2" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="money_po"/>
from money where id=#{dd}
</select>
Focus on the above findByIdV2
, the parameter name used in the above sql is #{dd}
different from the parameter name in the mapper interface, but the final result is no different
3. Multiple parameters
When the number of parameters exceeds 1 #{}
, there are two ways for the parameters in
- param1...N: where n is the number of parameters in the interface
- arg0...N
/**
* 不指定参数名时,mybatis自动封装一个 param1 ... paramN的Map,其中n表示第n个参数
* 也可以使用 arg0...n 来指代具体的参数
*
* @param name
* @param money
* @return
*/
List<MoneyPo> findByNameAndMoney(String name, Integer money);
The corresponding xml is as follows
<select id="findByNameAndMoney" resultMap="BaseResultMap">
select
<include refid="money_po"/>
-- from money where name=#{param1} and money=#{param2}
from money where name=#{arg0} and money=#{arg1}
</select>
Note that in the above xml, both types of parameter passing are possible. Of course, it is not recommended to use this default method to pass parameters, because it is very unintuitive and inelegant for subsequent maintenance.
3. Map parameters
If the parameter type is not a simple type, when it is a Map type, the parameters in the xml file can be directly referred to by the corresponding key in the map
/**
* 参数类型为map时,直接使用key即可
* @param map
* @return
*/
List<MoneyPo> findByMap(Map<String, Object> map);
The corresponding xml is as follows
<select id="findByMap" resultMap="BaseResultMap">
select
<include refid="money_po"/>
from money
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="id != null">
id = #{id}
</if>
<if test="name != null">
AND name=#{name}
</if>
<if test="money != null">
AND money=#{money}
</if>
</trim>
</select>
4. POJO objects
Another common case is to pass parameters as simple entity objects. At this time, the parameters in xml can also be directly referred to by the fieldName of the object, which is similar to how map is used.
/**
* 参数类型为java对象,同样直接使用field name即可
* @param po
* @return
*/
List<MoneyPo> findByPo(MoneyPo po);
The corresponding xml file is as follows
<select id="findByPo" parameterType="com.git.hui.boot.mybatis.entity.MoneyPo" resultMap="BaseResultMap">
select
<include refid="money_po"/>
from money
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="id != null">
id = #{id}
</if>
<if test="name != null">
AND name=#{name}
</if>
<if test="money != null">
AND money=#{money}
</if>
</trim>
</select>
5. Simple parameter + Map parameter
When there are multiple parameters, some of which are simple types and some are Maps, how to deal with the parameters in such a scenario?
- Simple types follow the rules above
- The parameters of the map parameter are passed using the prefix + "." + key
An example is as follows
List<MoneyPo> findByIdOrCondition(@Param("id") int id, @Param("map") Map<String, Object> map);
List<MoneyPo> findByIdOrConditionV2(int id, Map<String, Object> map);
The corresponding xml is as follows
<select id="findByIdOrCondition" resultMap="BaseResultMap">
select <include refid="money_po"/> from money where id = #{id} or `name`=#{map.name}
</select>
<select id="findByIdOrConditionV2" resultMap="BaseResultMap">
select <include refid="money_po"/> from money where id = #{param1} or `name`=#{param2.name}
</select>
6. Summary
This article mainly introduces several postures of passing parameters in mybatis:
- In the default scenario, when there is a single parameter, any name can be used to replace the parameter in the xml file
- In the default scenario, when there are multiple parameters, the first parameter can be represented by param1 or arg0, and the second parameter can be represented by param2 or arg1. . .
- When it is a single parameter and it is a map, you can directly use the key of the map as a parameter
- Single parameter, when pojo object, use the object's fieldName to represent the parameter
- The value defined in the @Param annotation indicates that this parameter is associated with the placeholder mapping in xml
- In the multi-parameter scenario, when a simple object + map/pojo, the parameter placeholder in map/pojo
paramN.xxx
can be completed by
The last question is, how does mybatis associate and map the parameters in the mapper interface with the placeholders in the xml?
How to predict the future, and see the details below; I am ashes, welcome everyone to pay attention to the return visit
III. Source code and related knowledge points that cannot be missed
0. Project
- Engineering: https://github.com/liuyueyi/spring-boot-demo
- Source code: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/103-mybatis-xml
Blog series
- [DB series] The basic posture of CURD in Mybatis series tutorials
- [DB series] Mybatis series of tutorials for the basic use of CURD - Annotation
1. WeChat public account: Yihuihui Blog
It is not as good as a letter. The above content is purely from the family. Due to limited personal ability, it is inevitable that there will be omissions and mistakes. If you find bugs or have better suggestions, you are welcome to criticize and correct them. Thank you
The following is a gray personal blog, recording all blog posts in study and work, welcome everyone to visit
- A Huihui Blog Personal blog https://blog.hhui.top
- A gray Blog-Spring special blog http://spring.hhui.top