怎么编写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>


<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编写规范


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

相关文章
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
XML Java 数据库连接
二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作)
二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作)
609 21
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
人工智能 Java 数据库连接
MyBatis Plus 使用 Service 接口进行增删改查
本文介绍了基于 MyBatis-Plus 的数据库操作流程,包括配置、实体类、Service 层及 Mapper 层的创建。通过在 `application.yml` 中配置 SQL 日志打印,确保调试便利。示例中新建了 `UserTableEntity` 实体类映射 `sys_user` 表,并构建了 `UserService` 和 `UserServiceImpl` 处理业务逻辑,同时定义了 `UserTableMapper` 进行数据交互。测试部分展示了查询、插入、删除和更新的操作方法及输出结果,帮助开发者快速上手 MyBatis-Plus 数据持久化框架。
1207 0
|
SQL 关系型数据库 MySQL
MySQL导入.sql文件后数据库乱码问题
本文分析了导入.sql文件后数据库备注出现乱码的原因,包括字符集不匹配、备注内容编码问题及MySQL版本或配置问题,并提供了详细的解决步骤,如检查和统一字符集设置、修改客户端连接方式、检查MySQL配置等,确保导入过程顺利。
|
SQL 数据库
为什么 SQL 日志文件很大,我应该如何处理?
为什么 SQL 日志文件很大,我应该如何处理?
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1853 1
Spring boot 使用mybatis generator 自动生成代码插件
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
1039 2
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
954 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
3702 2
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个