本文共 3982 字,大约阅读时间需要 13 分钟。
本篇隶属于《SSM 实战项目①丨图书管理系统 》
详情请见:
查看数据库表
reader_tbl 中有 operator_id 属性,对应到 manager_tbl,我们希望在查询 reader 信息的时候,顺带把 manager 的信息也查询到。
//1. 添加一个 Manager 属性,用作联合查询private Manager manager;//2. 为 manger 属性生成 getter 和 setter 方法public Manager getManager() { return manager;}public void setManager(Manager manager) { this.manager = manager;}//3. 生成无参构造函数public Reader() { }//4. 生成有参构造函数(不要带上 manager)public Reader(Integer readerId, String readerName, String readerGender, String readerBarcode, String readerProfession, Date readerBirth, String readerDocumentType, String readerDocumentNumber, String readerEmail, Date readerRegisterDate, Integer borrowedBookCount, Integer remainingBookCount, Integer operatorId) { this.readerId = readerId; this.readerName = readerName; this.readerGender = readerGender; this.readerBarcode = readerBarcode; this.readerProfession = readerProfession; this.readerBirth = readerBirth; this.readerDocumentType = readerDocumentType; this.readerDocumentNumber = readerDocumentNumber; this.readerEmail = readerEmail; this.readerRegisterDate = readerRegisterDate; this.borrowedBookCount = borrowedBookCount; this.remainingBookCount = remainingBookCount; this.operatorId = operatorId;}//5. 生成 toString()方法:带上 manager(注意,manager实体类也要加上 toString()方法)@Overridepublic String toString() { return "Reader{" + "readerId=" + readerId + ", readerName='" + readerName + '\'' + ", readerGender='" + readerGender + '\'' + ", readerBarcode='" + readerBarcode + '\'' + ", readerProfession='" + readerProfession + '\'' + ", readerBirth=" + readerBirth + ", readerDocumentType='" + readerDocumentType + '\'' + ", readerDocumentNumber='" + readerDocumentNumber + '\'' + ", readerEmail='" + readerEmail + '\'' + ", readerRegisterDate=" + readerRegisterDate + ", borrowedBookCount=" + borrowedBookCount + ", remainingBookCount=" + remainingBookCount + ", operatorId=" + operatorId + ", manager=" + manager + '}';}
PS:这里进行改造的时候,我们可以参考 MBG 自动生成的部分,然后进行添加改造既即可,如下:
r.reader_id, r.reader_name, r.reader_gender, r.reader_barcode, r.reader_profession, r.reader_birth, r.reader_document_type, r.reader_document_number, r.reader_email, r.reader_register_date, r.borrowed_book_count, r.remaining_book_count, r.operator_id, m.manager_id, m.manager_name
//新增两个方法:带上管理员ListselectByExampleWithManager(ReaderExample example);Reader selectByPrimaryKeyWithManager(Integer readerId);
这里就改造完成了。
编写一个测试类,调用 mapper 接口中的方法即可测试,下面举例:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:applicationContext.xml"})public class MapperTest { @Autowired ReaderMapper readerMapper; @Test public void mapperTest(){ //调用 readerMapper 中的 selectByPrimaryKeyWithManager,查看控制台信息,检查是否实现了联合查询 Reader reader = readerMapper.selectByPrimaryKeyWithManager(10); System.out.println(reader); }}
输出结果:
Reader{ readerId=10, readerName='邱筹妈', readerGender='男', readerBarcode='ab1eb7a', readerProfession='学生', readerBirth=Sun Oct 05 11:00:00 CST 2003, readerDocumentType='学生证', readerDocumentNumber='2018302178736', readerEmail='ab1eb7a@whu.edu.cn', readerRegisterDate=Fri May 29 11:00:00 CST 2020, borrowedBookCount=0, remainingBookCount=10, operatorId=4, manager=Manager{ managerId=4, managerName='张XX', managerPwd='null'}}
测试成功。
有纰漏之处,还望不吝指出~
转载地址:http://emded.baihongyu.com/