mybatis系列七:多对一关联查询

来源:互联网 发布:手机网络证书过期 编辑:程序博客网 时间:2024/06/01 09:27

       有了前面的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等。这些查询是如何处理的呢,这一讲我们就利用学生表(Student)和系表(Department),多对一的关系,来讲这个问题。

       对应的sql脚本请查看前面的博文。

=================实体类StudentEntity.java===========================

package com.obtk.entitys;public class StudentEntity implements java.io.Serializable {private static final long serialVersionUID = 4897498920955479723L;private Integer stuId;private String stuName;private String gender;private Integer age;private String address;private Integer deptIdd;private DeptEntity dept;  //关联属性public StudentEntity() {}public StudentEntity(String stuName, String gender,int age, String address) {this.stuName = stuName;this.gender = gender;this.age = age;this.address = address;}public void setDept(DeptEntity dept) {this.dept = dept;}public DeptEntity getDept() {return dept;}public Integer getStuId() {return this.stuId;}public void setStuId(Integer stuId) {this.stuId = stuId;}public String getStuName() {return this.stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public String getGender() {return this.gender;}public void setGender(String gender) {this.gender = gender;}public void setAge(Integer age) {this.age = age;}public Integer getAge() {return age;}public String getAddress() {return this.address;}public void setAddress(String address) {this.address = address;}public void setDeptIdd(Integer deptIdd) {this.deptIdd = deptIdd;}public Integer getDeptIdd() {return deptIdd;}}
=================实体类DeptEntity.java===========================
package com.obtk.entitys;public class DeptEntity implements java.io.Serializable {private static final long serialVersionUID = -2856143193567492289L;private int deptId;private String departName;private String address;public DeptEntity() {}public void setDepartName(String departName) {this.departName = departName;}public String getDepartName() {return departName;}public int getDeptId() {return this.deptId;}public void setDeptId(int deptId) {this.deptId = deptId;}public String getAddress() {return this.address;}public void setAddress(String address) {this.address = address;}}


mapper文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "mybatis-3-mapper.dtd"><mapper namespace="stu">    <!--关联映射是用来对返回结果集的处理  --><resultMap id="stuJoinDept" type="StudentEntity"><id property="stuId" column="stuId"/><result property="stuName" column="stuName"/><result property="gender" column="gender"/><result property="age" column="age"/><result property="address" column="address"/><!-- 关联属性,需要在实体类里面配置 --><association property="dept" javaType="DeptEntity" ><id property="deptId" column="deptId"/><result property="departName" column="departName"/><result property="address" column="address"/></association></resultMap>    <!-- 如果是关联查询,要用resultMap ,并且resultMap和resultType是冲突关系--><select id="selectByJoinId" parameterType="int" resultMap="stuJoinDept">select * from student s inner join department don s.deptIdd=d.deptIdwhere s.stuId=#{stuId}</select></mapper>

这样配置之后,就可以了,将select 语句与resultMap 对应的映射结合起来看,就明白了。用association 来得到关联的作者,这是多对一的情况,因为多个学生对应同一个系。关于association元素的属性详细描述如下表1所示:

表1 association元素的属性

属性

描述

property

映射数据库列的字段或属性。如果 JavaBean 的属性与给定的名称匹配,就会使用匹配的名字。否则,MyBatis 将搜索给定名称的字段。两种情况下您都可以使用逗点的属性形式。比如,您可以映射到”username ”,也可以映射到更复杂点的”address.street.number ”。

column

数据库的列名或者列标签别名。与传递给 resultSet.getString(columnName)的参数名称相同。注意: 在处理组合键时, 您可以使用 column= “{prop1=col1,prop2=col2}”这样的语法,设置多个列名传入到嵌套查询语句。这就会把 prop1 和 prop2 设置到目标嵌套选择语句的参数对象中。

javaType

完整 java 类名或别名 (参考上面的内置别名列表)。如果映射到一个 JavaBean ,那 MyBatis 通常会自行检测到。然而,如果映射到一个 HashMap,那您应该明确指定 javaType 来确保所需行为。

jdbcType

支持的 JDBC 类型列表中列出的 JDBC 类型。这个属性只在 insert,update 或

delete 的时候针对允许空的列有用。JDBC 需要这项,但 MyBatis 不需要。如果您直接编写 JDBC 代码,在允许为空值的情况下需要指定这个类型。

typeHandler

我们已经在文档中讨论过默认类型处理器。使用这个属性可以重写默认类型处理器。它的值可以是一个 TypeHandler 实现的完整类名,也可以是一个类型别名。



案例1.   根据id查询

package com.obtk.test2;import org.apache.ibatis.session.SqlSession;import com.obtk.entitys.StudentEntity;import com.obtk.utils.MybatisUtil;public class TestJoin01 {public static void main(String[] args) {SqlSession session=null;try {//4.得到sessionsession=MybatisUtil.getSession();//5.执行语句StudentEntity stu=session.selectOne("stu.selectByJoinId",129);System.out.println(stu.getStuName()+","+stu.getGender()+","+stu.getDept().getQqqName());} catch (Exception e) {e.printStackTrace();}finally{MybatisUtil.closeSession();}}}
案例2   传递多个参数查询出多条记录

sql配置:

<!-- 关联查询跨多张表传递参数,用parameterType="map"  --><select id="selectByJoinMany" parameterType="map" resultMap="stuJoinDept">select * from student s inner join department don s.deptIdd=d.deptIdwhere s.gender=#{sex}and d.departName=#{deptName}</select>
java代码

package com.obtk.test2;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;import com.obtk.entitys.StudentEntity;import com.obtk.utils.MybatisUtil;public class TestJoin02 {public static void main(String[] args) {SqlSession session=null;try {//4.得到sessionsession=MybatisUtil.getSession();Map paramMap=new HashMap();paramMap.put("sex", "男");paramMap.put("deptName", "计算机系");//5.执行语句List<StudentEntity> stuList=session.selectList("stu.selectByJoinMany",paramMap);for(StudentEntity stu : stuList){System.out.println(stu.getStuName()+","+stu.getGender()+","+stu.getDept().getQqqName()+","+stu.getDept().getAddress());}} catch (Exception e) {e.printStackTrace();}finally{MybatisUtil.closeSession();}}}
如果数据库列名和实体类的属性名一致的话,我们还可以进行简化配置,如下:

<mapper namespace="dept">    <!-- 单张表的映射 --><resultMap id="deptMap" type="DeptEntity"><id property="deptId" column="deptId"/><result property="departName" column="departName"/><result property="address" column="address"/></resultMap></mapper>


<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "mybatis-3-mapper.dtd"><mapper namespace="stu">    <resultMap id="stuMap" type="StudentEntity" autoMapping="true">        <!-- id不能省 --><id property="stuId" column="stuId"/></resultMap>    <!--关联映射是用来对返回结果集的处理  -->    <!-- autoMapping="true":对于列名和实体类的属性名一致,那么我们可以省略除主键之外的映射关系 --><resultMap id="stuJoinDept" type="StudentEntity" autoMapping="true"><id property="stuId" column="stuId"/><!-- 关联属性,需要在实体类里面配置 --><association property="dept" javaType="DeptEntity"                 resultMap="dept.deptMap"></association></resultMap></mapper>











原创粉丝点击