动态SQL
实现:select stuno,stuname from student where stuname = #{stuName}and stuage = #{stuAge}
第一种写法:
<select id="queryStuByNOrAWishSQLTag" parameterType="student" resultType="student" >
select stuno,stuname,stuage from student where 1=1
<if test="stuName !=null and stuName!='' ">
and stuname = #{stuName}
</if>
<if test="stuAge !=null and stuAge!=0 ">
and stuage = #{stuAge}
</if>
</select>
第二种写法:<where> 会自动处理第一个<if> 标签中的 and,但不会处理之后<if> 中的and
<select id="queryStuByNOrAWishSQLTag" parameterType="student" resultType="student" >
select stuno,stuname,stuage from student
<where>
<if test="stuName !=null and stuName!='' ">
and stuname = #{stuName}
</if>
<if test="stuAge !=null and stuAge!=0 ">
and stuage = #{stuAge}
</if>
</where>
</select>
实现:查询学号为1、2、3的学生信息 select stuno,stuname from student where stuno in(1,2,3)
<foreach> 迭代的类型:数组、对象数组、集合、属性(某个类中有属性: List<Integer>之类 )
public class Grade {
private List<Integer> stuNos ;
...
}
<select id="queryStudentsWithNosInGrade" parameterType="grade" resultType="student">
select * from student
<where>
<if test="stuNos!=null and stuNos.size>0">
<foreach collection="stuNos" open=" and stuno in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</if>
</where>
</select>
<select id="queryStudentsWithArray" parameterType="int[]" resultType="student">
select * from student
<where>
<if test="array!=null and array.length">
<foreach collection="array" open=" and stuno in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</if>
</where>
</select>
<select id="queryStudentsWithList" parameterType="list" resultType="student">
select * from student
<where>
<if test="list!=null and list.size>0">
<foreach collection="list" open=" and stuno in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</if>
</where>
</select>
对象数组形式的</foreach> :
Student[] students = {student0,student1,student2}
<select id="queryStudentsWithObjectArray" parameterType="Object[]" resultType="student">
select * from student
<where>
<if test="array!=null and array.length>0">
<foreach collection="array" open=" and stuno in (" close=")" item="stuNo" separator=",">
#{student.stuNo}
</foreach>
</if>
</where>
</select>
提取相同相似SQL片段:
<sql id="objectArrayStunos">
<where>
<if test="array!=null and array.length>0">
<foreach collection="array" open=" and stuno in (" close=")"
item="student" separator=",">
#{student.stuNo}
</foreach>
</if>
</where>
</sql>
<select id="queryStudentsWithObjectArray" parameterType="Object[]" resultType="student">
select * from student
<include refid="objectArrayStunos"></include>
</select>
关联查询
使用业务扩展类实现一对一:
<select id="queryStudentByNoWithOO" parameterType="int" resultType="StudentBusiness" >
select s.*,c.* from student s inner join studentcard c
on s.cardid=c.cardid
where s.stuno = #{stuNo}
</select>
@Data
public class StudentBusiness extends Student{
private int cardId;
private String cardInfo ;
}
使用 resultMap 实现一对一:
<select id="queryStudents" parameterType="int" resultMap="student_card_map">
select c.*,s.* from student s
inner join studentcard c
on s.classid = c.classid
where s.stuno = #{stuNo}
</select>
-->
<resultMap type="student" id="student_card_map">
<id property="stuNo" column="stuNo"/>
<result property="stuName" column="stuName"/>
<result property="stuAge" column="stuAge"/>
<association property="card" javaType="studentCard">
<id property="cardId" column="cardId"/>
<result property="cardInfo" column="cardInfo"/>
</association>
</resultMap>
@Data
public class Student implements Serializable{
private int stuNo ;
private String stuName ;
private int stuAge ;
private String graName ;
private boolean stuSex ;
private StudentCard card ;
}
使用 resultMap 实现一对多:
<select id="queryClassAndStudents" parameterType="int" resultMap="class_student_map">
select c.*,s.* from student s
inner join studentclass c
on c.classid = s.classid
where c.classid = #{classId}
</select>
<resultMap type="studentClass" id="class_student_map">
<id property="classId" column="classId"/>
<result property="className" column="className"/>
<collection property="students" ofType="student">
<id property="stuNo" column="stuNo"/>
<result property="stuName" column="stuName"/>
<result property="stuAge" column="stuAge"/>
</collection>
</resultMap>
public class StudentClass {
private int classId;
private String className;
List<Student> students ;
}
|