关本文是Mybatis基础系列的第四篇文章,点击下面链接可以查看前面的文章:
mybatis基础系列(二)——基础语法、别名、输入映射、输出映射
在进行表设计时,往往需要在具体的业务基础上分析表与表之间的业务关系,再分析数据库级别表与表之间的业务关系。
员工表和部门表之间的关系分析:一个员工只能属于一个部门。
需求:查询出员工编号为7369的员工信息和部门信息。
sql语句:
select * from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_emp.empno=7369
看看mybatis中的写法
mapper.xml
<select id="queryEmpDeptInfo" parameterType="com.itpsc.request.EmpRequest" resultType="com.itpsc.vo.EmpVo"> select * from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_emp.empno=#{emp.empno} </select>
扩展Emp类增加部门熟悉
public class EmpVo extends Emp{ //增加dept表的属性 private Integer deptno; private String dname; private String loc; //.... }
运行结果
==> Preparing: select * from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_emp.empno=? ==> Parameters: 7369(Integer) <== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno, deptno, dname, loc <== Row: 7369, SMITH, CLERK, 7902, 1980-12-17, 800.00, null, 20, 20, RESEARCH, DALLAS <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@266e9dda] Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 00:00:00 CST 1980, sal=800.0, comm=null, deptno=null}
从上面可以看出,如果输出映射的实体类中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
除了用EmpVo类继承Emp类并且增加部门属性外,也可以在Emp类中增加Dept类型的对象来完成映射。mapper.xml中association 标签来映射关联对象。
public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Float sal; private Float comm; private Integer deptno; private Dept dept; //.... }
mybatis的写法
<select id="queryEmpDeptInfo" parameterType="com.itpsc.request.EmpRequest" resultMap="empMap"> select * from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_emp.empno=#{emp.empno} </select> <resultMap id="empMap" type="com.itpsc.entity.Emp" > <result column="empno" property="empno" jdbcType="INTEGER" /> <result column="ename" property="ename" jdbcType="VARCHAR" /> <result column="job" property="job" jdbcType="VARCHAR" /> <result column="mgr" property="mgr" jdbcType="INTEGER" /> <result column="hiredate" property="hiredate" jdbcType="DATE" /> <result column="sal" property="sal" jdbcType="REAL" /> <result column="comm" property="comm" jdbcType="REAL" /> <result column="deptno" property="deptno" jdbcType="INTEGER" /> <association property="dept" javaType="com.itpsc.entity.Dept"> <result column="deptno" property="deptno" jdbcType="INTEGER" /> <result column="dname" property="dname" jdbcType="VARCHAR" /> <result column="loc" property="loc" jdbcType="VARCHAR" /> </association> </resultMap>
运行结果
==> Preparing: select * from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_emp.empno=? ==> Parameters: 7369(Integer) <== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno, deptno, dname, loc <== Row: 7369, SMITH, CLERK, 7902, 1980-12-17, 800.00, null, 20, 20, RESEARCH, DALLAS <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a87ba34] Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 00:00:00 CST 1980, sal=800.0, comm=null, deptno=20, dept=Dept{deptno=20, dname='RESEARCH', loc='DALLAS'}}
部门表和员工表之间的关系分析:一个部门可以有多个员工。
需求:查询部门编号为20的所有员工的信息。
sql语句语法:
select t_emp.* from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_dept.deptno=20;
看看mybatis中的写法:
<select id="queryEmpInfoByDeptno" parameterType="com.itpsc.request.EmpRequest" resultType="com.itpsc.entity.Emp"> select t_emp.* from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_dept.deptno=#{emp.deptno}; </select>
运行结果
==> Preparing: select t_emp.* from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_dept.deptno=?; ==> Parameters: 20(Integer) <== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno <== Row: 7369, SMITH, CLERK, 7902, 1980-12-17, 800.00, null, 20 <== Row: 7566, JONES, MANAGER, 7839, 1981-04-02, 2975.00, null, 20 <== Row: 7788, SCOTT, ANALYST, 7566, 1987-04-19, 3000.00, null, 20 <== Row: 7876, ADAMS, CLERK, 7788, 1987-05-23, 1100.00, null, 20 <== Row: 7902, FORD, ANALYST, 7566, 1981-12-03, 3000.00, null, 20 <== Row: 7100, itpsc, mannager, 7902, 1980-01-10, null, 1000.00, 20 <== Row: 7101, itpsc2, developer, 7902, 1980-01-10, 2000.00, 1000.00, 20 <== Total: 7
部门表和员工表之间的关系分析:一个部门可以有多个员工。
员工表和技能表之间的关系分析:一个员工可以有多个技能。
需求:查询部门编号为20的所有员工的信息,并查询这些员工拥有的技能。
Sql 语句:
select t1.*,t_skill.* from ( select t_dept.*, t_emp.empno, t_emp.ename, t_emp.job, t_emp.mgr, t_emp.hiredate, t_emp.sal from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_dept.deptno=20) t1 join t_skill on (t1.empno=t_skill.empno);
DeptMapper.xml
<mapper namespace="com.itpsc.mapper.DeptMapper" > <#gth#查询部门信息--> <resultMap id="deptMap" type="com.itpsc.entity.Dept" > <result column="deptno" property="deptno" jdbcType="INTEGER" /> <result column="dname" property="dname" jdbcType="VARCHAR" /> <result column="loc" property="loc" jdbcType="VARCHAR" /> <#gth#查询部门下的员工信息,一个部门有多个员工,用collection--> <collection property="empList" ofType="com.itpsc.entity.Emp"> <result column="empno" property="empno" jdbcType="INTEGER" /> <result column="ename" property="ename" jdbcType="VARCHAR" /> <result column="job" property="job" jdbcType="VARCHAR" /> <result column="mgr" property="mgr" jdbcType="INTEGER" /> <result column="hiredate" property="hiredate" jdbcType="DATE" /> <result column="sal" property="sal" jdbcType="REAL" /> <result column="comm" property="comm" jdbcType="REAL" /> <collection property="skillList" ofType="com.itpsc.entity.Skill"> <result column="skillid" property="skillid" jdbcType="INTEGER" /> <result column="empno" property="empno" jdbcType="INTEGER" /> <result column="sname" property="sname" jdbcType="VARCHAR" /> <result column="descs" property="descs" jdbcType="VARCHAR" /> </collection> </collection> </resultMap> <select id="queryByDeptno" parameterType="com.itpsc.request.EmpRequest" resultMap="deptMap"> select t1.*,t_skill.* from ( select t_dept.*, t_emp.empno, t_emp.ename, t_emp.job, t_emp.mgr, t_emp.hiredate, t_emp.sal from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_dept.deptno=#{emp.deptno}) t1 join t_skill on (t1.empno=t_skill.empno); </select> </mapper>
运行结果
==> Preparing: select t1.*,t_skill.* from ( select t_dept.*, t_emp.empno, t_emp.ename, t_emp.job, t_emp.mgr, t_emp.hiredate, t_emp.sal from t_emp JOIN t_dept on(t_emp.deptno=t_dept.deptno) where t_dept.deptno=?) t1 join t_skill on (t1.empno=t_skill.empno); ==> Parameters: 20(Integer) ... <== Total: 6 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1129829c] Dept{deptno=20, dname='RESEARCH', loc='DALLAS', empList=[Emp{empno=7100, ename='itpsc', job='mannager', mgr=7902, hiredate=Thu Jan 10 00:00:00 CST 1980, sal=null, comm=null, deptno=null, skillList=[Skill{skillid=1001, empno=7100, sname='mybatis', descs='database crud'}, Skill{skillid=1002, empno=7100, sname='mysql', descs='data store'}, Skill{skillid=1003, empno=7100, sname='java', descs='program language'}]}, Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 00:00:00 CST 1980, sal=800.0, comm=null, deptno=null, skillList=[Skill{skillid=1004, empno=7369, sname='spring', descs='spring'}, Skill{skillid=1005, empno=7369, sname='spring mvc', descs='spring mvc'}, Skill{skillid=1006, empno=7369, sname='thymealf', descs='thymealf'}]}]}
resultType:将查询结果按照表列名与java对象属性名一一对应映射到java对象中。
resultMap:自定义表列名与java对象属性名之间的映射关系。结合association和collection完成一对一和一对多高级映射。association标签的作用是将关联的信息映射到一个对象中,实现一对一的映射。而collection标签将关联信息映射到一个list集合中,实现一对多的映射。
上面我们已经知道使用association、collection可以实现一对一及一对多映射,association、collection还有另外一个延迟加载的功能。
延迟加载(lazy load)是关联对象默认的加载方式,延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作。
mybatis默认没有开启延迟加载功能,需要在springboot的yml配置文件中启动延迟加载功能:
mybatis-plus:
mapper-locations: "classpath:com/itpsc/mapper/**/*.xml"
type-aliases-package: "com.itpsc.entity"
global-config:
db-column-underline: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: true #配置的缓存的全局开关
lazyLoadingEnabled: true #延时加载的开关
log-
参与评论
手机查看
返回顶部