mybatis处理结果记得问题 springMVC下,MyBatis实现复杂查询

https://blog.csdn.net/e_anjing/article/details/49908459

springMVC下,MyBatis实现复杂查询

springMVC和MyBatis基础环境配置这里不表。

一、情况如下:(实体类的其他参数此处不写)

  1. 用户表user: id,user_name,nick_name,...
  2. 用户类User: id,userName,nickName,...
  3. 课程表lession: id,less_name,speeker,...
  4. 课程类Lession: id,leeName,speeker,...
  5. 关系表user_lession: id,user_id,less_id
  6. 关系类UserLession: id,userId,lessId

二、需要实现的查询情况:

1、对课程表查询时,查询出选课人数;
2、对关系表查询时,查询出对应的user和lession,并封装成User和Lession。
三、实现过程

1、对课程表查询时,查询出选课人数

(1)首先在Lession类中添加int stuCount(选课学生总数)字段,并set/get;

(2)在lessionMapper.xml中配置resultMap(映射关系)

  1. <resultMap id="BaseResultMap" type="cn.xxx.domain.Lession" > <!--对象所在包名自取-->
  2. <id column="id" property="id" jdbcType="BIGINT" />
  3. <result column="less_name" property="lessName" jdbcType="VARCHAR" />
  4. <result column="speeker" property="speeker" jdbcType="VARCHAR" />
  5. <association property="stuCount" select="getStuCount" column="{lessId=id}" javaType="java.lang.Integer"/>
  6. </resultMap>
(3)利用association对stuCount添加查询方法getStuCount,并将参数lessId传送给该方法,该方法返回值是int类型。

  1. <select id="getStuCount" resultType="java.lang.Integer">
  2. select count(1) as stuCount from user_lession where less_id=#{lessId,jdbcType=BIGINT};
  3. </select>
通过getStuCount获取课程被选总数,又通过resultMap自动映射到Lession的属性stuCount上。

(4)以下是MyBatis中findById方法获取Lession的sql配置:

  1. <select id="findById" resultMap="WithConditionResult" parameterType="java.lang.Long" >
  2. select *
  3. from lession
  4. where id = #{id,jdbcType=BIGINT}
  5. </select>

小结:这种方法类似于一个sql语句:

  1. select l.*,( select count( 1) from user_lession ul where ul.less_id=?) as stuCount
  2. from lession l where l.id=?

具体使用哪种方法看个人喜好,本人比较习惯于用了框架就全用。

2、对关系表查询时,查询出对应的user和lession,并封装成User和Lession

(1)在UserLession类中,添加User user和Lession lession属性,并set/get

(2)配置UserLessionMapper.xml中的resultMap(映射关系)

  1. <resultMap id="BaseResultMap" type="cn.xxx.domain.UserLession" >
  2. <id column="id" property="id" jdbcType="BIGINT" />
  3. <result column="user_id" property="userId" jdbcType="VARCHAR" />
  4. <result column="less_id" property="lessId" jdbcType="VARCHAR" />
  5. <association property="user" resultMap="UserResultMap" javaType="cn.xxx.domain.User"/>
  6. <association property="lession" resultMap="LessionResultMap" javaType="cn.xxx.domain.Lession"/>
  7. </resultMap>
配置中,添加了user和lession的property,对这两个property,规定了它们的javaType和对应的resultMap。

(3)则需要再配置相应的UserResultMap和LessionResultMap:

  1. <resultMap id="UserResultMap" type="cn.xxx.domain.User" >
  2. <id column="id" property="id" jdbcType="VARCHAR" />
  3. <result column="user_name" property="userName" jdbcType="VARCHAR" />
  4. <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
  5. </resultMap>
  1. <resultMap id="LessionResultMap" type="cn.xxx.domain.Lession" >
  2. <id column="id" property="id" jdbcType="BIGINT" />
  3. <result column="less_name" property="lessName" jdbcType="VARCHAR" />
  4. <result column="speeker" property="speeker" jdbcType="VARCHAR" />
  5. </resultMap>
