概述
查询返回对象里面包含一个对象
<?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">
<mapper namespace="com.bdm.mappers.EmpMapper">
<resultMap id="EmpResultMap" type="com.bdm.entities.Emp">
<id property="id" column="id" />
<result property="name" column="name" />
<association property="dept" column="dept_id"
select="com.bdm.mappers.DeptMapper.getById" />
</resultMap>
<select id="getById" parameterType="Integer" resultMap="EmpResultMap">
select *
from tbl_emp where id=#{id}
</select>
</mapper>
<?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">
<mapper namespace="com.bdm.mappers.DeptMapper">
<resultMap id="DeptResultMap" type="com.bdm.entities.Dept">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>
<select id="getById" parameterType="Integer" resultMap="DeptResultMap">
select id from tbl_dept where id = #{id}
</select>
</mapper>
select="dao路径加方法接口名称"《》-----》对象接口路径下面的方法
column《------------------------》传参数的列名
如果多参数传参---------------------》{key=value,key2=value2}
parameterType:传参类型
查询返回对象里面包含一个对象集合(list)
<?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">
<mapper namespace="com.bdm.mappers.DeptMapper">
<resultMap id="DeptResultMap" type="com.bdm.entities.Dept">
<id property="id" column="id" />
<result property="deptName" column="dept_name" />
<collection property="emps" column="id" ofType="com.bdm.entities.Emp"
select="com.bdm.mappers.EmpMapper.getEmpByDeptId" />
</resultMap>
<select id="getById" parameterType="Integer" resultMap="DeptResultMap">
select * from tbl_dept where id = #{id}
</select>
</mapper>
property:对象里面的集合名称 ofType:实体对应路径
select: dao层路径加具体接口方法名称
column:传参
<?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">
<mapper namespace="com.bdm.mappers.EmpMapper">
<resultMap id="EmpResultMap" type="com.bdm.entities.Emp">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>
<select id="getEmpByDeptId" parameterType="Integer" resultMap="EmpResultMap">
select * from tbl_emp where dept_Id = #{deptId}
</select>
</mapper>
注意懒加载:使用是才会出发具体的SQL字段或者SQL语句
按照实际情况自行配置
mybatis.configuration.lazy-loading-enabled=true
#false 为按需加载
mybatis.configuration.aggressive-lazy-loading=false
最后
以上就是紧张酒窝为你收集整理的mybatis级联查询的全部内容,希望文章能够帮你解决mybatis级联查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复