天天看点

怎么编写mybatis.xml文件,实现sql增删改查

<?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 test="organizationId != null">

and organization_id = #{organizationId}

<if test="carModel != null">

  and car_model like CONCAT('%',#{carModel},'%')  

<if test="carowner != null">

  and carowner like CONCAT('%',#{carowner},'%')   

<if test="carType != null">

and car_type = #{carType}

<if test="frameNo != null">

and frame_no like CONCAT('%',#{frameNo},'%')

<if test="engineNo != null">

and engine_no like CONCAT('%',#{engineNo},'%')

<if test="carPrice != null">

and car_price = #{carPrice}

<if test="oilCard != null">

and oil_card like CONCAT('%',#{oilCard},'%')

<if test="firstRegistDate != null">

and first_regist_date = #{firstRegistDate}

<if test="insuranceDate != null">

and insurance_date = #{insuranceDate}

<if test="insuranceAmount != null">

and insurance_amount = #{insuranceAmount}

<if test="checkDate != null">

and check_date = #{checkDate}

<if test="remark != null">

and remark = #{remark} 

<if test="createUser != null">

and create_user = #{createUser}

<if test="updateTime !=null">

and update_time = #{updateTime}

<if test="updateUser !=null">

and update_user = #{updateUser}

<if test="deviceNo !=null">

and device_no like CONCAT('%',#{deviceNo},'%')

<if test="attr1 !=null">

and attr1 = #{attr1}

<if test="attr2 !=null">

and attr2 = #{attr2}

<if test="attr3 !=null">

and attr3 = #{attr3}

<if test="attr4 !=null">

and attr4 = #{attr4}

<if test="attr5 !=null">

and attr5 = #{attr5}

<if test="beginTime != null">

      <![CDATA[ and check_date>=#{beginTime}]]>

       </if>

       <if test="endTime != null">

       <![CDATA[ and check_date<=#{endTime}]]>

<if test="deleteFlag != null">

and delete_flag = #{deleteFlag}

<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>

<!-- 插入车辆数据 -->

<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 id="deleteByIds" >

delete from tb_car_info where uid in

<foreach collection="array" index="index" item="item" open="(" separator="," close=")">

#{item}

</foreach>

<!-- 更新数据 -->

<update id="updateById" parameterType="CarInfo">

update tb_car_info

<set>

license = #{license},

<if test="driverId != null">

driver_id = #{driverId},

organization_id =#{organizationId},

 car_model = #{carModel}, 

car_type = #{carType},

frame_no = #{frameNo},

engine_no = #{engineNo},

car_price = #{carPrice},

oil_card = #{oilCard},

first_regist_date = #{firstRegistDate},

insurance_date = #{insuranceDate},

insurance_amount = #{insuranceAmount},

check_date = #{checkDate},

remark = #{remark},

create_user = #{createUser},

update_time = #{updateTime},

update_user = #{updateUser},

<if test="carowner !=null">

carowner = #{carowner},

device_no = #{deviceNo},

delete_flag = #{deleteFlag}

attr1 = #{attr1},

attr2 = #{attr2},

attr3 = #{attr3},

attr4 = #{attr4},

attr5 = #{attr5}

</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}

<!-- 查询 -->

<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}

<!-- 按Map查询 -->

<select id="findByMap" resultType="CarInfo" parameterType="java.util.Map">

<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 test="offset != null and pageSize != null">

limit #{offset} , #{pageSize}

<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 id="countByMap" resultType="java.lang.Integer"

parameterType="java.util.Map">

select count(*) from tb_car_info

</mapper>

本文转自 沉淀人生 51CTO博客,原文链接:http://blog.51cto.com/825272560/1882014