(4)做好这些准备后,我们来完成findById的实现:

  1. < select id= "findById" resultMap= "BaseResultMap" parameterType= "java.lang.Long" >
  2. select ul.*,u.*,l.*
  3. from user_lession ul
  4. left join user u on ul.user_id=u.id
  5. left join lession l on ul.less_id=l.id
  6. where ul.id = #{ id,jdbcType= BIGINT}
  7. </ select>

于是,findById就能讲u.*和l.*通过映射关系,封装成User和Lession,而通过BaseResultMap添加到UserLession中。

查询出来的结果结构如下:

  1. UserLession:
  2. {
  3. userId:xxx,
  4. lessId:xxx,
  5. user:{
  6. id:xxx,
  7. ....
  8. },
  9. lession:{
  10. id:xxx,
  11. ....
  12. }
  13. }
小结:这么做的好处是:既能查询user_lession对应的user和lession的信息,又能根据关系表的字段来实现模糊查询。

#引申:模糊查询findList(分页这里就不写了)

  1. <select id="findList" resultMap="BaseResultMap">
  2. select ul.*,u.*,l.*
  3. from user_lession ul
  4. left join user u on ul.user_id=u.id
  5. left join lession l on ul.less_id=l.id
  6. <include refid="baseCondition"/>
  7. order by ul.create_time DESC
  8. </select>
  1. <sql id="baseCondition">
  2. <where>
  3. 1=1
  4. <if test="userId != null" >
  5. and ul.user_id = #{userId,jdbcType=VARCHAR}
  6. </if>
  7. <if test="lessId != null" >
  8. and ul.less_id = #{lessId,jdbcType=VARCHAR}
  9. </if>
  10. <if test="keyword != null" >
  11. and CONCAT(
  12. IFNULL(u.user_name,''),IFNULL(u.nick_name,''),
  13. IFNULL(l.less_name,''),IFNULL(l.speeker,'')
  14. ) like CONCAT('%',#{keyword,jdbcType=VARCHAR},'%')
  15. </if>
  16. </where>
  17. </sql>
文章标签:  springM



springMVC和MyBatis基础环境配置这里不表。

一、情况如下:(实体类的其他参数此处不写)

  1. 用户表user: id,user_name,nick_name,...
  2. 用户类User: id,userName,nickName,...
  3. 课程表lession: id,less_name,speeker,...
  4. 课程类Lession: id,leeName,speeker,...
  5. 关系表user_lession: id,user_id,less_id
  6. 关系类UserLession: id,userId,lessId

二、需要实现的查询情况:

1、对课程表查询时,查询出选课人数;
2、对关系表查询时,查询出对应的user和lession,并封装成User和Lession。
三、实现过程

1、对课程表查询时,查询出选课人数

(1)首先在Lession类中添加int stuCount(选课学生总数)字段,并set/get;

(2)在lessionMapper.xml中配置resultMap(映射关系)

  1. <resultMap id="BaseResultMap" type="cn.xxx.domain.Lession" > <!--对象所在包名自取-->
  2. <id column="id" property="id" jdbcType="BIGINT" />
  3. <result column="less_name" property="lessName" jdbcType="VARCHAR" />
  4. <result column="speeker" property="speeker" jdbcType="VARCHAR" />
  5. <association property="stuCount" select="getStuCount" column="{lessId=id}" javaType="java.lang.Integer"/>
  6. </resultMap>
(3)利用association对stuCount添加查询方法getStuCount,并将参数lessId传送给该方法,该方法返回值是int类型。

  1. <select id="getStuCount" resultType="java.lang.Integer">
  2. select count(1) as stuCount from user_lession where less_id=#{lessId,jdbcType=BIGINT};
  3. </select>
通过getStuCount获取课程被选总数,又通过resultMap自动映射到Lession的属性stuCount上。

(4)以下是MyBatis中findById方法获取Lession的sql配置:

  1. <select id="findById" resultMap="WithConditionResult" parameterType="java.lang.Long" >
  2. select *
  3. from lession
  4. where id = #{id,jdbcType=BIGINT}
  5. </select>

小结:这种方法类似于一个sql语句:

  1. select l.*,( select count( 1) from user_lession ul where ul.less_id=?) as stuCount
  2. from lession l where l.id=?

具体使用哪种方法看个人喜好,本人比较习惯于用了框架就全用。

2、对关系表查询时,查询出对应的user和lession,并封装成User和Lession

(1)在UserLession类中,添加User user和Lession lession属性,并set/get

(2)配置UserLessionMapper.xml中的resultMap(映射关系)

  1. <resultMap id="BaseResultMap" type="cn.xxx.domain.UserLession" >
  2. <id column="id" property="id" jdbcType="BIGINT" />
  3. <result column="user_id" property="userId" jdbcType="VARCHAR" />
  4. <result column="less_id" property="lessId" jdbcType="VARCHAR" />
  5. <association property="user" resultMap="UserResultMap" javaType="cn.xxx.domain.User"/>
  6. <association property="lession" resultMap="LessionResultMap" javaType="cn.xxx.domain.Lession"/>
  7. </resultMap>
配置中,添加了user和lession的property,对这两个property,规定了它们的javaType和对应的resultMap。

(3)则需要再配置相应的UserResultMap和LessionResultMap:

  1. <resultMap id="UserResultMap" type="cn.xxx.domain.User" >
  2. <id column="id" property="id" jdbcType="VARCHAR" />
  3. <result column="user_name" property="userName" jdbcType="VARCHAR" />
  4. <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
  5. </resultMap>
  1. <resultMap id="LessionResultMap" type="cn.xxx.domain.Lession" >
  2. <id column="id" property="id" jdbcType="BIGINT" />
  3. <result column="less_name" property="lessName" jdbcType="VARCHAR" />
  4. <result column="speeker" property="speeker" jdbcType="VARCHAR" />
  5. </resultMap>
(4)做好这些准备后,我们来完成findById的实现:

  1. < select id= "findById" resultMap= "BaseResultMap" parameterType= "java.lang.Long" >
  2. select ul.*,u.*,l.*
  3. from user_lession ul
  4. left join user u on ul.user_id=u.id
  5. left join lession l on ul.less_id=l.id
  6. where ul.id = #{ id,jdbcType= BIGINT}
  7. </ select>

于是,findById就能讲u.*和l.*通过映射关系,封装成User和Lession,而通过BaseResultMap添加到UserLession中。

查询出来的结果结构如下:

  1. UserLession:
  2. {
  3. userId:xxx,
  4. lessId:xxx,
  5. user:{
  6. id:xxx,
  7. ....
  8. },
  9. lession:{
  10. id:xxx,
  11. ....
  12. }
  13. }
小结:这么做的好处是:既能查询user_lession对应的user和lession的信息,又能根据关系表的字段来实现模糊查询。

#引申:模糊查询findList(分页这里就不写了)

  1. <select id="findList" resultMap="BaseResultMap">
  2. select ul.*,u.*,l.*
  3. from user_lession ul
  4. left join user u on ul.user_id=u.id
  5. left join lession l on ul.less_id=l.id
  6. <include refid="baseCondition"/>
  7. order by ul.create_time DESC
  8. </select>
  1. <sql id="baseCondition">
  2. <where>
  3. 1=1
  4. <if test="userId != null" >
  5. and ul.user_id = #{userId,jdbcType=VARCHAR}
  6. </if>
  7. <if test="lessId != null" >
  8. and ul.less_id = #{lessId,jdbcType=VARCHAR}
  9. </if>
  10. <if test="keyword != null" >
  11. and CONCAT(
  12. IFNULL(u.user_name,''),IFNULL(u.nick_name,''),
  13. IFNULL(l.less_name,''),IFNULL(l.speeker,'')
  14. ) like CONCAT('%',#{keyword,jdbcType=VARCHAR},'%')
  15. </if>
  16. </where>
  17. </sql>

猜你喜欢

转载自blog.csdn.net/qq_36969486/article/details/80906851
今日推荐