Mybatis中的高级查询主要通过关联查询,集合查询或鉴别器来完成。其核心就是之前提到的通过resultMap标记来完成
1. 关联查询
关联查询一般有三种方式:
a.联合查询
利用resultMap的map.xml中的配置如下(Pojo1和Pojo2对应的数据表分别为table1和table2)
<resultMap id="Pojo1Map" type="Pojo1">
<id property="id" column="Pojo1.id" />
<result property="realName" column="realName" />
<result property="IDCard" column="IDCard" />
<association property="Pojo2" column="userID"
javaType="Pojo2">
<id property="id" column="Pojo2.id" />
<result property="userName" column="userName" />
<result property="password" column="password" />
</association>
</resultMap>
<select id="selectPojo1Join" resultMap="Pojo1Map">
select * from table1 inner join table2
on table2.id=table1.userID
</select>
联合查询主要是通过association标记来实现两张表的连接
b.构造查询
构造查询首先需要构造需要使用的参数对应的构造函数,比如需要查询userName和password两个关键字数据,这时,需要在POJO类中建立新的构造函数:
public User(String userName,String password){
super();
this.userName=userName;
this.password=password;
}
这里需要注意的事,Mybatis框架通过默认的构造函数找到相应的参数,所以,这里还需要补上默认的构造函数:
public User(String id,String userName,String password){
super();
}
构造函数查询的resultMap与之前联合查询有少许不同,需要 使用< constructor>标签,如下:
<association property="User" column="userID"
javaType="User">
<constructor>
<arg column="userName" javaType="String" />
<arg column="password" javaType="String" />
</constructor>
</association>
其余地方与联合查询相同,使用构造函数进行查询的好处是,只需要获取对应的关键字数据段,减少了查询的数据量。
c.子查询
子查询的形式为:
select * from table1 where userID in(select id from table2)
子查询的步骤大致分为两步:第一步建立一个子查询
<select id="selectOne" parameterType="int" resultType="Pojo2">
select * from table2 where id=#{id}
</select>
建立子查询也就是通过一张表的某个查询条件找到相应的数据
第二步为建立查询
<resultMap id="Pojo1SubMap" type="Pojo1">
……
<association property="Pojo2" column="userID"
javaType="Pojo2" select="selectOne">
</association>
</resultMap>
<select id="selectPojo1Sub" resultMap="Pojo1SubMap">
select * from table1
</select>
需要将两张表进行联合,这里需要在< association>标记中加上select标记,赋值为子查询的id,selectOne,这样便可将两个查询进行关联起来。
使用子查询为N+1次查询,占用的资源可大可小,而联合查询为一次查询,占用的资源比较大,在Mybatis框架中,支持懒加载机制,也就是说,只有当函数执行时才会进行相应的sql语句,利用子查询刚好可以将查询分开,这样有利于提高程序的执行效率。Mybatis默认不支持懒加载机制,需在配置文件中进行如下设置:
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
这样便启动了懒加载应用,非常方便实用。
2.集合查询
集合查询主要是通过< collection>标记实现的,函数返回的是一个集合
这里首先需要在POJO类中定义一个新的函数:
private List<Pojo3> pojo3List;
<resultMap id="pojo3Map" type="Pojo2">
<collection property="pojo3List" javaType="ArrayList"
column="Pojo3ID" ofType="Pojo3">
<result property="Pojo3ID" column="Pojo3ID" />
...
</collection>
</resultMap>
resultMap中的类型是关联的数据类型,可以从原始sql语句看出:
select * from table2 inner join visit on table2.id=table3.userID
其中,table2和table3分别对应于Pojo2,Pojo3的数据表。、
property标记与之前一样,为定义的函数名,就javaType返回的是一个ArrayList类型的对象,column关联的数据库关键字,ofType为对应的数据类型,之后便可建立查询:
<select id="selectPojo3" resultMap="pojo3Map">
select * from table2 inner join visit on table2.id=table3.userID
</select>
这样便可完成一次集合查询操作,返回一个list
3.鉴别器
鉴别器主要是通过< discriminator>标记完成,举个简单的例子,比如有如下表:
<discriminator javaType="int" column="gender">
<case value="1" >
<result property="boy" column=“Boy" />
</case>
<case value="2" >
<result property="girl" column="Girl" />
</case>
</discrimindator>
这就是一个简单的鉴别器,性别为1和2,当取到1时,就是Boy列,取到2时就是Girl列,鉴别器的作用就是有时候查询的结构会返回不同类型的集合,可以将其进行分别映射。比如上例中可能数据表中具有男孩和女孩特有的字段,这时便需要将查询的结果进行分别映射。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)