In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how the MyBatis framework implements query statements through xml mapping files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
What is the Mybatis framework?
MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native information, mapping interfaces and Java's POJOs (Plain Ordinary Java Object, ordinary Java objects) to records in the database.
How to use it?
Pom file dependency
Org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3
Yml file configuration, which matches the mapping file under the resource/mapper/ path.
Mybatis: mapper-locations: classpath:mapper/*.xml
Bind persistence layer interfaces and entity objects in a xml file
The namespace attribute in the mapper tag specifies the project path of our persistence layer interface
ResultMap is the most powerful element of Mybatis. It can map complex data from queries (such as queries to data in several tables) to a result set.
The type attribute of the resultMap tag is the project path of the entity object we want to map, and id is the unique identity of the resultMap.
The result tag in resultMap is the binding between the entity and the database table field, where the property attribute is the attribute name of the entity object, column is the field name of the database, and jdbcType is the field type.
Sql programming of xml Mapping File
Query through entities as filter criteria
Select id, host_name, ip, user_name, pass_word, state, tag, gmt_created, gmt_modified from host and id = # {id} and host_name like CONCAT ('%', # {hostName},'%') and ip like CONCAT ('%' # {ip},'%') and user_name = # {userName} and pass_word = # {passWord} and state = # {state} and tag = # {tag} and gmt_created = # {gmtCreated} and gmt_modified = # {gmtModified} / if >
Id= "queryAll" is the abstract method name of the persistence layer interface
The resultMap= "HostMap" specifies the result set of the resultMap received by the query result.
Persistence layer interface binding
/ * * conditional query * * @ param host conditional query * @ return object list * / List queryAll (Host host)
Batch delete through primary key
Delete from host where id in # {id}
The prototype of the above sql statement is
Delete from host where id in (1, 2, 3)
Attribute understanding in foreach tags
The collection property is the received data source
Item is every element in the collection
Index: used to indicate the location of each iteration during the iteration
Open: indicates what the statement starts with
Separator: indicates what symbol is used as a delimiter for data during iteration
Close: indicates what to end with
Abstract method of persistence layer interface
/ * bulk delete hosts * * @ param hostIds host id array * @ return Integer * / Integer deleteHostByIds (@ Param ("hostIds") Long [] hostIds)
Batch addition
Insert into host_and_group (host_group_id, host_id) values (# {hostGroupId}, # {hostId})
Persistent layer interface method
/ * add multiple hosts to a host group * * @ param request * @ return Integer * / Integer addHostList (HostAndGroupRequest request)
I am an element encapsulated in an object, so if there are a batch of elements in this object, you can pass an object directly.
Thank you for reading! On "MyBatis framework how to write query statements through xml mapping file" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
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.