数据库
首页 > 数据库> > sql 书写 规范 优化

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......dao (mapper).....">

  <resultMap id="BaseResultMap" type="com.........entity....">
    <id column="ID" jdbcType="INTEGER" property="id" />
    <result column="CompanyId" jdbcType="INTEGER" property="companyid" />

    .......

  </resultMap>

</mapper>

<sql id="ID">

  sql片段(多次使用的sql 语句 减少多次重复)个人认为

</sql>

1.INESRT 

  a.一般的添加

<insert id="...." parameterType="com.....entity.....">
  insert into 表名(ID, CompanyId, UserId,
  ......
  )
  values (#{id,jdbcType=INTEGER}, #{companyid,jdbcType=INTEGER}, #{userid,jdbcType=INTEGER},
  ......
  )    // jdbcType=INTEGER    jdbcType=VARCHAR  jdbcType=TIMESTAMP  jdbcType=DOUBLE
</insert>

   b.批量添加

  <insert id="ID" parameterType="com............entity...............(实体类全名)">
  insert into 表名
  <trim prefix="(" suffix=")" suffixOverrides=",">
  <if test="id != null">
  ID,
  </if>
  <if test="companyid != null">
  CompanyId,
  </if>
  <if test="userid != null">
  UserId,
  </if>
  <if test="tradename != null">
  TradeName,
  </if>
  ......
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
  <if test="id != null">
  #{id,jdbcType=INTEGER},
  </if>
  <if test="companyid != null">
  #{companyid,jdbcType=INTEGER},
  </if>
  <if test="userid != null">
  #{userid,jdbcType=INTEGER},
  </if>
  <if test="tradename != null">
  #{tradename,jdbcType=VARCHAR},
  </if>
  ...................
  </if>
  </trim>
  </insert>

2.DELETE  

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from 表名
    where ID = #{id,jdbcType=INTEGER}
  </delete>

3.UPDATE

  

4.SELECT

 

 

一些简单的用法 

  1.  trim   select trim ("           a            ") 结果 a  去除 前后空

  2.  

 

 

 

 

 

<!-- 查询条件 方法名 -->
<sql id="Example_Where_Clause" >
<!-- 条件 -->
<where >
<!-- 条件集合 -->
<!-- 传入集合类型是oredCriteria 循环变量是criteria 关系是或者-->
<foreach collection="oredCriteria" item="criteria" separator="or" >
<!-- 如果传入的条件有效 单词的意思标准(相当于条件).有效 -->
<if test="criteria.valid" >
<!-- <修剪前缀= "("后缀= ")"前缀覆盖 = "和" > -->
<trim prefix="(" suffix=")" prefixOverrides="and" >
<!-- 条件集合 -->
<!-- 集合类型是oredCriteria 循环变量是criteria -->
<foreach collection="criteria.criteria" item="criterion" >
<!-- 通过判断选择查询条件条件-->
<choose >
<!-- 通过判断标准来执行对应的查询条件 -->
<!-- 当条件不符合标准的时候 -->
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<!-- 条件是一个值的时候 调用此条件 相当与id查询 name查询-->
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<!-- 条件是两个值的时候 调用此条件 -->
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<!-- 如果条件是一个集合的话调用此条件 -->
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<!-- 批量更新 -->
<sql id="Update_By_Example_Where_Clause" >
<where >
<!-- 传入集合类型是example.oredCriteria 循环变量是criteria -->
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<!-- 结构是条件判断如果满足就追加 -->
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<!-- 表中的字段值 -->
<sql id="Base_Column_List" >
id, name, price, color, cima, imgPath
</sql>
<!-- 查询的方法 -->
<!-- 方法名 返回值类型 参数类型 -->
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.baidu.bean.GGoodExample" >
<!-- 查询 -->
select
<!-- 条件满足 这个字段在exa实体类中 是一个布尔值 -->
<if test="distinct" >
distinct
</if>
<!-- 正常查询 -->
<include refid="Base_Column_List" />
from g_good
<!-- 如果参数列表不为空 调用下面的方法 这个方法返回的是参数列表 也可以是条件查询-->
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<!-- 如果有分组字段 根据该字段进行分组查询 -->
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
<!-- 这个是分页查询要在exa实体类中添加两个字段 offset limit -->
<if test="offset != null && limit != null">
limit ${offset},${limit}
</if>
</select>
<!-- 通过id查询 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<!-- 要查询的字段 -->
<include refid="Base_Column_List" />
from g_good
where id = #{id,jdbcType=INTEGER}
</select>
<!-- 通过id删除 -->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from g_good
where id = #{id,jdbcType=INTEGER}
</delete>
<!-- 条件删除 -->
<delete id="deleteByExample" parameterType="com.baidu.bean.GGoodExample" >
delete from g_good
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<!-- 插入数据 -->
<insert id="insert" parameterType="com.baidu.bean.GGood" >
insert into g_good (id, name, price,
color, cima, imgPath
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=INTEGER},
#{color,jdbcType=VARCHAR}, #{cima,jdbcType=VARCHAR}, #{imgpath,jdbcType=VARCHAR}
)
</insert>
<!-- 插入方法自动判断有没有参数 没有参数也不会报错 -->
<insert id="insertSelective" parameterType="com.baidu.bean.GGood" >
insert into g_good
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="price != null" >
price,
</if>
<if test="color != null" >
color,
</if>
<if test="cima != null" >
cima,
</if>
<if test="imgpath != null" >
imgPath,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=INTEGER},
</if>
<if test="color != null" >
#{color,jdbcType=VARCHAR},
</if>
<if test="cima != null" >
#{cima,jdbcType=VARCHAR},
</if>
<if test="imgpath != null" >
#{imgpath,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!-- 查询表中的记录数 -->
<select id="countByExample" parameterType="com.baidu.bean.GGoodExample" resultType="java.lang.Integer" >
select count(*) from g_good
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<!-- 普通修改 -->
<update id="updateByExampleSelective" parameterType="map" >
update g_good
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.name != null" >
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.price != null" >
price = #{record.price,jdbcType=INTEGER},
</if>
<if test="record.color != null" >
color = #{record.color,jdbcType=VARCHAR},
</if>
<if test="record.cima != null" >
cima = #{record.cima,jdbcType=VARCHAR},
</if>
<if test="record.imgpath != null" >
imgPath = #{record.imgpath,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<!-- 高级修改 带条件修改 -->
<update id="updateByExample" parameterType="map" >
update g_good
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
price = #{record.price,jdbcType=INTEGER},
color = #{record.color,jdbcType=VARCHAR},
cima = #{record.cima,jdbcType=VARCHAR},
imgPath = #{record.imgpath,jdbcType=VARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<!-- 通过id修改 允许非空修改 -->
<update id="updateByPrimaryKeySelective" parameterType="com.baidu.bean.GGood" >
update g_good
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=INTEGER},
</if>
<if test="color != null" >
color = #{color,jdbcType=VARCHAR},
</if>
<if test="cima != null" >
cima = #{cima,jdbcType=VARCHAR},
</if>
<if test="imgpath != null" >
imgPath = #{imgpath,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<!-- 通过id修改不允许有空值 -->
<update id="updateByPrimaryKey" parameterType="com.baidu.bean.GGood" >
update g_good
set name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=INTEGER},
color = #{color,jdbcType=VARCHAR},
cima = #{cima,jdbcType=VARCHAR},
imgPath = #{imgpath,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

标签:VARCHAR,color,书写,sql,criterion,jdbcType,INTEGER,优化,id
来源: https://www.cnblogs.com/gzl180110/p/10557504.html