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

How to use association and collection in Mybatis

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

Share

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

This article will explain in detail how to use association and collection in Mybatis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Association and collection usage 1. Single association query association

1.1 representation of associations between entities

Package com.worldly.config.entity;import java.io.Serializable;/** * @ Description * @ Author xiaoqx * @ Version V1.0.0 * @ Since 2017-11-26 * / public class Employee implements Serializable {private Integer id; private String name; private String email; private String tel; / / Associated departmental entity. When you query a person, you can query the department information to private Department dep. Public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public String getEmail () {return email;} public void setEmail (String email) {this.email = email } public String getTel () {return tel;} public void setTel (String tel) {this.tel = tel;} public Department getDep () {return dep;} public void setDep (Department dep) {this.dep = dep } @ Override public String toString () {return "{\" Employee\ ": {" + "\" id\ ":\"+ id +"\ "" + ",\" name\ ":\"+ name +"\ "" + ",\" email\ ":\"+ email +"\ "" + "+" \ "tel\":\ "+ tel +"\ "" + ",\" dep\ ":" + dep + "}" }}

1.2 two ways of associative query

/ / the first method: directly perform the association query to configure the attributes of the associated entity in xml / / and then find out the association SELECT * FROM t_emp e INNER JOIN t_dep d ON E.emp_dep = d.dep_id / / query method in the second Use select in association to query select * from t_emp SELECT * FROM t_dep d WHERE d.dep_id = # {emp_dep}

1.3 the advantages and disadvantages of the two ways

a. The query conditions are the same and the time taken: the test results show that the associated query is faster than the nested query (the results are not necessarily accurate, and the results may change when there are many associated tables)

a. The query conditions are the same and the time taken: the test results show that the associated query is faster than the nested query (the results are not necessarily accurate, and the results may change when there are many associated tables)

b. Applicable situation

two。 Multiple associated queries collection

2.1 representation of associations between entities

Package com.worldly.config.entity;import java.util.List;/** * @ Description * @ Author xiaoqx * @ Version V1.0.0 * @ Since 1.0 * @ Date 2017-12-16 * / public class Department {private int id; private String name; private String addr; List employeeList; public int getId () {return id;} public void setId (int id) {this.id = id } public String getNamel () {return name;} public void setNamel (String name) {this.name = name;} public String getAddr () {return addr;} public void setAddr (String addr) {this.addr = addr;} public List getEmployeeList () {return employeeList;} public void setEmployeeList (List employeeList) {this.employeeList = employeeList } @ Override public String toString () {return "{\" Department\ ": {" + "\" id\ ":\"+ id +"\ "" + ",\" name\ ":\"+ name +"\ "" + ",\" addr\ ":\"+ addr +"\ "" + "+" \ "employeeList\": "+ employeeList +"}} " }}

2.2 two ways of associative query

/ / the first way is to nest query SELECT * FROM t_dep d WHERE d.dep_id = # {param} SELECT * FROM T_emp e WHERE e.emp_dep = # {dep_id} / / the association query SELECT * FROM t_dep d INNER JOIN t in the second mode _ emp e ON d.dep_id = e.emp_dep WHERE d.dep_id = # {param}

2.3 Multi-conditional query

SELECT * FROM t_dep d WHERE d.dep_id = # {param} SELECT * FROM t_emp e WHERE e.emp_dep = # {depId} AND e.emp_status=# {status}

Multi-conditional query, using {} to wrap the method

3. Discriminator discriminator

3.1 scenarios where the discriminator is applicable

3.2 implementation of discriminator

Usage of association and collection associative query

Only the simplest usage is done here. Please check other methods by yourself.

One-to-many collection

Give an example

One to one & many to one

Give an example

This is the end of the article on "how to use association and collection in Mybatis". 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 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