SQL 语句规范

一、基本T-SQL 语句

程序中一般使用的基本sql语句

模式

Inset

Insert  into  表名  (列名1,列名2,列名3,...) values   (值1,值2,值3,...)

delete

Delete from where 列名1 = 值1 and 列名2 = 值2 and 列名3 = 值3...

update

Update 表名  set  列名1 = 值1 ,列名2 = 值2 ,列名3 = 值3…   

Select

Select 列名1,列名2,列名3...  from 表名  where 列名1 = 值1 and 列名2 = 值2 and 列名3 = 值3...

示例:以mybatis为例。

<?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](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fmybatis.org%2Fdtd%2Fmybatis-3-mapper.dtd)"> <mapper namespace="com.apps.sys.mapper.ArticleMapper"> <sql id="tableName">

tb_Article

</sql> <sql id="keyId">

id

</sql> <sql id="Columns">

type_id,user_id,sitemodules_id,status,bigTile,smallTitle,content,source,author,publishTime

</sql> <sql id="selectColumns">

<if test="id !=null">and id=#{id} </if>

<if test="typeId !=null">and type_id=#{typeId} </if>

<if test="userId !=null">and user_id=#{userId} </if>

<if test="sitemodulesId !=null">and sitemodules_id=#{sitemodulesId} </if>

<if test="bigTile !=null">and bigTile=#{bigTile} </if>

<if test="smallTitle !=null">and smallTitle=#{smallTitle} </if>

<if test="author !=null">and author=#{author} </if>

<if test="publishTime !=null">and publishTime=#{publishTime} </if>

<if test="source !=null">and source=#{source} </if>

<if test="content !=null">and content=#{content} </if>

</sql> <sql id="updateColumns">

<if test="typeId !=null">, type_id=#{typeId} </if>

<if test="userId !=null">, user_id=#{userId} </if>

<if test="sitemodulesId !=null">, sitemodules_id=#{sitemodulesId} </if>

<if test="bigTile !=null">, bigTile=#{bigTile} </if>

<if test="smallTitle !=null">, smallTitle=#{smallTitle} </if>

<if test="author !=null">, author=#{author} </if>

<if test="publishTime !=null">, publishTime=#{publishTime} </if>

<if test="source !=null">, source=#{source} </if>

<if test="content !=null">, content=#{content} </if>

</sql> <insert id="insertOne" parameterType="Article" useGeneratedKeys="true" keyProperty="id">

Insert

 <include refid="tableName"/> (<include refid="Columns"/>)

values (#{typeId},#{userId},#{sitemodulesId},#{bigTile},#{smallTitle},#{content},#{source},#{author},#{publishTime})

</insert> <delete id="deleteOne" parameterType="int">

delete from

<include refid="tableName"/>

where id=#{id}

</delete> <update id="updateByColumns" parameterType="Article">

Update

 <include refid="tableName"/>

 set id=#{id}

<include refid="updateColumns"/>

 where id=#{id}

</update> <select id="selectOneById" parameterType="int" parameterType="Article">

select

<include refid="keyId" />,<include refid="Columns"/>

 from <include refid="tableName"/> where id=#{id}

</select> <select id="listPageAll" resultMap="ArticleResultMap">

select * from <include refid="tableName"/>

</select> <select id="listPageByColumns" parameterType="Article" resultMap="ArticleResultMap">

select

<include refid="keyId" />,<include refid="Columns"/>

 from

<include refid="tableName"/>

  where 1=1

<include refid="selectColumns"/> </select> <select id="selectByColumns" parameterType="Article" resultMap="ArticleResultMap">

select

<include refid="keyId" />,

<include refid="Columns"/>

 from

<include refid="tableName"/>

 where 1=1

<include refid="selectColumns"/> </select> </mapper>

二、查询的方法

public Integer insertOne(Role role);

根据字段是否为空添加相应的字段

public Integer deleteOneById(String Id);

根据id删除一条记录

public Integer updateOne(Role role);

根据字段是否为空更新相应的字段

public Role selectOneById(String id);

根据id查询一条记录

public List<Role> selectListByObj(Role role);

根据对象单表查询出多条记录

public List<Role> selectListrefLJByObj(Role role);

根据对象左链接查询出多条记录

public List<Role> selectListrefRJByObj(Role role);

根据对象右链接查询出多条记录

public List<Role> selectListrefFJByObj(Role role);

根据对象全链接查询出多条记录

public List<Role> listPageByObj(Role role);

根据对象单表分页查询出多条记录

public List<Role> listPageRefLJByObj(Role role);

根据对象左链接分页查询出多条记录

public List<Role> listPageRefRJByObj(Role role);

根据对象右链接分页查询出多条记录

public List<Role> listPageRefFJByObj(Role role);

根据对象全链接分页查询出多条记录

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''

MySQL学习——操作表

MySQL学习——操作表摘要:本文主要学习了使用DDL语句操作表的方法。创建表语法1createtable表名表定义选项表选项;表定义选项用来创建定义表的结构,由列名(col\_name)、列的定义(column\_definition)以及可能的空值说明、完

SQL 语句规范 - HelloWorld