mybatis常见的增删改查语句和条件查询

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.EmpDao">

    <!--    查所有-->
    <select id="SelectAllEmp" resultType="HashMap">
        select * from emp,dept where emp.deptno = dept.deptno
    </select>

    <!--    新增-->
    <insert id="addEmp" parameterType="com.sky.pojo.Emp" >
        insert into emp VALUES (#{empno},#{ename},#{job},#{salary},#{mgr},#{bonus},#{hiredate},#{deptno})
    </insert>


<!--    分页  -->
    <select id="getEmplimits" parameterType="map" resultType="HashMap">
        select * from emp limit #{startIndex},#{pageSize}
    </select>


<!--    登录查询 -->
    <select id="selectEmpByIdAndPwd" parameterType="map" resultType="HashMap">
        select username,password from user where username = #{username} and password = #{password}
    </select>

<!--    条件查询-->
    <select id="GetUserByIdAndAge" resultType="HashMap">
        select * from user
        <where>
        <if test="sex !=null and sex !=''">
            and sex = #{sex}
        </if>
        <if test="name !=null and name !=''">
            and name like concat('%',#{name},'%')
        </if>
        <if test="age != null and age !=''">
            and age > #{age}
        </if>
        </where>
    </select>

<!--    模糊条件查询-->
    <select id="getUserByNameAndAge" resultType="HashMap">
        select * from user where sex='男'
        <choose>
            <when test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="age != null and age != '' and age != 0">
                and age = #{age}
            </when>
        <otherwise>
            and age &lt; 18
        </otherwise>
        </choose>
    </select>

<!--    批量删除 -->
    <delete id="deleteEmp">
        delete from emp where empno in
        <foreach collection="ids" separator="," open="(" close=")" item="id">
            #{id}
        </foreach>
    </delete>

<!--    修改信息 -->
    <update id="updateEmp" parameterType="com.sky.pojo.Emp">
        UPDATE `kg16`.`emp`
        <set>
            <if test="ename != null and ename != ''">
                ename= #{ename},
            </if>
            <if test="job != null and job != ''">
                job= #{job},
            </if>
            <if test="salary != null and salary != ''">
                salary= #{salary},
            </if>
            <if test="mgr != null and mgr != ''">
                mgr= #{mgr},
            </if>
            <if test="bonus != null and bonus != ''">
                bonus= #{bonus},
            </if>
            <if test="deptno != null and deptno != ''">
                deptno= #{deptno},
            </if>
        </set>
        WHERE
            `empno` = #{empno};
    </update>

    <!--    根据员工id查看他的信息和他的部门信息  *用对象封装* -->
    <resultMap id="kkk" type="com.sky.pojo.Emp" autoMapping="true">
<!--        <id column="empno" property="empno"/>-->
<!--        <result property="deptno" column="deptno"/>-->
        <association property="dept" javaType="com.sky.pojo.Dept">
<!--            <id property="deptno" column="deptno"/>-->
<!--            <result column="dlocation" property="dlocation"/>-->
<!--            <result column="dname" property="dname"/>-->
        </association>
    </resultMap>
    <select id="getEmpById" resultMap="kkk">
        select * from emp left join dept d on emp.deptno = d.deptno where empno=#{id}
    </select>



<!--    解决表面列表不一致-->
    <resultMap id="ccc" type="com.sky.pojo.Dept" autoMapping="true">
<!--                <id column="deptno" property="deptno"/>-->
<!--                <result property="dname" column="dname"/>-->
<!--                <result property="dlocation" column="dlocation"/>-->
        <collection property="emps" javaType="List" ofType="Emp" autoMapping="true">
<!--            <id property="empno" column="empno"/>-->
<!--            <result property="ename" column="ename"/>-->
        </collection>
    </resultMap>
    <select id="getEmpBydeptno" resultMap="ccc">
        SELECT * FROM dept LEFT JOIN emp e on dept.deptno = e.deptno where e.deptno = #{deptno}
    </select>

</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值