• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring boot2基于Mybatis实现多表关联查询

java 搞代码 4年前 (2022-01-05) 56次浏览 已收录 0个评论

这篇文章主要介绍了Spring boot2基于Mybatis实现多表关联查询,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

模拟业务关系:

一个用户user有对应的一个公司company,每个用户有多个账户account。

spring boot 2的环境搭建见上文:spring boot 2整合mybatis

一、mysql创表和模拟数据sql

 CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `company_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `company` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES (1, 'aa', 1), (2, 'bb', 2); INSERT INTO `company` VALUES (1, 'xx公司'), (2, 'yy公司'); INSERT INTO `account` VALUES (1, '中行', 1), (2, '工行', 1), (3, '中行', 2);

二、创建实体

 public class User { private Integer id; private String name; private Company company; private List accounts; //getter/setter 这里省略... } public class Company { private Integer id; private String companyName; //getter/setter 这里省略... } public class Account { private Integer id; private String accountName; //getter/setter 这里省略... }

三、开发Mapper

方法一:使用注解

1、AccountMapper.java

 package com.example.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import com.example.demo.entity.Account; public interface AccountMapper { /* * 根据用户id查询账户信息 */ @Select("SELECT * FROM `account` WHERE user_id = #{userId}") @Results({ @Result(property = "accountName", column = "name") }) List getAccountByUserId(Long userId); }

2、CompanyMapper.java

 package com.example.demo.mapper; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import com.example.demo.entity.Company; public interface CompanyMapper { /* * 根据公司id查询公司信息 */ @Select("SELECT * FROM company WHERE id = #{id}") @Results({ @Result(property = "companyName", column = "name") }) Company getCompanyById(Long id); }

3、UserMapper.java

 package com.example.demo.mapper; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Many; import com.example.demo.entity.User; public interface UserMapper { /* * 一对一查询 * property:查询结果赋值给此实体属性 * column:对应数据库的表字段,做为下面@One(select方法的查询参数 * one:一对一的查询 * @One(select = 方法全路径) :调用的方法 */ @Select("SELECT * FROM user WHERE id = #{id}") @Results({ @Result(property = "company", column = "company_id", one = @One(select = "com.example.demo.mapper.CompanyMapper.getCompanyById")) }) User getUserWithCompany(Long id); /* * 一对多查询 * property:查询结果赋值给此实体属性 * column:对应数据库的表字段,可做为下面@One(select方法)的查询参数 * many:一对多的查询 * @Many(select = 方法全路径) :调用的方法 */ @Select("SELECT * FROM user WHERE id = #{id}") @Results({ @Result(property = "id", column = "id"),//加此行,否则id值为空 @Result(property = "accounts", column = "id", many = @Many(select = "com.example.demo.mapper.AccountMapper.getAccountByUserId")) }) User getUserWithAccount(Long id); /* * 同时用一对一、一对多查询 */ @Select("SELECT * FROM user") @Results({ @Result(property = "id", column = "id"), @Result(property = "company", column = "company_id", one = @One(select = "com.example.demo.mapper.CompanyMapper.getCompanyById")), @Result(property = "accounts", column = "i<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码$网</span>d", many = @Many(select = "com.example.demo.mapper.AccountMapper.getAccountByUserId")) }) List getAll(); }

方法二:使用XML

参考上文spring boot 2整合mybatis配置application.properties和mybatis-config.xml等后,
以上面的getAll()方法为例,UserMapper.xml配置如下:

    <!--封装映射company表数据,user表与company表1对1关系,配置1对1的映射 association:用于配置1对1的映射 属性property:company对象在user对象中的属性名 属性javaType:company属性的java对象 类型 属性column:user表中的外键引用company表 --> <!--配置1对多关系映射 property:在user里面的List的属性名 ofType:当前account表的java类型 column:外键 -->  SELECT u.id,u.name,c.id companyid, c.name companyname, a.id accountid,a.name accountname FROM user u LEFT JOIN company c on u.company_id=c.id LEFT JOIN account a on u.id=a.user_id 

四、控制层

 package com.example.demo.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; @RestController public class UserController { @Autowired private UserMapper userMapper; //请求例子:http://localhost:9001/getUserWithCompany/1 /*请求结果:{"id":1,"name":"aa","company":{"id":1,"companyName":"xx公司"},"accounts":null}*/ @RequestMapping("/getUserWithCompany/{id}") public User getUserWithCompany(@PathVariable("id") Long id) { User user = userMapper.getUserWithCompany(id); return user; } //请求例子:http://localhost:9001/getUserWithAccount/1 /*请求结果:{"id":1,"name":"aa","company":null,"accounts":[{"id":1,"accountName":"中行"},{"id":2,"accountName":"工行"}]}*/ @RequestMapping("/getUserWithAccount/{id}") public User getUserWithAccount(@PathVariable("id") Long id) { User user = userMapper.getUserWithAccount(id); return user; } //请求例子:http://localhost:9001/getUserWithAccount/1 /*请求结果:[{"id":1,"name":"aa","company":{"id":1,"companyName":"xx公司"},"accounts":[{"id":1,"accountName":"中行"}, {"id":2,"accountName":"工行"}]},{"id":2,"name":"bb","company":{"id":2,"companyName":"yy公司"},"accounts":[{"id":3,"accountName":"中行"}]}]*/ @RequestMapping("/getUsers") public List getUsers() { List users=userMapper.getAll(); return users; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Spring boot2基于Mybatis实现多表关联查询的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring boot2基于Mybatis实现多表关联查询

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址