您的位置:首页 > 其它

典型的查询语句

2015-06-26 16:40 330 查看
1.多个关联

<!--老师个人中心需要的信息 wuyiwen  -->
<select id="getActivitiesByTeacherId" resultMap="resultListTeachers">
select
ac.id as acid,ac.maxNum as acmaxNum, aa.id as aaid,aa.name as aaname,
aa.publishUser,aa.startDate as aastartDate,aa.endDate as aaendDate,
aa.courseStartDate,aa.courseEndDate,aa.admissions,aa.warning as aawarning,
aa.status,c.name as cname,c.type as ctype,c.id as cid,t.name as tname ,
t.id as tId,act.hour as actHour,act.startTime as actStartTime,act.endTime as actEndTime,
count(sc.id) as count
from aa_activitycourse ac
left join  bd_course c
on ac.id_course = c.id
left join aa_activitycourseteacher acte
on ac.id = acte.id_activitycourse
left join bd_teacher t
on t.id = acte.id_teacher
left join aa_applyactivity aa
on aa.id=ac.id_applyactivity
left join aa_activitycoursetime act
on act.id_activitycourse=ac.id
left join aa_studentcourse  sc
on ac.id = sc.id_activitycourse
where t.id = #{teacherId}
<if test="activeName != null and activeName != '' ">
and aa.name like CONCAT('%',#{activeName},'%')
</if>
<if test="courseName != null and courseName != '' ">
and c.name like CONCAT('%',#{courseName},'%')
</if>
GROUP BY sc.id_activitycourse,sc.id_applyactivity
order by aa.endDate
</select>


<!--老师个人的活动课程  -->
<resultMap type="ActivityCourse" id="resultListTeachers">
<id column="acid" property="id" />
<result column="acmaxNum" property="maxNum"/>
<result column="count" property="courseReportCount"/>

<association property="applyActivity" javaType="ApplyActivity">
<id column="aaid" property="id" />
<result column="aaname" property="name"/>
<result column="publishUser" property="publishUser"/>
<result column="aastartDate" property="startDate" />
<result column="aaendDate" property="endDate" />
<result column="admissions" property="admissions" />
<result column="aawarning" property="warning" />
<result column="courseStartDate" property="courseStartDate" />
<result column="courseEndDate" property="courseEndDate" />
<result column="status" property="status" />
</association>

<association property="course" javaType="Course">
<id column="cid" property="id" />
<result column="cname" property="name"/>
<result column="ctype" property="type"/>

</association>

<collection property="activityCourseTeachers" ofType="ActivityCourseTeacher">
<id column="actId" property="id" />
<association property="teacher" javaType="Teacher">
<id column="tId" property="id" />
<result column="tname" property="name" />
</association>
</collection>

<collection property="activityCourseTimes" ofType="ActivityCourseTime">
<id column="timeId" property="id" />
<result column="actStartTime" property="startTime"/>
<result column="actHour" property="hour"/>
<result column="actEndTime" property="endTime"/>
<result column="place" property="place"/>
</collection>

</resultMap>


2.条件查询

<select id="getPage" resultMap="resultListStudent" parameterType="page">
select
stu.id id,stu.name name,stu.sex sex,stu.birthDate birthDate,
stu.address address,stu.identityCard identityCard,stu.email email,
stu.parentPhone1 parentPhone1,stu.parentPhone2 parentPhone2, stu.ft ft,
sch.id as id_school,sch.name as schoolName,
are.id as areaId ,are.name as areaName
from bd_student stu
left join bd_school sch
on stu.id_school=sch.id
left join bd_area are
on sch.id_area=are.id
left join bd_user us
on us.id=stu.id
where 1=1 and us.status=1
<if test="params.name != null and params.name != '' ">
and stu.name like CONCAT('%',#{params.name},'%')
</if>
<if test="params.sex != null and params.sex != '' ">
and stu.sex=#{params.sex}
</if>
<if test="params.birthDate != null and params.birthDate != '' ">
and stu.birthDate=#{params.birthDate}
</if>
<if test="params.address != null and params.address != '' ">
and stu.address=#{params.address}
</if>
<if test="params.identityCard != null and params.identityCard != '' ">
and stu.identityCard like CONCAT('%',#{params.identityCard},'%')
</if>
<if test="params.email != null and params.email != '' ">
and stu.email=#{params.email}
</if>
<if test="params.parentPhone1 != null and params.parentPhone1 != '' ">
and stu.parentPhone1=#{params.parentPhone1}
</if>
<if test="params.parentPhone2 != null and params.parentPhone2 != '' ">
and stu.parentPhone2=#{params.parentPhone2}
</if>
<if test="params.schoolId != null and params.schoolId != '' ">
and stu.id_school=#{params.schoolId}
</if>
<if test="params.areaId != null and params.areaId != ''">
and sch.id_area=#{params.areaId}
</if>
order by stu.id desc
</select>


3.更新

<!--只对非空字段进行更新  -->
<update id="update" parameterType="Student">
update bd_student
<set>
<if test="name != null and name != '' ">
name=#{name},
</if>
<if test="sex != null and sex != '' ">
sex=#{sex},
</if>
<if test="birthDate != null and birthDate != '' ">
birthDate=#{birthDate},
</if>
<if test="address != null and address != '' ">
address=#{address},
</if>
<if test="identityCard != null and identityCard != '' ">
identityCard=#{identityCard},
</if>
<if test="email != null and email != '' ">
email=#{email},
</if>
<if test="email != null and email == '' ">
email=#{email},
</if>

<if test="parentPhone1 != null and parentPhone1 != '' ">
parentPhone1=#{parentPhone1},
</if>
<if test="parentPhone2 != null and parentPhone2 != '' ">
parentPhone2=#{parentPhone2},
</if>
<if test="parentPhone2 != null and parentPhone2 == '' ">
parentPhone2=#{parentPhone2},
</if>
<if test="school!=null and school.id != null and school.id != '' ">
id_school=#{school.id},
</if>
</set>
WHERE id=#{id}
</update>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: