<!-- 根据id查询:单值参数:::当一个参数时 占位符的名字可以随意 --><selectid="getBySid01"parameterType="int"resultType="Student">
select * from student where sid=#{s}
</select><!-- 根据id查询:parameterType参数类型是可以省略的 --><selectid="getBySid02"resultType="Student">
select * from student where sid=#{s}
</select>
//<!-- 解决方案1:把参数定义为对象::占位符的名字就是对象的属性名 -->//<select id="getByCondition01" parameterType="Student" resultType="Student">publicList<Student>getByCondition01(Student stu);//<!-- 解决方案2:把参数定义为Map::占位符的名字就是键的名字 -->//<select id="getByCondition02" parameterType="java.util.Map" resultType="Student">publicList<Student>getByCondition02(Map<String,Object> map);//<!-- 解决方案3:多个参数:占位符用索引表示::从0开始 -->// <select id="getByCondition03" resultType="Student">// select * from student where sex=#{0} and sdy=#{1} and score > #{2} // </select>publicList<Student>getByCondition03(String sex,boolean sdy,float score);// <!-- 解决方案4:使用注解@Param::属性与占位符名一致即可 -->// <select id="getByCondition04" resultType="Student">// select * from student where sex=#{se} and sdy=#{sd} and score > #{sc} // </select>publicList<Student>getByCondition04(@Param("se")String sex,@Param("sd")boolean sdy,@Param("sc")float score);
<!-- 根据性别 最低分 是否党员作为条件进行查询 --><!-- 解决方案1:把参数定义为对象::占位符的名字就是对象的属性名 --><selectid="getByCondition01"parameterType="Student"resultType="Student">
select * from student where sex=#{sex} and sdy=#{sdy} and score > #{score}
</select><!-- 解决方案2:把参数定义为Map::占位符的名字就是键的名字 --><selectid="getByCondition02"parameterType="java.util.Map"resultType="Student">
select * from student where sex=#{se} and sdy=#{dy} and score > #{sc}
</select><!-- 解决方案3:多个参数:占位符用索引表示::从0开始 --><selectid="getByCondition03"resultType="Student">
select * from student where sex=#{0} and sdy=#{1} and score > #{2}
</select><!-- 解决方案4:使用注解@Param::属性与占位符名一致即可 --><selectid="getByCondition04"resultType="Student">
select * from student where sex=#{se} and sdy=#{sd} and score > #{sc}
</select>
<!-- 当不处理时:查询丈夫 其wife属性是肯定是null --><selectid="getAll1"resultType="Husband">
select * from husband
</select><!-- 1对1解决方案:和n对1的解决方案完全相同 --><!-- 1对1解决方案:子查询 --><resultMaptype="Husband"id="husbandMap2"><idcolumn="hid"property="hid"/><resultcolumn="hname"property="hname"/><resultcolumn="hsalary"property="hsalary"/><!-- 根据hid查询wife表 获取一个Wife对象赋值给当前对象的wife属性 --><!-- 属性property:指定要给当前对象的wife属性赋值 --><!-- 属性column:指定要使用结果集的hid列的值进行子查询getOneWifeByHid --><!-- 属性select::指定要进行子查询的select标签的id --><associationproperty="wife"column="hid"select="getOneWifeByHid"/></resultMap><selectid="getOneWifeByHid"resultType="Wife">
select * from wife where wid=#{hid}
</select><selectid="getAll2"resultMap="husbandMap2">
select * from husband
</select>
6.2 1对1解决方案2:路径查询
<!-- 1对1解决方案:连接查询--><resultMaptype="Husband"id="husbandMap3"><!-- 给Husband的直接属性赋值 --><idcolumn="hid"property="hid"/><resultcolumn="hname"property="hname"/><resultcolumn="hsalary"property="hsalary"/><!-- 把结果集中wife表的列封装为Wife对象 赋值给当前对象的wife属性--><associationproperty="wife"javaType="Wife"><idcolumn="wid"property="wid"/><resultcolumn="wname"property="wname"/><resultcolumn="wage"property="wage"/></association></resultMap><selectid="getAll3"resultMap="husbandMap3">
select * from husband h left join wife w on h.hid=w.wid
</select>
<!-- 当不处理时:查询Stu 其kcList属性是肯定是null --><selectid="getAll1"resultType="Stu">
select * from stu
</select><!-- 解决n对n的方案1:子查询 --><resultMaptype="Stu"id="stuMap2"><!-- 把结果集中stu的列 封装为Stu对象 --><idcolumn="sid"property="sid"/><resultcolumn="sname"property="sname"/><resultcolumn="sex"property="sex"/><!-- 使用sid的值 对stu_ke_cheng关系表和ke_cheng 表进行子查询 获取的信息封装成KeCheng对象 赋值给kcList --><collectionproperty="kcList"ofType="KeCheng"select="getAllKeChengBySid"column="sid"/></resultMap><selectid="getAllKeChengBySid"resultType="KeCheng">
select * from stu_ke_cheng skc,ke_cheng kc where skc.gsid=#{sid} and skc.gkid=kc.kid
</select><selectid="getAll2"resultMap="stuMap2">
select * from stu
</select>
7.3 n对n解决方案2:连接查询
<!-- 解决n对n的方案2:连接查询--><resultMaptype="Stu"id="stuMap3"><!-- 把结果集中stu的列 封装为Stu对象 --><idcolumn="sid"property="sid"/><resultcolumn="sname"property="sname"/><resultcolumn="sex"property="sex"/><!-- 把结果集中kecheng的列封装为KeCheng对象 赋值给当前对象的kcList属性 --><collectionproperty="kcList"ofType="KeCheng"><idcolumn="kid"property="kid"/><resultcolumn="kname"property="kname"/><resultcolumn="kshow"property="kshow"/></collection></resultMap><selectid="getAll3"resultMap="stuMap3">
select * from stu s left join stu_ke_cheng skc on s.sid=skc.gsid
left join ke_cheng kc on kc.kid=skc.gkid
</select>