In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of multi-table operation and annotation development of the Mybatis framework in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
One-to-one query
Model of one-to-one query
The relationship between the user table and the order table is that a user has multiple orders, and an order belongs to only one user.
Demand for one-to-one query: query an order and at the same time find out who the order belongs to
One-to-one query statement
The corresponding sql statement: select * from orders ojournal user u where o.uidqiu.id; the result of the query is as follows:
Create Order and User entities
Create an OrderMapper interface
Public interface OrderMapper {/ / query all methods public List findAll ();}
Configure OrderMapper.xml
SELECT *, o.id oid FROM orders omega user u WHERE o.uid=u.id
Configure UserMapper.xml
SELECT *, o.id oid FROM USER uorder o WHERE u.id=o.uid SELECT * FROM USER uMagazine syscraper usernames role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
Configure sqlMapConfig.xml
Test code
@ Test public void test1 () throws IOException {InputStream resourceAsStream = Resources.getResourceAsStream ("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession (); OrderMapper mapper = sqlSession.getMapper (OrderMapper.class); List orderList = mapper.findAll (); for (Order order: orderList) {System.out.println (order);} sqlSession.close () }
One-to-many query
One-to-many query model
The relationship between the user table and the order table is that a user has multiple orders, and an order only belongs to the one-to-many query needs of one user: query a user, and at the same time query the orders that the user has.
The corresponding sql statement: select *, o.id oid from user u left join orders o on u.iddistribuo.uid; the result of the query is as follows:
SELECT *, o.id oid FROM USER u, orders o WHERE u.id=o.uid
Modify User solid
Create a UserMapper interface
Configure UserMapper.xml
Test result
Many-to-many query
Many-to-many query model
The relationship between the user table and the role table is that a user has multiple roles, and a role is used by multiple users for many-to-many queries: query users query all roles of the user at the same time.
Many-to-many query statement
The corresponding sql statement: select u.recording journal r.recording journal r.id rid from user u left join user_role ur on u.id=ur.user_idinner join role r on ur.role_id=r.id
The query results are as follows:
Create Role entities and modify User entities
Add UserMapper interface method
Configure UserMapper.xml (above)
Test code
Summary:
MyBatis multi-table configuration method:
One-to-one configuration: using for configuration
One-to-many configuration: use + for configuration
Many-to-many configuration: use + for configuration
Annotation development of Mybatis
Common notes on Mybatis
Annotation development has become more and more popular in recent years, and Mybatis can also use annotation development, so we can write fewer Mapper mapping files. We first learn around some basic CRUD, and then learn about complex mapping multi-table operations.
@ Insert: add
@ Update: implement update @ Delete: implement deletion @ Select: implement query
@ Result: implement result set encapsulation
@ Results: can be used with @ Result to encapsulate multiple result sets @ One: achieve one-to-one result set encapsulation
@ Many: implement one-to-many result set encapsulation
Private UserMapper mapper; @ Before public void before () throws IOException {InputStream resourceAsStream = Resources.getResourceAsStream ("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession (true); mapper = sqlSession.getMapper (UserMapper.class);} @ Test public void testSave () {User user = new User (); user.setUsername ("tom") User.setPassword ("abc"); mapper.save (user);} @ Test public void testUpdate () {User user = new User (); user.setId (18); user.setUsername ("lucy"); user.setPassword ("123"); mapper.update (user);} @ Test public void testDelete () {mapper.delete (18) @ Test public void testFindById () {User user = mapper.findById (2); System.out.println (user);} @ Test public void testFindAll () {List all = mapper.findAll (); for (User user: all) {System.out.println (user);}}
To modify the core configuration file of MyBatis, we use a mapping file instead of annotations, so we only need to load the Mapper interface that uses annotations
Or you can specify the package that scans the interface that contains the mapping relationship.
UserMapper:
Public interface UserMapper {@ Insert ("insert into user values (# {id}, # {username}, # {password}, # {birthday})") public void save (User user); @ Update ("update user set username=# {username}, password=# {password} where id=# {id}") public void update (User user); @ Delete ("delete from user where id=# {id}") public void delete (int id) @ Select ("select * from user where id=# {id}") public User findById (int id); @ Select ("select * from user") public List findAll () @ Select ("select * from user") @ Results ({@ Result (id=true, column = "id", property = "id"), @ Result (column = "username", property = "username"), @ Result (column = "password", property = "password"), @ Result (property = "orderList", column = "id") JavaType = List.class, many = @ Many (select = "com.longdi.mapper.OrderMapper.findByUid")}) public List findUserAndOrderAll () @ Select ("SELECT * FROM USER") @ Results ({@ Result (id = true,column = "id", property = "id"), @ Result (column = "username", property = "username"), @ Result (column = "password", property = "password"), @ Result (property = "roleList", column = "id") JavaType = List.class, many = @ Many (select = "com.longdi.mapper.RoleMapper.findByUid")}) public List findUserAndRoleAll () } annotations of MyBatis to realize complex mapping development
Before implementing complex relationship mapping, we can configure it in the mapping file. After developing with annotations, we can use @ Results annotation, @ Result annotation, @ One annotation and @ Many annotation to complete the configuration of complex relationship.
One-to-one query (configure Mapper with annotations)
Test the code:
One-to-many queries (configure Mapper with annotations)
Test code
Many-to-many queries (configure mapper with annotations)
Test the code:
On the "Java Mybatis framework multi-table operation and annotation development example analysis" this article is shared here, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.