instructions

This tutorial relies on the id primary key in the User table. If you don’t have a user table, check out my blog to see how to create a User table in SpringBoot

Mysql statement


--
-- table structure 'company
--

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

--
-- Dump data in table 'company'
--

INSERT INTO `company` (`id`.`uid`.`name`) VALUES
(1.1.Beijing XXXX Company);

--
-- Index of the dump table
--

--
-- Table index 'company
--
ALTER TABLE `company`
  ADD PRIMARY KEY (`id`),
  ADD KEY `uid` (`uid`);

--
-- Use AUTO_INCREMENT in the exported table
--

--
Alter table AUTO_INCREMENT 'company
--
ALTER TABLE `company`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- Restrict exported tables
--

--
-- restrict table '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 */;

Copy the code

Remember that there must be a user table, and the user table must have an int id field, if you do not create or read the blog chapter 1 to create after this chapter to learn

Add the Company entity class

import lombok.Data; @Data public class Company { private int id; Private int uid; // Which User does the company belong to? Private String name; private String name; private String name; // Company name}Copy the code

The user object is assigned to the entity class by the uid query

Modify UserMapper


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

This method only queries ID and user, not pass, because the password is not returned to the customer, but provided to Company through ID query

Add 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(a);
}

Copy the code

Property which attribute receives the result of a join table query uid which field is used for a join table query one = @one one one-to-one relationship @ one (select = “Www.td0f7.cn.springboot.springboot.mapper.UserMapper.findById”), the writing is the full path to the mapper. The method name

Call findById in UserMapper and return the value assigned to the user object

Since all the contents of the table are queried, the returned data may be multiple rows, so the return value is List

Add 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(a) {
        List<Book> books = mapper.findALl();
        if (books == null || books.size() == 0) return new BaseResult(500."No data found!"."");

        return new BaseResult(200."", books); }}Copy the code

Very simple, if the query data is not null and greater than 0, the query is successful, otherwise no data error message is returned

So let’s test that out

{
    "code": 200."msg": ""."data": [{"id": 1."uid": 0."user": {
                "id": 1."user": "wz"."pass": null
            },
            "name": Beijing XXXX Company}}]Copy the code

Pass = null; id = null; user = null

Bilibili video tutorials are updated synchronously