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 is the knowledge of MyBatis3.X complex Sql query

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail about MyBatis3.X complex Sql query related knowledge, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

MyBatis3.X complex Sql query resultMap of MyBatis3.X

The sql statement of 1.Mybatis returns two kinds of results

ResultType

The queried field must have the same field or basic data type in the corresponding pojo.

Suitable for simple query

ResultMap

Need custom fields, or multi-table query, one-to-many and other relationships, more powerful than resultType

Suitable for complex query

Select id, title as video_tile, summary, cover_img from video where id = # {video_id} ResultMap association of one-to-one query result mapping for complex objects

Association: maps to a complex type attribute of POJO, such as an order order object that contains a user object

Select o.id id, o.user_id, o.out_trade_no, o.state, o.total_fee, o.video_id, o.video_title, u.name, u.head_img, u.phone from video_order o left join user u on o.user_id = u.id

Code

/ / resultmap association associative query VideoOrderMapper videoOrderMapper = sqlSession.getMapper (VideoOrderMapper.class); List videoOrderList = videoOrderMapper.queryVideoOrderList (); System.out.println (videoOrderList.toString ()); collection of one-to-many query result mapping for ResultMap complex objects

Collection: query mapping of one-to-many query results, for example, user has multiple orders

Select u.id, u.name, u.head_img, u.phone, o.id order_id, o.out_trade_no, o.user_id, o.state, o.total_fee, o.video_id, o.video_title from user u left join video_order o on u.id = o.user_id

Code

/ / resultmap association association query VideoOrderMapper videoOrderMapper = sqlSession.getMapper (VideoOrderMapper.class); / / resultmap collection test List userList = videoOrderMapper.queryUserOrder (); System.out.println (userList.toString ()); Mybatis3.X ResultMap complex object query summary ResultMap complex object query

Association maps a pojo class that handles one-to-one associations.

A list of collections mapped by collection that deals with one-to-many associations.

Template

This is the end of this article on "MyBatis3.X complex Sql query related knowledge". 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, please share it out for more people to see.

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

Development

Wechat

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

12
Report