本篇隶属于《SSM 实战项目①丨图书管理系统 》
详情请见:SSM 实战项目①丨图书管理系统 丨(0) 项目概要及本系列介绍
SSM 实战项目①丨图书管理系统 丨(2) MyBatis Generator 的基本使用
查看数据库表
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()方法)
@Override
public 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 自动生成的部分,然后进行添加改造既即可,如下:
<resultMap id="ResultMapWithManager" type="com.bean.Reader">
<id column="reader_id" jdbcType="INTEGER" property="readerId" />
<result column="reader_name" jdbcType="VARCHAR" property="readerName" />
<result column="reader_gender" jdbcType="VARCHAR" property="readerGender" />
<result column="reader_barcode" jdbcType="VARCHAR" property="readerBarcode" />
<result column="reader_profession" jdbcType="VARCHAR" property="readerProfession" />
<result column="reader_birth" jdbcType="TIMESTAMP" property="readerBirth" />
<result column="reader_document_type" jdbcType="VARCHAR" property="readerDocumentType" />
<result column="reader_document_number" jdbcType="VARCHAR" property="readerDocumentNumber" />
<result column="reader_email" jdbcType="VARCHAR" property="readerEmail" />
<result column="reader_register_date" jdbcType="TIMESTAMP" property="readerRegisterDate" />
<result column="borrowed_book_count" jdbcType="INTEGER" property="borrowedBookCount" />
<result column="remaining_book_count" jdbcType="INTEGER" property="remainingBookCount" />
<result column="operator_id" jdbcType="INTEGER" property="operatorId" />
<association property="manager" javaType="com.bean.Manager">
<id column="manager_id" property="managerId">id>
<result column="manager_name" property="managerName">result>
association>
resultMap>
<sql id="Column_List_With_Manager">
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
sql>
<select id="selectByExampleWithManager" parameterType="com.bean.ReaderExample" resultMap="ResultMapWithManager">
select
<if test="distinct">
distinct
if>
<include refid="Column_List_With_Manager" />
FROM reader_tbl r
LEFT JOIN manager_tbl m ON r.operator_id = m.manager_id
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
if>
<if test="orderByClause != null">
order by ${orderByClause}
if>
select>
<select id="selectByPrimaryKeyWithManager" parameterType="java.lang.Integer" resultMap="ResultMapWithManager">
select
<include refid="Column_List_With_Manager" />
FROM reader_tbl r
LEFT JOIN manager_tbl m ON r.operator_id = m.manager_id
where reader_id = #{readerId,jdbcType=INTEGER}
select>
//新增两个方法:带上管理员
List<Reader> selectByExampleWithManager(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='[email protected]', readerRegisterDate=Fri May 29 11:00:00 CST 2020, borrowedBookCount=0, remainingBookCount=10, operatorId=4, manager=Manager{managerId=4, managerName='张XX', managerPwd='null'}}
测试成功。
有纰漏之处,还望不吝指出~