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

One-to-one, one-to-many, many-to-one usage in MyBatis

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "one-to-one, one-to-many, many-to-one usage in MyBatis". In daily operation, I believe many people have doubts about the usage of one-to-one, one-to-many and many-to-one in MyBatis. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubts about "one-to-one, one-to-many, many-to-one usage in MyBatis!" Next, please follow the editor to study!

Introduction to MyBatis

MyBatis was originally an open source project of apache, iBatis, which was moved from apache software foundation to google code in 2010 and renamed MyBatis. Migrated to Github in November 2013. The word iBATIS, which comes from the combination of "internet" and "abatis", is a persistence layer framework based on Java. The persistence layer framework provided by iBATIS includes SQL Maps and Data Access Objects (DAOs) currently, and the latest version is MyBatis 3.5.4, which will be released on February 4, 2020.

Now that we know about MyBatis, let's look at how to use MyBatis in Java to do one-to-one, one-to-many, many-to-one queries.

Java entity class object

Public class Teacher {private Integer id; private String name; private List students

/ /. Omit the get,set method here}

Public class Student {private Integer id; private String name; private Teacher teacher / /. Omit the get,set method here}

Java Dao layer code

/ * * one-to-one: check a student's information and find his teacher * / Student queryStudentInfoById (@ Param ("id") Integer id); / * * one-to-many: check a teacher and find all the teacher's students * / Teacher queryTeacherInfoById (@ Param ("id") Integer id); / * * many-to-one: query all students and find the corresponding teacher * / List queryStudentInfo ()

One-on-one: check a student's information and find out his teacher

SELECT id,name,teacher_id FROM student WHERE id = # {id}

Select id,name from teacher where id = # {teacher_id}

One-to-many: check a teacher and find out all the teacher's students

SELECT id,name FROM teacher where id = # {id}

Select id,name from student where teacher_id = # {id}

Many to one: query all the students and find out the corresponding teachers

SELECT id,name FROM student

Select id,name from teacher where id = # {teacher_id} at this point, the study of "one-to-one, one-to-many, many-to-one usage in MyBatis" is over. I hope it can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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