多表联查别忘了映射
这里还用了where标签
sql语句写的好,可以大幅度减轻代码复杂度
就好比这里,title和columntitle为空时,为查询全部,一但赋值就可以进行根据title值的模糊查询及根据columntitle的值的精确查询,非常的奈斯。
<!-- 建立JavaBean和数据库表的映射用于关系多表联查 -->
<resultMap id="aaa" type="Paper">
<result column="id" property="id"></result>
<result column="cid" property="cid"></result>
<result column="papertitle" property="papertitle"></result>
<result column="paperdescribe" property="paperdescribe"></result>
<result column="paperlogo" property="paperlogo"></result>
<result column="publishtime" property="publishtime"></result>
<association property="column" javaType="Column">
<result column="id" property="id"></result>
<result column="columntitle" property="columntitle"></result>
</association>
</resultMap>
<select id="findAll" resultMap="aaa">
SELECT
DISTINCT p.*, c.*
FROM
t_paper AS p LEFT JOIN t_column AS c
ON
p.cid=c.id
<where>
<if test="papertitle !='null' and papertitle != ''">
AND title like '%${title}%'
</if>
<if test="columntitle !='null' and columntitle != ''">
AND columntitle=#{columntitle}
</if>
</where>
ORDER BY p.publishtime DESC
LIMIT #{pageIndex}, #{pageSize}
</select>