美文网首页
SpringBoot主外建查询接口

SpringBoot主外建查询接口

作者: wzapp | 来源:发表于2021-03-08 13:23 被阅读0次

说明

本章课程依赖于user表中id主键,如果您没有user表,则翻看我的博客查看springboot第一章登陆接口教程创建user表

mysql语句


--
-- 表的结构 `company`
--

CREATE TABLE `company` (
  `id` int(11) NOT NULL,
  `uid` int(11) NOT NULL,
  `name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `company`
--

INSERT INTO `company` (`id`, `uid`, `name`) VALUES
(1, 1, '北京xxxx公司');

--
-- 转储表的索引
--

--
-- 表的索引 `company`
--
ALTER TABLE `company`
  ADD PRIMARY KEY (`id`),
  ADD KEY `uid` (`uid`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `company`
--
ALTER TABLE `company`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- 限制导出的表
--

--
-- 限制表 `company`
--
ALTER TABLE `company`
  ADD CONSTRAINT `company_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `user` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

切记必须要有user表,且user表中必须有 int 型 id字段,如果没有自己创建或者翻看博客第一章进行创建后在进行本章学习

添加Company实体类

import lombok.Data;

@Data
public class Company {
    private int id;//主键自增长
    private int uid;//该公司属于哪个用户
    private User user;//这个数据库中没有,但是我们通过查询给这个对象赋值
    private String name;//公司名称
}

看到实体类多了一个user对象,这个是我们通过uid查询用户赋值到user对象上

修改 UserMapper


    @Select("select id,user from user where id = #{id}")
    User findById(int id);

本方法只查询id,user,不查询pass,因为密码不返回给客户,通过id查询提供给Company使用

添加 CompanyMapper

import org.apache.ibatis.annotations.*;
import www.td0f7.cn.springboot.springboot.entity.Company;

import java.util.List;

@Mapper
public interface CompanyMapper {

    @Select("select * from company")
    @Results({
            @Result(
                    property = "user", column = "uid",
                    one = @One(select = "www.td0f7.cn.springboot.springboot.mapper.UserMapper.findById")
            )
    })
    List<Company> findAll();
}

property代表哪个属性接收连表查询的结果
uid代表哪个字段用于连表查询
one = @One代表1对1关系
@One(select = "www.td0f7.cn.springboot.springboot.mapper.UserMapper.findById")这个写的是mapper的完整路径.方法名

以上代表,uid做为传值调用UserMapper中的findById方法,返回值赋值给user对象

由于查询表内所有内容,返回数据有可能是多行,所以返回值是 List

添加BookController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.td0f7.cn.springboot.springboot.base.BaseResult;
import www.td0f7.cn.springboot.springboot.entity.Book;
import www.td0f7.cn.springboot.springboot.mapper.BookMapper;

import java.util.List;

@RestController
@RequestMapping("book")
public class BookController {
    @Autowired(required = false)
    private BookMapper mapper;

    @GetMapping
    public BaseResult findAll() {
        List<Book> books = mapper.findALl();
        if (books == null || books.size() == 0) return new BaseResult(500, "没有查询到数据!", "");

        return new BaseResult(200, "", books);
    }
}

很简单,查询数据如果不是null并且大于0则代表查询成功,否则则返回无数据错误提示

接下来测试一下

在这里插入图片描述
{
    "code": 200,
    "msg": "",
    "data": [
        {
            "id": 1,
            "uid": 0,
            "user": {
                "id": 1,
                "user": "wz",
                "pass": null
            },
            "name": "北京xxxx公司"
        }
    ]
}

可以看到多了一个user对象,pass由于我们没有查询所以是null,id、user已经查询出来了

bilibili视频教程同步更新

相关文章

网友评论

      本文标题:SpringBoot主外建查询接口

      本文链接:https://www.haomeiwen.com/subject/txieqltx.html