在项目中,我们经常会用到sql语句为我们的数据库和前端进行连接,各种繁琐的sql也让很多人有所困惑,今天我就来给大家分享一种超级简单的sql语句的写法。
一、将数据库与实体类一一对应。
<resultMap id="BaseResultMap" type="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
<id column="lawyerInfoId" property="lawyerInfoId" jdbcType="VARCHAR" />
<result column="lawyerSurname" property="lawyerSurname" jdbcType="VARCHAR" />
<result column="lawyerName" property="lawyerName" jdbcType="VARCHAR" />
<result column="lawyerAge" property="lawyerAge" jdbcType="VARCHAR" />
<result column="projectName" property="projectName" jdbcType="VARCHAR" />
<result column="locationCodePath" property="locationCodePath" jdbcType="VARCHAR" />
<result column="projectCountry" property="projectCountry" jdbcType="VARCHAR" />
</resultMap>
二、查看SQL。
1、将所有字段写出,取名为Base_Column_List。
<sql id="Base_Column_List" >
lawyerInfoId, lawyerSurname, lawyerName, lawyerAge, projectName, locationCodePath, projectCountry,
lawyerSex, lawyerPost, languages, mailBox, expert, Telephone, expatriates, modifierId, modifierName, modifyTime,
createrId, createrName, createTime, isDelete
</sql>
2、如果isDelete = 0,不删除数据,默认为不删除。
<sql id="common_where_if">
isDelete = 0
<if test="lawyerSurname != null and lawyerSurname != ''">
AND lawyerSurname LIKE Concat('%', #{lawyerSurname,jdbcType=VARCHAR}, '%')
</if>
<if test="lawyerName != null and lawyerName != ''">
AND lawyerName LIKE Concat('%', #{lawyerName,jdbcType=VARCHAR}, '%')
</if>
<if test="lawyerAge != null and lawyerAge != ''">
AND adminUserNames LIKE Concat('%', #{adminUserNames,jdbcType=VARCHAR}, '%')
</if>
<if test="projectName != null and projectName != ''">
AND projectName = #{projectName,jdbcType=VARCHAR}
</if>
<if test="locationCodePath != null and locationCodePath != ''">
AND locationCodePath = #{locationCodePath,jdbcType=VARCHAR}
</if>
<if test="projectCountry != null and projectCountry != ''">
AND projectCountry = #{projectCountry,jdbcType=VARCHAR}
</if>
</sql>
3、查询项目列表。
<!-- 查询项目列表 -->
<select id="queryProjectBaseInfoList" resultType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" parameterType="java.util.Map" >
select
<include refid="Base_Column_List"></include>
from lrm_lawyer_abroad
<where>
<include refid="common_where_if"></include>
</where>
order by createTime desc
</select>
4、获取数据详情。
<!-- 获取数据详情 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from lrm_lawyer_abroad
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</select>
三、根据Id删除数据。
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from lrm_lawyer_abroad
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</delete>
四、新增数据。
1、新增所有。
<insert id="insert" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
insert into lrm_lawyer_abroad (lawyerInfoId,
expert, mailBox, Telephone, expatriates,
modifierId, modifierName, modifyTime,
createrId, createrName, createTime,
isDelete)
values (#{lawyerInfoId,jdbcType=VARCHAR}, #{lawyerSurname,jdbcType=VARCHAR}, #{lawyerName,jdbcType=VARCHAR},
#{lawyerAge,jdbcType=VARCHAR}, #{projectName,jdbcType=VARCHAR}, #{locationCodePath,jdbcType=VARCHAR}, #{projectCountry,jdbcType=VARCHAR},
#{lawyerSex,jdbcType=VARCHAR}, #{lawyerPost,jdbcType=VARCHAR}, #{languages,jdbcType=VARCHAR},
#{expert,jdbcType=VARCHAR},#{mailBox,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR},
#{expatriates,jdbcType=VARCHAR},#{modifierId,jdbcType=VARCHAR}, #{modifierName,jdbcType=VARCHAR},
#{modifyTime,jdbcType=TIMESTAMP}, #{createrId,jdbcType=VARCHAR}, #{createrName,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{isDelete,jdbcType=INTEGER})
</insert>
2、根据条件新增。
<insert id="insertSelective" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
insert into lrm_lawyer_abroad
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="lawyerInfoId != null" >
lawyerInfoId,
</if>
<if test="languages != null" >
languages,
</if>
<if test="expert != null" >
expert,
</if>
<if test="expatriates != null" >
expatriates,
</if>
<if test="modifierId != null" >
modifierId,
</if>
<if test="modifierName != null" >
modifierName,
</if>
<if test="modifyTime != null" >
modifyTime,
</if>
<if test="createrId != null" >
createrId,
</if>
<if test="createrName != null" >
createrName,
</if>
<if test="createTime != null" >
createTime,
</if>
<if test="isDelete != null" >
isDelete,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="lawyerInfoId != null" >
#{lawyerInfoId,jdbcType=VARCHAR},
</if>
<if test="lawyerSurname != null" >
#{lawyerSurname,jdbcType=VARCHAR},
</if>
<if test="lawyerName != null" >
#{lawyerName,jdbcType=VARCHAR},
</if>
<if test="lawyerAge != null" >
#{lawyerAge,jdbcType=VARCHAR},
</if>
<if test="expert != null" >
#{expert,jdbcType=VARCHAR},
</if>
<if test="mailBox != null" >
#{mailBox,jdbcType=VARCHAR},
</if>
<if test="telephone != null" >
#{telephone,jdbcType=VARCHAR},
</if>
<if test="expatriates != null" >
#{expatriates,jdbcType=VARCHAR},
</if>
<if test="modifierId != null" >
#{modifierId,jdbcType=VARCHAR},
</if>
<if test="modifierName != null" >
#{modifierName,jdbcType=VARCHAR},
</if>
<if test="modifyTime != null" >
#{modifyTime,jdbcType=TIMESTAMP},
</if>
<if test="createrId != null" >
#{createrId,jdbcType=VARCHAR},
</if>
<if test="createrName != null" >
#{createrName,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="isDelete != null" >
#{isDelete,jdbcType=INTEGER},
</if>
</trim>
</insert>
五、修改数据库数据。
<update id="updateByPrimaryKeySelective" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
update lrm_lawyer_abroad
<set >
<if test="lawyerSurname != null" >
lawyerSurname = #{lawyerSurname,jdbcType=VARCHAR},
</if>
<if test="lawyerName != null" >
lawyerName = #{lawyerName,jdbcType=VARCHAR},
</if>
<if test="expert != null" >
expert = #{expert,jdbcType=VARCHAR},
</if>
<if test="modifierId != null" >
modifierId = #{modifierId,jdbcType=VARCHAR},
</if>
<if test="modifierName != null" >
modifierName = #{modifierName,jdbcType=VARCHAR},
</if>
<if test="modifyTime != null" >
modifyTime = #{modifyTime,jdbcType=TIMESTAMP},
</if>
<if test="createrId != null" >
createrId = #{createrId,jdbcType=VARCHAR},
</if>
<if test="createrName != null" >
createrName = #{createrName,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
createTime = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="isDelete != null" >
isDelete = #{isDelete,jdbcType=INTEGER},
</if>
</set>
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
update lrm_lawyer_abroad
set lawyerSurname = #{lawyerSurname,jdbcType=VARCHAR},
lawyerName = #{lawyerName,jdbcType=VARCHAR},
modifierId = #{modifierId,jdbcType=VARCHAR},
modifierName = #{modifierName,jdbcType=VARCHAR},
modifyTime = #{modifyTime,jdbcType=TIMESTAMP},
createrId = #{createrId,jdbcType=VARCHAR},
createrName = #{createrName,jdbcType=VARCHAR},
createTime = #{createTime,jdbcType=TIMESTAMP},
isDelete = #{isDelete,jdbcType=INTEGER}
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</update>
即可实现增删改查功能,最后附上完整代码。
<?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.pcitc.slms.lrm.system.dao.LrmLawyerAbroadDao" >
<resultMap id="BaseResultMap" type="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
<id column="lawyerInfoId" property="lawyerInfoId" jdbcType="VARCHAR" />
<result column="lawyerSurname" property="lawyerSurname" jdbcType="VARCHAR" />
<result column="lawyerName" property="lawyerName" jdbcType="VARCHAR" />
<result column="lawyerAge" property="lawyerAge" jdbcType="VARCHAR" />
<result column="projectName" property="projectName" jdbcType="VARCHAR" />
<result column="locationCodePath" property="locationCodePath" jdbcType="VARCHAR" />
<result column="projectCountry" property="projectCountry" jdbcType="VARCHAR" />
<result column="lawyerSex" property="lawyerSex" jdbcType="VARCHAR" />
<result column="lawyerPost" property="lawyerPost" jdbcType="VARCHAR" />
<result column="languages" property="languages" jdbcType="VARCHAR" />
<result column="expert" property="expert" jdbcType="VARCHAR" />
<result column="mailBox" property="mailBox" jdbcType="VARCHAR" />
<result column="Telephone" property="telephone" jdbcType="VARCHAR" />
<result column="expatriates" property="expatriates" jdbcType="VARCHAR" />
<result column="modifierId" property="modifierId" jdbcType="VARCHAR" />
<result column="modifierName" property="modifierName" jdbcType="VARCHAR" />
<result column="modifyTime" property="modifyTime" jdbcType="TIMESTAMP" />
<result column="createrId" property="createrId" jdbcType="VARCHAR" />
<result column="createrName" property="createrName" jdbcType="VARCHAR" />
<result column="createTime" property="createTime" jdbcType="TIMESTAMP" />
<result column="isDelete" property="isDelete" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
lawyerInfoId, lawyerSurname, lawyerName, lawyerAge, projectName, locationCodePath, projectCountry,
lawyerSex, lawyerPost, languages, mailBox, expert, Telephone, expatriates, modifierId, modifierName, modifyTime,
createrId, createrName, createTime, isDelete
</sql>
<sql id="common_where_if">
isDelete = 0
<if test="lawyerSurname != null and lawyerSurname != ''">
AND lawyerSurname LIKE Concat('%', #{lawyerSurname,jdbcType=VARCHAR}, '%')
</if>
<if test="lawyerName != null and lawyerName != ''">
AND lawyerName LIKE Concat('%', #{lawyerName,jdbcType=VARCHAR}, '%')
</if>
<if test="lawyerAge != null and lawyerAge != ''">
AND adminUserNames LIKE Concat('%', #{adminUserNames,jdbcType=VARCHAR}, '%')
</if>
<if test="projectName != null and projectName != ''">
AND projectName = #{projectName,jdbcType=VARCHAR}
</if>
<if test="locationCodePath != null and locationCodePath != ''">
AND locationCodePath = #{locationCodePath,jdbcType=VARCHAR}
</if>
<if test="projectCountry != null and projectCountry != ''">
AND projectCountry = #{projectCountry,jdbcType=VARCHAR}
</if>
<if test="lawyerSex != null and lawyerSex != ''">
AND lawyerSex = #{lawyerSex,jdbcType=VARCHAR}
</if>
<if test="lawyerPost != null and lawyerPost != ''">
AND lawyerPost = #{lawyerPost,jdbcType=VARCHAR}
</if>
<if test="languages != null and languages != ''">
AND languages = #{languages,jdbcType=VARCHAR}
</if>
<if test="expert != null and expert != ''">
AND expert = #{expert,jdbcType=VARCHAR}
</if>
<if test="mailBox != null and mailBox != ''">
AND mailBox = #{mailBox,jdbcType=VARCHAR}
</if>
<if test="Telephone != null and Telephone != ''">
AND Telephone = #{Telephone,jdbcType=VARCHAR}
</if>
<if test="expatriates != null and expatriates != ''">
AND expatriates = #{expatriates,jdbcType=VARCHAR}
</if>
<if test="createrId != null and createrId != ''">
AND createrId = #{createrId,jdbcType=VARCHAR}
</if>
<if test="createrName != null and createrName != ''">
AND createrName = #{createrName,jdbcType=VARCHAR}
</if>
<if test="createTime != null and createTime != ''">
AND createTime = #{createTime,jdbcType=VARCHAR}
</if>
<if test="modifierId != null and modifierId != ''">
AND modifierId = #{modifierId,jdbcType=VARCHAR}
</if>
<if test="modifierName != null and modifierName != ''">
AND modifierName = #{modifierName,jdbcType=VARCHAR}
</if>
<if test="modifyTime != null and modifyTime != ''">
AND modifyTime = #{modifyTime,jdbcType=VARCHAR}
</if>
</sql>
<!-- 查询项目列表 -->
<select id="queryProjectBaseInfoList" resultType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" parameterType="java.util.Map" >
select
<include refid="Base_Column_List"></include>
from lrm_lawyer_abroad
<where>
<include refid="common_where_if"></include>
</where>
order by createTime desc
</select>
<!-- 查询基本详情 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from lrm_lawyer_abroad
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from lrm_lawyer_abroad
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
insert into lrm_lawyer_abroad (lawyerInfoId, lawyerSurname, lawyerName,
lawyerAge, projectName, locationCodePath, projectCountry,
lawyerSex, lawyerPost, languages,
expert, mailBox, Telephone, expatriates,
modifierId, modifierName, modifyTime,
createrId, createrName, createTime,
isDelete)
values (#{lawyerInfoId,jdbcType=VARCHAR}, #{lawyerSurname,jdbcType=VARCHAR}, #{lawyerName,jdbcType=VARCHAR},
#{lawyerAge,jdbcType=VARCHAR}, #{projectName,jdbcType=VARCHAR}, #{locationCodePath,jdbcType=VARCHAR}, #{projectCountry,jdbcType=VARCHAR},
#{lawyerSex,jdbcType=VARCHAR}, #{lawyerPost,jdbcType=VARCHAR}, #{languages,jdbcType=VARCHAR},
#{expert,jdbcType=VARCHAR},#{mailBox,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR},
#{expatriates,jdbcType=VARCHAR},#{modifierId,jdbcType=VARCHAR}, #{modifierName,jdbcType=VARCHAR},
#{modifyTime,jdbcType=TIMESTAMP}, #{createrId,jdbcType=VARCHAR}, #{createrName,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{isDelete,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
insert into lrm_lawyer_abroad
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="lawyerInfoId != null" >
lawyerInfoId,
</if>
<if test="lawyerSurname != null" >
lawyerSurname,
</if>
<if test="lawyerName != null" >
lawyerName,
</if>
<if test="lawyerAge != null" >
lawyerAge,
</if>
<if test="projectName != null" >
projectName,
</if>
<if test="locationCodePath != null" >
locationCodePath,
</if>
<if test="projectCountry != null" >
projectCountry,
</if>
<if test="lawyerSex != null" >
lawyerSex,
</if>
<if test="lawyerPost != null" >
lawyerPost,
</if>
<if test="languages != null" >
languages,
</if>
<if test="expert != null" >
expert,
</if>
<if test="mailBox != null" >
mailBox,
</if>
<if test="telephone != null" >
Telephone,
</if>
<if test="expatriates != null" >
expatriates,
</if>
<if test="modifierId != null" >
modifierId,
</if>
<if test="modifierName != null" >
modifierName,
</if>
<if test="modifyTime != null" >
modifyTime,
</if>
<if test="createrId != null" >
createrId,
</if>
<if test="createrName != null" >
createrName,
</if>
<if test="createTime != null" >
createTime,
</if>
<if test="isDelete != null" >
isDelete,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="lawyerInfoId != null" >
#{lawyerInfoId,jdbcType=VARCHAR},
</if>
<if test="lawyerSurname != null" >
#{lawyerSurname,jdbcType=VARCHAR},
</if>
<if test="lawyerName != null" >
#{lawyerName,jdbcType=VARCHAR},
</if>
<if test="lawyerAge != null" >
#{lawyerAge,jdbcType=VARCHAR},
</if>
<if test="projectName != null" >
#{projectName,jdbcType=VARCHAR},
</if>
<if test="locationCodePath != null" >
#{locationCodePath,jdbcType=VARCHAR},
</if>
<if test="projectCountry != null" >
#{projectCountry,jdbcType=VARCHAR},
</if>
<if test="lawyerSex != null" >
#{lawyerSex,jdbcType=VARCHAR},
</if>
<if test="lawyerPost != null" >
#{lawyerPost,jdbcType=VARCHAR},
</if>
<if test="languages != null" >
#{languages,jdbcType=VARCHAR},
</if>
<if test="expert != null" >
#{expert,jdbcType=VARCHAR},
</if>
<if test="mailBox != null" >
#{mailBox,jdbcType=VARCHAR},
</if>
<if test="telephone != null" >
#{telephone,jdbcType=VARCHAR},
</if>
<if test="expatriates != null" >
#{expatriates,jdbcType=VARCHAR},
</if>
<if test="modifierId != null" >
#{modifierId,jdbcType=VARCHAR},
</if>
<if test="modifierName != null" >
#{modifierName,jdbcType=VARCHAR},
</if>
<if test="modifyTime != null" >
#{modifyTime,jdbcType=TIMESTAMP},
</if>
<if test="createrId != null" >
#{createrId,jdbcType=VARCHAR},
</if>
<if test="createrName != null" >
#{createrName,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="isDelete != null" >
#{isDelete,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
update lrm_lawyer_abroad
<set >
<if test="lawyerSurname != null" >
lawyerSurname = #{lawyerSurname,jdbcType=VARCHAR},
</if>
<if test="lawyerName != null" >
lawyerName = #{lawyerName,jdbcType=VARCHAR},
</if>
<if test="lawyerAge != null" >
lawyerAge = #{lawyerAge,jdbcType=VARCHAR},
</if>
<if test="projectName != null" >
projectName = #{projectName,jdbcType=VARCHAR},
</if>
<if test="locationCodePath != null" >
locationCodePath = #{locationCodePath,jdbcType=VARCHAR},
</if>
<if test="projectCountry != null" >
projectCountry = #{projectCountry,jdbcType=VARCHAR},
</if>
<if test="lawyerSex != null" >
lawyerSex = #{lawyerSex,jdbcType=VARCHAR},
</if>
<if test="lawyerPost != null" >
lawyerPost = #{lawyerPost,jdbcType=VARCHAR},
</if>
<if test="languages != null" >
languages = #{languages,jdbcType=VARCHAR},
</if>
<if test="expatriates != null" >
expatriates = #{expatriates,jdbcType=VARCHAR},
</if>
<if test="mailBox != null" >
mailBox = #{mailBox,jdbcType=VARCHAR},
</if>
<if test="telephone != null" >
Telephone = #{telephone,jdbcType=VARCHAR},
</if>
<if test="expert != null" >
expert = #{expert,jdbcType=VARCHAR},
</if>
<if test="modifierId != null" >
modifierId = #{modifierId,jdbcType=VARCHAR},
</if>
<if test="modifierName != null" >
modifierName = #{modifierName,jdbcType=VARCHAR},
</if>
<if test="modifyTime != null" >
modifyTime = #{modifyTime,jdbcType=TIMESTAMP},
</if>
<if test="createrId != null" >
createrId = #{createrId,jdbcType=VARCHAR},
</if>
<if test="createrName != null" >
createrName = #{createrName,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
createTime = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="isDelete != null" >
isDelete = #{isDelete,jdbcType=INTEGER},
</if>
</set>
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.pcitc.slms.lrm.system.entity.LrmLawyerAbroad" >
update lrm_lawyer_abroad
set lawyerSurname = #{lawyerSurname,jdbcType=VARCHAR},
lawyerName = #{lawyerName,jdbcType=VARCHAR},
lawyerAge = #{lawyerAge,jdbcType=VARCHAR},
projectName = #{projectName,jdbcType=VARCHAR},
locationCodePath = #{locationCodePath,jdbcType=VARCHAR},
projectCountry = #{projectCountry,jdbcType=VARCHAR},
lawyerSex = #{lawyerSex,jdbcType=VARCHAR},
lawyerPost = #{lawyerPost,jdbcType=VARCHAR},
languages = #{languages,jdbcType=VARCHAR},
expert = #{expert,jdbcType=VARCHAR},
mailBox = #{mailBox,jdbcType=VARCHAR},
Telephone = #{telephone,jdbcType=VARCHAR},
expatriates = #{expatriates,jdbcType=VARCHAR},
modifierId = #{modifierId,jdbcType=VARCHAR},
modifierName = #{modifierName,jdbcType=VARCHAR},
modifyTime = #{modifyTime,jdbcType=TIMESTAMP},
createrId = #{createrId,jdbcType=VARCHAR},
createrName = #{createrName,jdbcType=VARCHAR},
createTime = #{createTime,jdbcType=TIMESTAMP},
isDelete = #{isDelete,jdbcType=INTEGER}
where lawyerInfoId = #{lawyerInfoId,jdbcType=VARCHAR}
</update>
</mapper>