当数据库中表有外键时,增删改查的操作就要涉及两个表,之前的操作仅涉及一个表,下面将介绍涉及两个表该如何编写代码
数据库情况:apply表中有字段goods_id作为外键,它是goods表的主键id字段。
一\前提情况
1、项目配置
idea+mysql 5.x
apache-maven-3.3.9 + spring-2.0.3.RELEASE + repository
(可参考 )
2、先实现两个表中只涉及一个表的基本的增删改查
(可参考 )
二\代码编写
1\ApplyExtend.java
1、继承 Apply类
2、声明 Goods
private Goods goods;
3、实现get|set方法
整体代码:
package com.bs.mstp.mstp01.bean.extend;
import com.bs.mstp.mstp01.bean.Apply;
import com.bs.mstp.mstp01.bean.Goods;
public class ApplyExtend extends Apply {
private Goods goods;
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
}
2\ApplyExtendMapper.java
1、编写所需要的方法:例如在查询申请信息时,需要同时通过goods_id查看所申请的物品信息,在这里我们命名为findAll
List<ApplyExtend> findAll();
整体代码:
package com.bs.mstp.mstp01.dao.extend;
import com.bs.mstp.mstp01.bean.extend.ApplyExtend;
import java.util.List;
public interface ApplyExtendMapper {
List<ApplyExtend> findAll();
}
3\ApplyExtendMapper.xml
1、在<mapper>标签的 namespace 属性,使用对应的dao.extend: com.bs.mstp.mstp01.dao.extend.ApplyExtendMapper
2、在<select>标签的 resultType 属性删除,需要编写新的封装方式,并使用resultMap的id属性连接
<select>标签中的sql语句这里就不多做介绍,可以自行百度 sql的多表操作语法
3、设置<resultMap>的参数,设置属性 id 为 "ApplyExtendResultMap",设置属性 type为 "com.bs.mstp.mstp01.bean.extend.ApplyExtend",并在 <select> 中添加属性resultMap
4、编写详细的<resultMap>标签对 (在这里,我们只显示goods表的name字段,不涉及goods其他字段)
分两部分:首先是apply表,因为表中含有外键;然后是goods表,因为表中有被其他表作为外键的字段。
步骤:
首先,<id>标签的 column 和 property 参数,分别对应在sql查询中所使用的用于区分两个表的字段名 和 实际数据库中的字段;
其次,剩下所有字段都写在<result >标签中,编写方式和上面<id>的一样;
其中,需要注意一点,你需要什么字段,就把相应的字段信息写在标签中,但是 主键 id 和 外键 goods_id.一定要写清楚;
然后,在<association >标签对中,将表中字段被作为外键的 goods 表的信息进行设置。参数 property 和 javaType ,分别对应表的名字 和 对应表的实体类;
最后,<id> 和 <result>标签的编写方式和上面相同。
整体代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--apply:
private Long id;
private Long goods_id;
private String statu;
private String num;
private String applytime;
goods:
private Long id;
private String barcode;
private String name;
private String norm;
private String unit;
private String num;
private String boxnorm;
private String brithtime;-->
<mapper namespace="com.bs.mstp.mstp01.dao.extend.ApplyExtendMapper">
<select id="findAll"
resultMap="ApplyExtendResultMap">
select
a.id as a_id,
g.name as g_name,
a.statu as a_statu,
a.num as a_num,
a.applytime as a_applytime
from apply a, goods g
where a.goods_id = g.id
</select>
<resultMap id="ApplyExtendResultMap"
type="com.bs.mstp.mstp01.bean.extend.ApplyExtend">
<id column="a_id" property="id"/>
<result column="a_goods_id" property="goods_id"/>
<result column="a_statu" property="statu"/>
<result column="a_num" property="num"/>
<result column="a_applytime" property="applytime"/>
<association property="goods"
javaType="com.bs.mstp.mstp01.bean.Goods">
<id column="g_id" property="id"/>
<result column="g_name" property="name"/>
</association>
</resultMap>
</mapper>
4\IDepartmentService.java
1、添加方法 (注意命名可以和之前的 findAll()方法不同)
因为我们是在查询申请信息时,需要同时通过goods_id查看所申请的物品信息,在这里我们命名为findAllWithGoods
整体代码:
package com.bs.mstp.mstp01.service;
import com.bs.mstp.mstp01.bean.Apply;
import com.bs.mstp.mstp01.bean.extend.ApplyExtend;
import java.util.List;
public interface IApplyService {
List<Apply> findAll();
List<ApplyExtend> findAllWithGoods();
Apply findById(long id);
List<Apply> query(Apply apply);
void saveOrUpdate(Apply apply) throws Exception;
void deleteById(long id) throws Exception;
}
5\ApplyServiceImpl.java
1、注释 @Resource,注入applyExtendMapper
@Resource
private ApplyExtendMapper applyExtendMapper;
2、实现接口中的方法 findAllWithGoods
@Override
public List<ApplyExtend> findAllWithGoods() {
return applyExtendMapper.findAll();
}
6\ApplyController.java
1、编写 findAllWithGoods 相关代码
@ApiOperation("查询列表信息时,携带的有申请的物品名称信息-查询")
@GetMapping("findAllWithGoods")
public List<ApplyExtend> findAllWithGoods(){
return applyService.findAllWithGoods();
}
三\实现效果
成功!