Get the App
SLTechnology News&Howtos  ›  Database  › 

One-to-many Mapping of mybatis

Shulou Source: shulou.com Published: 2022-06-01 03:29:34 10月03日 Update

Continue the one-to-one problem of mybatis, or the one-on-one example above (http://fengcl.blog.51cto.com/9961331/1875657)

What if a user has more than one work? This involves an one-to-many problem. Similarly, mybatis one-to-many can still be solved in two ways.

1. Implement one-to-many mapping using embedded ResultMap

1) entity

Public class User implements Serializable {private static final long serialVersionUID = 112596782083832677L; private Integer id; / / numbered private String email; / / mailbox private String realName; / / real name private String telephone; / / telephone number private List worksInfos; / / works / / get,set method.} public class WorksInfo implements Serializable {private Integer id; private Integer userId Private Date uploadDate; / / upload time private Date updateDate; / / update time / / get,set method.}

2) dao interface is omitted.

3) mapper mapping file

Select u.resume. * from user u left join worksInfo w on u.id = w.userId where u.id = # {id}

4) Test omission

Second, realize one-to-many by nested query.

1) the entity class is as above

2) dao layer interface omitted

3) mapper file mapping

Select * from worksInfo where userId = # {userId} select * from user where id = # {id}

4) Test method ignoring

Note: the column attribute in the collention element, that is, the condition in the main table to be passed to the secondary table for query, such as in this case:

Timely pass the id field in the user table to the findWorksInfoByUserId method for parameter use, corresponding to the userId field in the worksInfo table. In addition, nesting select statements can cause problems with Number1. First, the main query will be executed (once), for the main

For each row returned by the query, another query will be executed (the main query N rows, then this query N times). For

For large databases, this can lead to poor performance problems.

Tags: Queries methods questions works fields entities interfaces files methods time tests one-to-one one-line examples elements parameters numbers multiple names attributes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Shulou Tech Info Docker NVidia