In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the methods of parameter transmission in Mybatis. It is very detailed and has a certain reference value. Friends who are interested must read it!
i. Environment configuration
We use SpringBoot + Mybatis + MySql to build the instance demo
Springboot: 2.2.0.RELEASE
Mysql: 5.7.22
1. Project configuration org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java
The core dependency is mybatis-spring-boot-starter. As for version selection, go to the mvn repository and find the latest
Another thing that is not available is 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 table
Database for testing
CREATE TABLE `money` (`id` int (11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar (20) NOT NULL DEFAULT''COMMENT' username', `money`int (26) NOT NULL DEFAULT'0' COMMENT 'money', `is_ deleted`tinyint (1) NOT NULL DEFAULT '0percent, `create_ at`timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT' creation time, `update_ at`update time', PRIMARY KEY (`id`), KEY `name` (`name`) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8mb4 ii. Parameter transfer
Next, let's take a look at several postures in which the parameters in the Mapper interface are mapped to the parameters in the xml file. With regard to the construction of the mybatis project, we will skip it here. The key information is as follows.
Database entity object
@ Datapublic 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
@ Mapperpublic interface MoneyMapper {}
Xml file. Under the resource folder, the directory level is exactly the same as the package path of the mapper API (follow the default Mapper API binding relationship with xml files. For more information, see several postures of Mapper API and Sql binding of SpringBoot series Mybatis)
Notes on id, name, money, is_deleted, create_at, update_at 1. @ Param
Add the @ Param annotation to the parameters of the interface and internally specify the parameter name passed to xml
A simple case is as follows
Int addMoney (@ Param ("id") int id, @ Param ("money") int money)
Focus on the above parameters
Specify the parameter name when passed to xml through @ Param
The sql in the corresponding xml file is as follows. Use # {} to implement parameter binding
Update money set money=money+# {money} where id=# {id} 2. Single parameter
Next, let's take a look at how parameters should be specified in xml in the default scenario when the @ Param annotation is not used. Because the actual results of single parameter and multi-parameter are not consistent, they are explained separately here.
In a single parameter scenario, the parameter name in xml can be indicated by any value.
The mapper interface is defined as follows
/ * when a single parameter is used, it can be expressed directly by the parameter name by default. In fact, any value in # {} can be used without any restriction. It means the only parameter * @ param id * @ return * / MoneyPo findById (int id). / * demo # {} in xml is a matching string, and you can also correctly replace * @ param id * @ return * / MoneyPo findByIdV2 (int id).
The corresponding xml file is as follows
Select from money where id=# {id} select from money where id=# {dd}
Let's take a look at the findByIdV2 above. The parameter passed in the above sql uses # {dd}, which is 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: the parameter in the interface represented by n
Arg0... N
Mybatis automatically encapsulates a param1 when the parameter name is not specified. Map of paramN, where n represents the nth parameter * you can also use arg0...n to refer to the specific parameter * * @ param name * @ param money * @ return * / List findByNameAndMoney (String name, Integer money)
The corresponding xml is as follows
Select-from money where name=# {param1} and money=# {param2} from money where name=# {arg0} and money=# {arg1}
Note that in the xml above, both kinds of parameter transfer are possible. Of course, it is not recommended to use this default method to pass parameters, because it is very unintuitive and not elegant for subsequent maintenance.
3. Pass parameters by Map
If the parameter type is not a simple type, the parameters in the xml file can be directly referred to by the corresponding key in map when the Map type is at that time.
If the parameter type is map, you can use key directly * @ param map * @ return * / List findByMap (Map map)
The corresponding xml is as follows
Select from money id = # {id} AND name=# {name} AND money=# {money} 4. POJO object
Another common case is to pass parameters to simple entity objects. At this time, the parameters in xml can also be directly referred to by the object's fieldName, which is similar to the way map is used.
/ * Parameter type is java object, and you can also use field name directly * @ param po * @ return * / List findByPo (MoneyPo po)
The corresponding xml file is as follows
Select from money id = # {id} AND name=# {name} AND money=# {money} 5. Simple parameter + Map parameter
When there are multiple parameters, some of them are simple types and some are Map, how to deal with the parameters in such a scenario?
Simple types follow the above rules
Pass parameters to the map parameter, using the prefix + "." + key
An example is as follows
List findByIdOrCondition (@ Param ("id") int id, @ Param ("map") Map map); List findByIdOrConditionV2 (int id, Map map)
The corresponding xml is as follows
Select from money where id = # {id} or `name` = # {map.name} select from money where id = # {param1} or `name` = # {param2.name} all the contents of this article "what are the methods for passing parameters in Mybatis"? thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.