Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of mybatis Mapping XML File

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of mybatis mapping XML file, the article is very detailed, has a certain reference value, interested friends must read it!

Mybatis Mapping XML Fil

A simple mapping file:

Of course, there are no elements in this file.

The Mapper XML files have only a few first class elements:

Cache-Configuration of the cache for a given namespace.

Cache-ref-Reference to a cache configuration from another namespace.

ResultMap-The most complicated and powerful element that describes how to load your objects from the database result sets.

Sql-A reusable chunk of SQL that can be referenced by other statements.

Insert-A mapped INSERT statement.

Update-A mapped UPDATE statement.

Delete-A mapped DELETE statement.

Select-A mapped SELECT statement.

Select

A simple example:

SELECT * FROM PERSON WHERE ID = # {id}

Select also has many properties that you can configure:

Insert, update and delete

Statement:

Insert into Author (id,username,password,email,bio) values (# {id}, # {username}, # {password}, # {email}, # {bio}) update Author set username = # {username}, password = # {password}, email = # {email}, bio = # {bio} where id = # {id} delete from Author where id = # {id}

F your database supports auto-generated key fields (e.g. MySQL and SQL Server), the above insert statement can be written as:

Insert into Author (username,password,email,bio) values (# {username}, # {password}, # {email}, # {bio})

If your database also supports multiple record inserts, you can use the following statement:

Insert into Author (username, password, email, bio) values (# {item.username}, # {item.password}, # {item.email}, # {item.bio})

Sql

This element can define some fragments of sql code and then use it in multiple statements to reduce coupling. For example:

${alias} .id, ${alias} .username, ${alias} .password

Then use it in the following statement:

Select, from some_table t1 cross join some_table t2

Result Maps

The official website gives the most complicated example.

In general, it means that a blog system has an author, many blog posts, a lot of comments, a lot of tags (including one-to-many, one-to-one)

Select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, A.favourite_section as author_favourite_section, P.id as post_id, P.blog_id as post_blog_id P.author_id as post_author_id, P.created_on as post_created_on, P.section as post_section, P.subject as post_subject, P.draft as draft, P.body as post_body, C.id as comment_id, C.post_id as comment_post_id, C.name as comment_name, C.comment as comment_text, T.id as tag_id T.name as tag_name from Blog B left outer join Author An on B.author_id = A.id left outer join Post P on B.id = P.blog_id left outer join Comment C on P.id = C.post_id left outer join Post_Tag PT on PT.post_id = P.id left outer join Tag T on PT.tag_id = T.id where B.id = # {id} This is all the content of the article "sample Analysis of mybatis Mapping XML Files" 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report