在碰到多表联查
方法一
不用xml的查询,直接在mapper.java里边的方法通过@Select注解的形式
@Select("select b.id, b.bus_num, b.name, b.create_time, b.end_time, b.staff_id,b.linkman, b.phone, b.status, b.update_time, " +
"b.is_speak, b.is_video, b.team_num, b.day_num, b.channel_num, b.product_id, b.menu, b.mobile_type, b.industry_id, " +
"b.server, p.name as product_name , s.name as server_name from intercom_business b " +
"left join intercom_product p on p.id = b.product_id " +
"left join intercom_server s on s.id = b.server")
public List<IntercomBusinessVo> selectAllBusinessList(IntercomBusiness intercomBusiness);之后Vo类继承,只需要添加productName跟serverName这2个字段即可。然后就可以查询了。
但是这里只能查全部,不能where,如果需要where的话,特别是那种动态的where就很复杂。
方法二
沿用上边的Vo子类。
在xml中,重新新建一个resultMap,以及一个新的查询id
这个是原来的
<resultMap type="IntercomBusiness" id="IntercomBusinessResult">
<result property="id" column="id" />
<result property="busNum" column="bus_num" />
<result property="name" column="name" />
<result property="createTime" column="create_time" jdbcType="BIGINT" typeHandler="com.duijiang.typehandler.Long2DateHandler" />
<result property="endTime" column="end_time" />
<result property="staffId" column="staff_id" />
<result property="linkman" column="linkman" />
<result property="phone" column="phone" />
<result property="status" column="status" />
<result property="updateTime" column="update_time" jdbcType="BIGINT" typeHandler="com.duijiang.typehandler.Long2DateHandler" />
<result property="isSpeak" column="is_speak" />
<result property="isVideo" column="is_video" />
<result property="teamNum" column="team_num" />
<result property="dayNum" column="day_num" />
<result property="channelNum" column="channel_num" />
<result property="productId" column="product_id" />
<result property="menu" column="menu" />
<result property="mobileType" column="mobile_type" />
<result property="industryId" column="industry_id" />
<result property="server" column="server" />
</resultMap>
这个是新的
<resultMap type="com.duijiang.domain.vo.IntercomBusinessVo" id="IntercomBusinessVoResult">
<result property="id" column="id" />
<result property="busNum" column="bus_num" />
<result property="name" column="name" />
<result property="createTime" column="create_time" jdbcType="CHAR" typeHandler="com.duijiang.typehandler.String2DateHandler" />
<result property="endTime" column="end_time" jdbcType="CHAR" typeHandler="com.duijiang.typehandler.String2DateHandler" />
<result property="staffId" column="staff_id" />
<result property="linkman" column="linkman" />
<result property="phone" column="phone" />
<result property="status" column="status" />
<result property="updateTime" column="update_time" jdbcType="INTEGER" typeHandler="com.duijiang.typehandler.Long2DateHandler" />
<result property="isSpeak" column="is_speak" />
<result property="isVideo" column="is_video" />
<result property="teamNum" column="team_num" />
<result property="dayNum" column="day_num" />
<result property="channelNum" column="channel_num" />
<result property="productId" column="product_id" />
<result property="menu" column="menu" />
<result property="mobileType" column="mobile_type" />
<result property="industryId" column="industry_id" />
<result property="server" column="server" />
<result property="productName" column="product_Name" />
<result property="serverName" column="server_name" />
</resultMap>
这个就是新的查询方法
<select id="selectAllBusinessList" parameterType="IntercomBusiness" resultMap="IntercomBusinessVoResult">
select b.id, b.bus_num, b.name, b.create_time, b.end_time, b.staff_id,b.linkman, b.phone, b.status, b.update_time, b.is_speak, b.is_video, b.team_num, b.day_num, b.channel_num, b.product_id, b.menu, b.mobile_type, b.industry_id, b.server, p.name as product_name, s.name as server_name
from intercom_business b left join intercom_product p on p.id = b.product_id left join intercom_server s on s.id = b.server
<where>
<if test="busNum != null and busNum != ''"> and bus_num like concat('%', #{busNum}, '%')</if>
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
<if test="params.beginEndTime != null and params.beginEndTime != '' and params.endEndTime != null and params.endEndTime != ''"> and end_time between #{params.beginEndTime} and #{params.endEndTime}</if>
<if test="staffId != null "> and staff_id = #{staffId}</if>
<if test="linkman != null and linkman != ''"> and linkman like concat('%', #{linkman}, '%')</if>
<if test="phone != null and phone != ''"> and phone like concat('%', #{phone}, '%')</if>
<if test="status != null "> and status = #{status}</if>
<if test="isSpeak != null "> and is_speak = #{isSpeak}</if>
<if test="isVideo != null "> and is_video = #{isVideo}</if>
<if test="teamNum != null "> and team_num = #{teamNum}</if>
<if test="dayNum != null "> and day_num = #{dayNum}</if>
<if test="channelNum != null "> and channel_num = #{channelNum}</if>
<if test="productId != null "> and product_id = #{productId}</if>
<if test="menu != null and menu != ''"> and menu = #{menu}</if>
<if test="mobileType != null "> and mobile_type = #{mobileType}</if>
<if test="industryId != null "> and industry_id = #{industryId}</if>
<if test="server != null "> and server = #{server}</if>
</where>
</select>当出现单条查询时,其实也可以用all的方法。只需要传入对于的where就行了