1.用mybatis进行三表联立查询:
指定一个mapper.xml进行修改,(比如StudentMapper.xml)
声明resultMap,并将type指定相关的类,取id名
<collection property="gradeList" ofType="com.lxz.demo.entity.Grade">
<id column="gid" jdbcType="INTEGER" property="id" />
<result column="gname" jdbcType="VARCHAR" property="name" />
<collection property="team" javaType="com.lxz.demo.entity.Team" >
<id column="tid" jdbcType="INTEGER" property="id" />
<result column="tname" jdbcType="INTEGER" property="name" />
<result column="teacher" jdbcType="VARCHAR" property="teacher" />
</collection>
</collection>
</resultMap>
<select id="selectAll" resultMap="MyMap">
select s.id sid,s.name,s.age,t.name tname,g.name gname,t.teacher,g.id gid,t.id tid from param_student s left join param_team t on s.team=t.id left join param_grade g on s.grade=g.id ;
</select>
一定要在相关的entity声明相关类(实质是嵌套)
名字尽量不要有重复 如果有就在sql下取别名,再在column用别名对应
property一定是和相关类属性一一对应才行
2.常用xml方式的基本模板
<resultMap id="IpoMaxPurchaseRateRepVO" type="com.win.dfbp.newstock.vo.response.IpoMaxPurchaseRateRepVO" >
<result column="id" property="id" />
<result column="prod_code" property="prodCode" />
<result column="prod_name" property="prodName" />
<result column="remark" property="remark" />
<result column="max_ratio" property="maxRatio" />
</resultMap>
<select id="list" resultMap="IpoMaxPurchaseRateRepVO" >
select id,prod_code,prod_name,remark,update_time from ipo_new_stock_max_ratio
<where>
delete_flag=0
<if test="prodCode != null">
and prod_code like concat('%',#{prodCode},'%')
</if>
</where>
ORDER BY update_time DESC
</select>
<insert id="save">
INSERT INTO ipo_new_stock_max_ratio
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null ">
id,
</if>
<if test="prodCode != null and prodCode.length() > 0">
prod_code,
</if>
<if test="prodName != null and prodName.length() > 0">
prod_name,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="createUserId != null">
create_user_id,
</if>
<if test="updateTime != null">
update_time,
</if>
<if test="updateUserId != null">
update_user_id,
</if>
<if test="maxRatio != null">
max_ratio,
</if>
remark,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id},
</if>
<if test="prodCode != null and prodCode.length() > 0">
#{prodCode},
</if>
<if test="prodName != null and prodName.length() > 0">
#{prodName},
</if>
<if test="createTime != null">
#{createTime},
</if>
<if test="createUserId != null">
#{createUserId},
</if>
<if test="updateTime != null">
#{updateTime},
</if>
<if test="updateUserId != null">
#{updateUserId},
</if>
<if test="maxRatio != null ">
#{maxRatio},
</if>
remark,
</trim>
</insert>
<update id="update" parameterType="com.win.dfbp.newstock.entity.IpoMaxPurchaseRate">
UPDATE ipo_new_stock_max_ratio
<set>
<if test ='null != prodCode'>prod_code = #{prodCode},</if>
<if test ='null != prodName'>prod_name = #{prodName},</if>
<if test ='null != maxRatio'>max_ratio = #{maxRatio},</if>
<if test ='null != remark'>remark = #{remark},</if>
<if test ='null != updateUserId'>update_user_id = #{updateUserId},</if>
<if test ='null != updateTime'>update_time = #{updateTime}</if>
</set>
WHERE prod_code = #{prodCode} AND delete_flag = 0
</update>
id对应mapper的方法名,parameterType对应返回参数实体类
<resultMap id="BaseResultMap" type="com.win.dfbp.newstock.entity.IpoMaxPurchaseRate" >
<result column="id" property="id" />
<result column="prod_code" property="prodCode" />
<result column="prod_name" property="prodName" />
<result column="remark" property="remark" />
<result column="max_ratio" property="maxRatio" />
<result column="delete_flag" property="deleteFlag" />
<result column="create_user_id" property="createUserId" />
<result column="create_time" property="createTime" />
<result column="update_user_id" property="updateUserId" />
<result column="update_time" property="updateTime" />
</resultMap>
```
在写对应的属性名时,注意column与property的区分