描述:要在jsp页面显示多张表的不同字段,如果弄一个vo来综合两个表是可以,jsp可以显示,但是要将整个vo传到后台并且带上数据,后台似乎接受不到vo的值,可能是因为在跳转到此页面之前忘了添加model吧,model.addAttribute("zmisJybTeacher", new ZmisJybTeacher());写这篇文章的时候我也没来得及测试是不是因为这个原因导致后台接受不到vo实体,不过也无所谓啦,这篇文章主要是教同学们利用来查询并返回多表的结果集。
1.首先新建一个实体,包含你要查询的n个表的所有字段
2.新建一个Dao
其中@MyBatisDao注解代码如下,代码难得,君应该珍惜
/**
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
*/
package com.bookestore.dao;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
/**
* 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。
* @author thinkgem
* @version 2013-8-28
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface MyBatisDao {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
把自定义的查询的xml贴出来,下面细说
<?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.thinkgem.jeesite.zmis.jyb.dao.ZmisJybTeacherDao">
<sql id="zmisJybTeacherColumns">
j.xh AS xh,
j.com_name AS comName,
j.com_address AS comAddress ,
j.com_yb AS comYb,
j.gddh AS gddh,
j.frdb AS frdb,
j.moblie AS moblie ,
j.xc AS xc,
j.qysj AS qysj,
j.gslx AS gslx,
j.htfj AS htfj,
j.create_by AS "createBy.id",
j.create_date AS "createDate",
j.update_by AS "updateBy.id",
j.update_date AS "updateDate",
j.remarks AS "remarks",
j.del_flag AS "delFlag",
s.zy AS zy,
s.bj AS bj,
s.`name` AS name,
s.sex AS sex,
s.phone AS phone,
s.qq AS qq,
s.email AS email,
s.sfz AS sfz,
s.address AS address,
s.dgsxcj AS dgsxcj
</sql>
<sql id="zmisJybTeacherJoins">
</sql>
<select id="findList" resultType="ZmisJybTeacher">
SELECT
<include refid="zmisJybTeacherColumns"/>
FROM zmis_jyb j JOIN zmis_student s ON j.xh = s.xh
<include refid="zmisJybTeacherJoins"/>
<where>
j.del_flag = #{DEL_FLAG_NORMAL}
<if test="xh != null and xh != ''">
AND j.xh LIKE
<if test="dbName == 'oracle'">'%'||#{xh}||'%'</if>
<if test="dbName == 'mssql'">'%'+#{xh}+'%'</if>
<if test="dbName == 'mysql'">concat('%',#{xh},'%')</if>
</if>
<if test="name != null and name != ''">
AND s.name LIKE
<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
</if>
<if test="comName != null and comName != ''">
AND j.com_name LIKE
<if test="dbName == 'oracle'">'%'||#{comName}||'%'</if>
<if test="dbName == 'mssql'">'%'+#{comName}+'%'</if>
<if test="dbName == 'mysql'">concat('%',#{comName},'%')</if>
</if>
</where>
<choose>
<when test="page !=null and page.orderBy != null and page.orderBy != ''">
ORDER BY ${page.orderBy}
</when>
<otherwise>
ORDER BY j.update_date DESC
</otherwise>
</choose>
</select>
</mapper>
到这里,如果id以及方法名,实体类名写对了的话,并且连接上了数据库,那么直接调用dao的那个方法就可以获取到上面sql的结果集了,当然,到这里,看sql就知道这是查询两张表所有关联的结果,如果是条件查询的话,不妨再往下看
后面是UI分页的配置
到这里已经讲完了,谢谢大家,哈哈哈