說明
本章課程依賴于user表中id主鍵猛遍,如果您沒有user表顶捷,則翻看我的博客查看springboot第一章登陸接口教程創(chuàng)建user表
mysql語句
--
-- 表的結(jié)構(gòu) `company`
--
CREATE TABLE `company` (
`id` int(11) NOT NULL,
`uid` int(11) NOT NULL,
`name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 轉(zhuǎn)存表中的數(shù)據(jù) `company`
--
INSERT INTO `company` (`id`, `uid`, `name`) VALUES
(1, 1, '北京xxxx公司');
--
-- 轉(zhuǎn)儲(chǔ)表的索引
--
--
-- 表的索引 `company`
--
ALTER TABLE `company`
ADD PRIMARY KEY (`id`),
ADD KEY `uid` (`uid`);
--
-- 在導(dǎo)出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `company`
--
ALTER TABLE `company`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
--
-- 限制導(dǎo)出的表
--
--
-- 限制表 `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字段口芍,如果沒有自己創(chuàng)建或者翻看博客第一章進(jìn)行創(chuàng)建后在進(jìn)行本章學(xué)習(xí)
添加Company實(shí)體類
import lombok.Data;
@Data
public class Company {
private int id;//主鍵自增長
private int uid;//該公司屬于哪個(gè)用戶
private User user;//這個(gè)數(shù)據(jù)庫中沒有杖剪,但是我們通過查詢給這個(gè)對(duì)象賦值
private String name;//公司名稱
}
看到實(shí)體類多了一個(gè)user對(duì)象蠢笋,這個(gè)是我們通過uid查詢用戶賦值到user對(duì)象上
修改 UserMapper
@Select("select id,user from user where id = #{id}")
User findById(int id);
本方法只查詢id,user拨齐,不查詢pass,因?yàn)槊艽a不返回給客戶昨寞,通過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代表哪個(gè)屬性接收連表查詢的結(jié)果
uid代表哪個(gè)字段用于連表查詢
one = @One代表1對(duì)1關(guān)系
@One(select = "www.td0f7.cn.springboot.springboot.mapper.UserMapper.findById")這個(gè)寫的是mapper的完整路徑.方法名
以上代表瞻惋,uid做為傳值調(diào)用UserMapper中的findById方法,返回值賦值給user對(duì)象
由于查詢表內(nèi)所有內(nèi)容编矾,返回?cái)?shù)據(jù)有可能是多行熟史,所以返回值是 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, "沒有查詢到數(shù)據(jù)!", "");
return new BaseResult(200, "", books);
}
}
很簡單窄俏,查詢數(shù)據(jù)如果不是null并且大于0則代表查詢成功蹂匹,否則則返回?zé)o數(shù)據(jù)錯(cuò)誤提示
接下來測試一下
在這里插入圖片描述
{
"code": 200,
"msg": "",
"data": [
{
"id": 1,
"uid": 0,
"user": {
"id": 1,
"user": "wz",
"pass": null
},
"name": "北京xxxx公司"
}
]
}
可以看到多了一個(gè)user對(duì)象,pass由于我們沒有查詢所以是null凹蜈,id限寞、user已經(jīng)查詢出來了