使用Mybatis如何遍写常用sql语句 mapper.xml如何遍写常用sql语句

<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="com.fh.mapper.StudentMapper">
  <!-- 条件查询-->
    <sql id="studentWhere">
        <where>
          <!-- 查询名称-->
            <if test="studentName != null and studentName != ''">
                s.studentName like concat('%',#{studentName},'%')
            </if>
             <!-- 查询年龄-->
            <if test="minage != null">
                and s.age &gt;= #{minage}
            </if>
            <if test="maxage != null">
                and s.age &lt;= #{maxage}
            </if>
				 <!-- 查询出生日期-->
            <if test="minDate != null">
                and s.birthday &gt;= #{minDate}
            </if>
            <if test="maxDate != null">
                and s.birthday &lt;= #{maxDate}
            </if>
 			<!-- 查询体重-->
            <if test="minweight != null">
                and s.weight &gt;= #{minweight}
            </if>
            <if test="maxweight != null">
                and s.weight &lt;= #{maxweight}
            </if>
          <!-- 查询班级下拉框  这是另一张表-->
          <if test="gradea != null and gradea != -1">
                and s.gradeId = #{gradea}
            </if>
       <!-- 查询复选框  爱好-->
            <if test="hobby != null and hobby != ''">
                and s.hobby in (${hobby})
            </if>
 			 <!-- 查询性别 单选-->
          <if test="sex != null">
                and s.sex = #{sex}
            </if>
        </where>
    </sql>

        <!-- 注册、登录时根据名称查询用户是否存在-->
    <select id="queryUserByUserName" parameterType="java.lang.String" resultType="User">
        select * from s_user where username=#{userName}
    </select>

    <!-- 查询总条数-->
    <select id="queryCount" resultType="long">
         select count(*) from s_student
        <include refid="studentWhere"></include>
    </select>

   <!-- 查询数据-->
    <select id="queryStudent" resultType="Student">
        select * from s_student s left join s_grade g on s.gradeId = g.gradeId <include refid="studentWhere"></include> limit #{start},#{length}
    </select>

    <!-- 查询班级-->
    <select id="queryGrade" resultType="Grade">
        select * from s_grade
    </select>

  <!--  回显-->
    <select id="queryStudentById" parameterType="int" resultType="Student">
        select * from s_student where id = #{id}
    </select>


  <!--  新增数据-->
    <select id="addStudent" parameterType="Student">
        insert into s_student (studentName,age,weight,sex,hobby,birthday,fileName,gradeId) values (#{studentName},#{age},#{weight},#{sex},#{hobby},#{birthday},#{fileName},#{gradeId})
    </select>


    <!--  修改数据-->
    <select id="uodateStudent" parameterType="Student">
 update s_student set studentName=#{studentName},age=#{age},weight=#{weight},sex=#{sex},hobby=#{hobby},weight=#{weight},birthday=#{birthday},fileName=#{fileName},gradeId=#{gradeId} where id=#{id}
    </select>

    <!--删除-->
    <delete id="deleteStudent" parameterType="int">
        delete from s_student where id = #{id}
    </delete>

    <delete id="pideleteStudent" parameterType="list">
        delete from s_student where id in
        <foreach collection="list" separator="," open="(" close=")" item="id">
            #{id}
        </foreach>
    </delete>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jq1223

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值