设为首页 收藏本站
查看: 1405|回复: 0

[经验分享] 怎么编写mybatis.xml文件,实现sql增删改查

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-13 08:19:09 | 显示全部楼层 |阅读模式
<?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.shangyu.mapper.dsz.CarInfoMapper">
        <!-- 查询字段 -->
        <sql id="Base_Column_List">
                uid, supplier_id, license, driver_id, organization_id, car_model, car_type, frame_no, engine_no, car_price,
                oil_card, first_regist_date, insurance_date, insurance_amount, check_date, remark, create_user, create_time, update_time, update_user,carowner,
                device_no, attr1, attr2, attr3, attr4, attr5, delete_flag
        </sql>
        <!-- 查询条件 -->
        <sql id="selectWhereClause">
                <where>
                        <trim prefixOverrides="and">
                                <if test="keyword != null">
                                        and (license like CONCAT('%',#{keyword},'%')
                                        OR car_model like CONCAT('%',#{keyword},'%')
                                        OR carowner like CONCAT('%',#{keyword},'%')
                                        OR frame_no like CONCAT('%',#{keyword},'%')
                                        OR engine_no like CONCAT('%',#{keyword},'%')
                                        OR oil_card like CONCAT('%',#{keyword},'%')
                                        OR device_no like CONCAT('%',#{keyword},'%')
                                       
                                        )
                                </if>
                                <if test="license != null">
                                        and license like CONCAT('%',#{license},'%')
                                </if>

                                <if test="organizationId != null">
                                        and organization_id = #{organizationId}
                                </if>
                                <if test="carModel != null">
                                         and car_model like CONCAT('%',#{carModel},'%')  
                                </if>
                                <if test="carowner != null">
                                         and carowner like CONCAT('%',#{carowner},'%')   
                                </if>
                                <if test="carType != null">
                                        and car_type = #{carType}
                                </if>
                                <if test="frameNo != null">
                                        and frame_no like CONCAT('%',#{frameNo},'%')
                                </if>
                                <if test="engineNo != null">
                                        and engine_no like CONCAT('%',#{engineNo},'%')
                                </if>
                                <if test="carPrice != null">
                                        and car_price = #{carPrice}
                                </if>
                                <if test="oilCard != null">
                                        and oil_card like CONCAT('%',#{oilCard},'%')
                                </if>
                                <if test="firstRegistDate != null">
                                        and first_regist_date = #{firstRegistDate}
                                </if>
                                <if test="insuranceDate != null">
                                        and insurance_date = #{insuranceDate}
                                </if>
                                <if test="insuranceAmount != null">
                                        and insurance_amount = #{insuranceAmount}
                                </if>
                                <if test="checkDate != null">
                                        and check_date = #{checkDate}
                                </if>
                                <if test="remark != null">
                                        and remark = #{remark}
                                </if>
                                <if test="createUser != null">
                                        and create_user = #{createUser}
                                </if>
                                <if test="updateTime !=null">
                                        and update_time = #{updateTime}
                                </if>
                                <if test="updateUser !=null">
                                        and update_user = #{updateUser}
                                </if>
                                <if test="deviceNo !=null">
                                        and device_no like CONCAT('%',#{deviceNo},'%')
                                </if>
                                <if test="attr1 !=null">
                                        and attr1 = #{attr1}
                                </if>
                                <if test="attr2 !=null">
                                        and attr2 = #{attr2}
                                </if>
                                <if test="attr3 !=null">
                                        and attr3 = #{attr3}
                                </if>
                                <if test="attr4 !=null">
                                        and attr4 = #{attr4}
                                </if>
                                <if test="attr5 !=null">
                                        and attr5 = #{attr5}
                                </if>

                                <if test="beginTime != null">
                                     <![CDATA[ and check_date>=#{beginTime}]]>
                              </if>
                              <if test="endTime != null">
                                      <![CDATA[ and check_date<=#{endTime}]]>
                              </if>

                                <if test="deleteFlag != null">
                                        and delete_flag = #{deleteFlag}
                                </if>
                                <if test="driverUsers != null">
                                      and uid in(select a.carinfo_id from tb_car_info_driver a where a.delete_flag='0' and a.driver_user=#{driverUsers})
                              </if>
                        </trim>
                </where>
        </sql>
        <!-- 插入车辆数据 -->
        <insert id="insert" parameterType="CarInfo">
                insert into tb_car_info(
                        uid, supplier_id, license, driver_id, organization_id, car_model, car_type, frame_no, engine_no, car_price,oil_card, first_regist_date,
                        insurance_date, insurance_amount, check_date, remark, create_user, create_time,update_time, update_user,carowner,device_no,attr1,attr2,attr3,attr4,attr5,delete_flag
                ) values (
                        #{uid}, #{supplierId}, #{license}, #{driverId}, #{organizationId}, #{carModel}, #{carType}, #{frameNo}, #{engineNo}, #{carPrice},#{oilCard}, #{firstRegistDate},
                        #{insuranceDate}, #{insuranceAmount}, #{checkDate}, #{remark}, #{createUser}, #{createTime}, #{updateTime}, #{updateUser},#{carowner},#{deviceNo},#{attr1},#{attr2},
                        #{attr3},#{attr4},#{attr5},#{deleteFlag}
                )
        </insert>
        <!-- 批量插入司机数据 -->
        <insert id="insertUser">
            insert into tb_car_info_driver (carinfo_id, driver_user, delete_flag) values
            <foreach collection="driverUsers" item="item" index="index" separator="," >  
            (#{uid}, #{item}, #{deleteFlag})
            </foreach>
          </insert>
         
        <!-- 单个删除车辆 -->
        <delete id="deleteById" parameterType="CarInfo">
                delete from tb_car_info where uid = #{uid}
        </delete>
        <!-- 单个删除司机 -->
        <delete id="deleteDriverById" parameterType="java.lang.String" >
                 delete from tb_car_info_driver where delete_flag = '0' and carinfo_id = #{uid}
        </delete>
       
        <!-- 批量删除 -->
        <delete id="deleteByIds" >
                delete from tb_car_info where uid in
                <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
                        #{item}
                </foreach>
        </delete>
       
        <!-- 更新数据 -->
        <update id="updateById" parameterType="CarInfo">
                update tb_car_info
                <set>
                        <if test="license != null">
                                license = #{license},
                        </if>
                        <if test="driverId != null">
                                driver_id = #{driverId},
                        </if>
                        <if test="organizationId != null">
                                organization_id =#{organizationId},
                        </if>
                        <if test="carModel != null">
                                 car_model = #{carModel},
                        </if>
                        <if test="carType != null">
                                car_type = #{carType},
                        </if>
                        <if test="frameNo != null">
                                frame_no = #{frameNo},
                        </if>
                        <if test="engineNo != null">
                                engine_no = #{engineNo},
                        </if>
                        <if test="carPrice != null">
                                car_price = #{carPrice},
                        </if>
                        <if test="oilCard != null">
                                oil_card = #{oilCard},
                        </if>
                        <if test="firstRegistDate != null">
                                first_regist_date = #{firstRegistDate},
                        </if>
                       
                        <if test="insuranceDate != null">
                                insurance_date = #{insuranceDate},
                        </if>
                        <if test="insuranceAmount != null">
                                insurance_amount = #{insuranceAmount},
                        </if>
                        <if test="checkDate != null">
                                check_date = #{checkDate},
                        </if>
                        <if test="remark != null">
                                remark = #{remark},
                        </if>
                        <if test="createUser != null">
                                create_user = #{createUser},
                        </if>
                        <if test="updateTime !=null">
                                update_time = #{updateTime},
                        </if>
                        <if test="updateUser !=null">
                                update_user = #{updateUser},
                        </if>
                        <if test="carowner !=null">
                                carowner = #{carowner},
                        </if>
                        <if test="deviceNo !=null">
                                device_no = #{deviceNo},
                        </if>
                        <if test="deleteFlag != null">
                                delete_flag = #{deleteFlag}
                        </if>
                        <if test="attr1 !=null">
                                attr1 = #{attr1},
                        </if>
                        <if test="attr2 !=null">
                                attr2 = #{attr2},
                        </if>
                        <if test="attr3 !=null">
                                attr3 = #{attr3},
                        </if>
                        <if test="attr4 !=null">
                                attr4 = #{attr4},
                        </if>
                        <if test="attr5 !=null">
                                attr5 = #{attr5}
                        </if>
                </set>
                where uid = #{uid}
        </update>
        <!-- 更新司机 -->
        <update id="updateDriverById" parameterType="java.lang.String" >
            update tb_car_info_driver set delete_flag = '1' where delete_flag = '0' and carinfo_id = #{uid}
        </update>
       
        <!-- 查询 -->
        <select id="getById" resultType="CarInfo" parameterType="java.lang.String">
                select
                <include refid="Base_Column_List"/>
                from tb_car_info
                where uid=#{uid}
        </select>
       
        <!-- 查询司机 -->
        <select id="getByDriverId" resultType="java.lang.String" parameterType="java.lang.String">
                select driver_user from tb_car_info_driver where delete_flag='0' and carinfo_id=#{uid}
        </select>
       
        <!-- 按Map查询 -->
        <select id="findByMap" resultType="CarInfo" parameterType="java.util.Map">
                select
                <include refid="Base_Column_List" />,
                 (SELECT  CASE WHEN TIMESTAMPDIFF(MINUTE,create_time,NOW())>5 THEN 3 ELSE STATUS END FROM tb_gps_record WHERE device_no=ci.device_no ORDER BY create_time DESC LIMIT 1) AS STATUS
                from tb_car_info ci
                <include refid="selectWhereClause" />
                <if test="orderByClause != null">
                        order by ${orderByClause}
                </if>
                <if test="offset != null and pageSize != null">
                        limit #{offset} , #{pageSize}
                </if>
        </select>
       
        <select id="findByDriverNull" resultType="CarInfo" parameterType="java.util.Map">
                SELECT <include refid="Base_Column_List" />
                FROM tb_car_info b WHERE
                b.uid NOT IN(
        SELECT a.`carinfo_id` FROM tb_car_info_driver a where a.delete_flag='0')
        </select>
        <!-- 统计数量 -->
        <select id="countByMap" resultType="java.lang.Integer"
                parameterType="java.util.Map">
                select count(*) from tb_car_info
                <include refid="selectWhereClause" />
        </select>

</mapper>


案例:mybatis.xml中sql编写规范


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-313454-1-1.html 上篇帖子: MyBatis批量插入 下篇帖子: SSM三大框架整合详细教程Spring+SpringMVC+MyBatis+maven
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表