Master One-to-One and One-to-Many MyBatis Mappings with Real-World Examples

This tutorial walks through implementing one-to-one and one-to-many relationships in MyBatis, showing database tables, entity classes, XML mapping files, key configuration attributes, and test results with clear diagrams and code snippets.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master One-to-One and One-to-Many MyBatis Mappings with Real-World Examples

1. One-to-One

Scenario: In everyday life each person has an ID card, representing the simplest one-to-one relationship.

(1) User table

(2) ID card table

(3) User entity class,

com.xuliugen.mybatis.demo.bean.User

(4) ID card entity class,

com.xuliugen.mybatis.demo.bean.Card

(5) User table XML mapping file

<code><!-- One-to-one relationship mapping --> <association column="card_id" property="card" select="com.xuliugen.mybatis.demo.dao.CardMapper.selectByPrimaryKey" javaType="com.xuliugen.mybatis.demo.bean.Card"/> </code>

Key points (highlighted in red):

1. column indicates the card_id field in the user table; 2. property indicates the card attribute in the User entity; 3. select uses the column value as the parameter for the method; 4. javaType specifies the returned object type.

(6) ID card table XML mapping file

(7) Test Controller

(8) Test result

2. One-to-Many

Scenario: In everyday life each class has multiple students, representing the simplest one-to-many relationship.

(1) Student table

Here, clazz_id represents the class ID.

(2) Class table

(3) Class entity class

(4) Student entity class

(5) Class table XML mapping file

Key points (highlighted in red):

1. fetchType indicates lazy loading; choose eager or lazy . Typically one-to-many associations are set to lazy loading; 2. property indicates the attribute in the parent entity that holds the collection; 3. select uses the column value as the method parameter; 4. javaType specifies the returned object type.

(6) Student table XML mapping file

(7) Test Controller

(8) Test result

Conclusion

If you need the code, download it from Baidu Cloud:

http://pan.baidu.com/s/1jItB8HK password: slti

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMyBatisORMOne-to-ManyDatabase MappingOne-to-One
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.