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

使用sql

<if test="_parameter != null">
<include refid="sqlID" />
</if>

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

<update id="ID" parameterType="map">
update AccountDetail
set ID = #{record.id,jdbcType=INTEGER},
CompanyId = #{record.companyid,jdbcType=INTEGER},
UserId = #{record.userid,jdbcType=INTEGER},
TradeName = #{record.tradename,jdbcType=VARCHAR},
.....
</update>

<update id="updateByPrimaryKeySelective"

parameterType="com.transfar.busi.entity.ecallcenter.Accountdetail">
update AccountDetail
<set>
<if test="companyid != null">
CompanyId = #{companyid,jdbcType=INTEGER},
</if>
<if test="userid != null">
UserId = #{userid,jdbcType=INTEGER},
</if>
....
</set>
where ID = #{id,jdbcType=INTEGER}
</update>

4.SELECT

一些简单的用法 

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

 -- 原字段 “STR”

Select  ‘STR’;  

-- TRIM 去前后空白 “STR”

Select  TRIM(‘STR’);

--  LTRIM 去前空白 “STR”

Select  LTRIM(‘STR’);

--  RTRIM 去后空白 “STR”

Select  RTRIM(‘STR’);

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>


mybatis 批量插入数据
 <insert id="insertByBatch" parameterType="java.util.List">
        insert into BPM_D_DATA_INDEX (THE_MONTH, BU_CODE, PHASE_CODE,
        PHASE_NM, AREA_CODE, AREA_NAME,DATA_LEVEL, INDEX_CODE, INDEX_NAME,
MONTH_VALUE,OP_TIME,EXECUTION_ID,LOAD_DATE,BU, DATEKEY,INSERT_DATA_TIME)
        (<foreach collection="list" index="" item="item" separator="union all">
        SELECT
        #{item.theMonth,jdbcType=VARCHAR}, #{item.buCode,jdbcType=VARCHAR}, #{item.phaseCode,jdbcType=VARCHAR},
        #{item.phaseNm,jdbcType=VARCHAR}, #{item.areaCode,jdbcType=VARCHAR}, #{item.areaName,jdbcType=VARCHAR},
        #{item.dataLevel,jdbcType=VARCHAR}, #{item.indexCode,jdbcType=VARCHAR}, #{item.indexName,jdbcType=VARCHAR},
        #{item.monthValue,jdbcType=DECIMAL}, #{item.opTime,jdbcType=VARCHAR}, #{item.executionId,jdbcType=VARCHAR},
        #{item.loadDate,jdbcType=VARCHAR}, #{item.bu,jdbcType=VARCHAR}, #{item.datekey,jdbcType=VARCHAR},
        #{item.insertDataTime,jdbcType=TIMESTAMP}
        FROM DUAL
    </foreach>)
    </insert>
----------------------------------
mybatis 默认插入当前时间
<insert id="importSickTree" parameterType="list" >
    insert into tb_sick_tree (treeCode,projectID,
    lat, lon, distCode, distName,
    identifyTime,processed,year)
    VALUES
    <foreach collection="list" item="sickTree" separator=",">
        ( #{sickTree.treecode,jdbcType=VARCHAR},#{sickTree.projectid,jdbcType=INTEGER},
        #{sickTree.lat,jdbcType=DOUBLE}, #{sickTree.lon,jdbcType=DOUBLE},
        #{sickTree.distcode,jdbcType=VARCHAR}, #{sickTree.distname,jdbcType=VARCHAR},
        #{sickTree.identifytime,jdbcType=TIMESTAMP},

#{sickTree.processed,jdbcType=INTEGER},SUBSTR(NOW(),1,4) )
    </foreach>
</insert>

关于优化

1. 

........

sorta

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值