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

What are the knowledge points of MyBatis?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain to you in detail what are the knowledge points about MyBatis, the editor thinks it is very practical, so I will share it with you for reference. I hope you can get something after reading this article.

4.1 Annotation pattern development

The early MyBatis was a XML-driven framework. The configuration information is based on XML, and the mapping statement is defined in XML. In MyBatis 3, other configuration methods are provided. MyBatis 3 is built on a comprehensive and powerful configuration API based on the Java language. It is the basis for XML and annotation configuration. Annotations provide a simple and low-cost way to implement simple mapping statements.

It is suggested that the expressive ability and flexibility of Java annotations are very limited. Although the MyBatis team spends a lot of time investigating, designing, and experimenting, the most powerful MyBatis maps cannot be built with annotations.

Experience is more convenient to use annotations for single table operations, but for complex multiple tables, it is difficult to maintain annotated schemas. It is recommended to use XML.

Common notes

Annotated use @ Insert New @ Update Update @ Delete Delete @ Select query @ Result Encapsulation result set @ Results works with @ Result to encapsulate multiple result sets @ One to achieve one-to-one result set encapsulation @ Many to achieve one-to-many result set encapsulation

The following query cases and requirements are consistent with the XML schema, except that the xml operation is replaced with an annotation operation

The query operation for annotations does not require a Mapper.xml configuration file, because the configuration and result mapping of sql are implemented through annotations

SqlMapConfig.xml update

4.1.1 simple operation

The basic operations of adding, updating, querying and deleting a single table are as follows. The sql statement is the same as xml, which is not described here.

Public interface UserMapper {/ / add @ Insert (value = "insert into user (id, username, `password`, birthday) values (# {id}, # {username}, # {password}, # {birthday})") void addUser (User user); / / Update @ Update (value = "update user set username = # {username}, `password` = # {password}, birthday = # {birthday} where id = # {id}") void updateUser (User user) / / query @ Select (value = "select * from user") List selectAll (); / / delete @ Delete (value = "delete from user where id = # {id}") void deleteUser (Integer id);} 4.1.2 one-to-one query

Execution process:

1) execute the sql statement of the @ Select tag.

2) the statement that executes the @ Results tag encapsulates the query result of @ Select.

3) when encapsulating the result, one attribute is user, and the internal one attribute is associated with another query result. So it will go to UserMapper and execute selectById to query the results according to column=uid with the uid of order as a parameter.

/ order one-to-one query public interface OrdersMapper {/ / select * from orders o left join user u on o.uid = u.id @ Results ({@ Result (property = "id", column = "id"), @ Result (property = "ordertime", column = "ordertime"), @ Result (property = "total", column = "total"), @ Result (javaType = User.class) Property = "user", column = "uid", one = @ One (select = "com.zyj.mapper.UserMapper.selectById")}) @ Select (value = "select * from orders") List findAllOrderAndUser () } / / user query public interface UserMapper {@ Select (value = "select * from user where id = # {id}") User selectById (Integer id);}

Summary of the corresponding relationship between comments and tags:

Comment labels @ Selectselect@ResultsresultMap@Resultid and

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

Internet Technology

Wechat

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

12
Report