Mybatis中常用的SQL
1.BaseResultMap
<resultMap id="BaseResultMap" type="com.stylefeng.guns.common.persistence.model.LoginTest">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="password" property="password" />
</resultMap>
2.SQL
<sql id="Base_Column_List">
id, name, password
</sql>
3.确切的Select
<select id="selectUser" resultMap="BaseResultMap" parameterType="String">
SELECT <include refid="Base_Column_List" /> FROM login_test
<where>
<if test="name != ''">
name=#{name}
</if>
</where>
</select>
4.模糊的Select
<select id="selectUsers" resultMap="BaseResultMap" parameterType="String">
SELECT <include refid="Base_Column_List" /> FROM login_test
<where>
<if test="name != ''">
name like '%#{name}%'
</if>
</where>
</select>
具体可参考:SQL 模糊查询
5.批量的Select(可用于数据库表的批量导出)
<select id="selectBySomeid" parameterType="list" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM login_test WHERE id in
<foreach collection="Idlist" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
6.有选择性的update
<update id="updateByPrimaryKeySelective" parameterType="com.mall.pojo.LoginTest">
update login_test
<set>
<if test="name != null">
username = #{username},
</if>
<if test="password != null">
password = #{password},
</if>
</set>
where id = #{id}
</update>
7.无选择性的uptate
<update id="updateByPrimaryKey" parameterType="com.mall.pojo.LoginTest">
update login_test
set name = #{username},
password = #{password},
where id = #{id}
</update>
8.单个delete
<delete id="deleteByid" parameterType="Integer">
DELETE FROM login_test
WHERE id =#{id}
</delete>
9.批量delete
<delete id="deleteByid" parameterType="list">
DELETE FROM login_test WHERE id in
<foreach collection="Idlist" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
10.有选择性的单个insert
<insert id="insertSelective" parameterType="com.mall.pojo.LoginTest">
insert into login_test
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="password != null">
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id},
</if>
<if test="username != null">
#{name},
</if>
<if test="password != null">
#{password},
</if>
</trim>
</insert>
具体可参考:mybatis之特殊标签的使用
11.无选择性的单个insert
<insert id="insert" parameterType="com.mall.pojo.LoginTest">
insert into mmall_user (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
12.批量插入
<insert id="batchInsert" parameterType="list">
insert into mmall_order_item (id, name, password)
values
<foreach collection="List" item="item" index="index" separator=",">
(
#{item.id},#{item.name},#{item.password} )
</foreach>
</insert>
13.多表更新
<update id="updateObjectVersion" parameterType="com.huhu.Dto">
UPDATE ${dataCode} set OBJECT_VERSION_NUMBER=#{objectVersionNumber}
<where>
<if test="codeId != null">
CODE_ID = #{codeId}
</if>
<if test="codeValueId != null">
AND CODE_VALUE_ID = #{codeValueId}
</if>
<if test="productId != null">
AND PRODUCT_ID = #{productId}
</if>
<if test="propertyId != null">
AND PROPERTY_ID = #{propertyId}
</if>
<if test="cmdId != null">
AND CMD_ID = #{cmdId}
</if>
<if test="paramId != null">
AND PARAM_ID = #{paramId}
</if>
<if test="templateId != null">
AND title = #{templateId}
</if>
</where>
</update>
不要难为自己,常用的就记录下来
-------记录点:白银
----------------------------------------------------------------------------------------------------------------------------------
数据库中类型是datetime,在mybatis中是

自定义插入某个表:
<update id="updateObjectVersion" parameterType="com.xx.xx">
UPDATE ${dataCode} set OBJECT_VERSION_NUMBER=#{objectVersionNumber}
<where>
<if test="codeId != null">
CODE_ID = #{codeId}
</if>
<if test="codeValueId != null">
AND CODE_VALUE_ID = #{codeValueId}
</if>
<if test="productId != null">
AND PRODUCT_ID = #{productId}
</if>
<if test="propertyId != null">
AND PROPERTY_ID = #{propertyId}
</if>
<if test="cmdId != null">
AND CMD_ID = #{cmdId}
</if>
<if test="paramId != null">
AND PARAM_ID = #{paramId}
</if>
<if test="templateId != null">
AND TEMPLATE_ID = #{templateId}
</if>
</where>
</update>
Mybatis中常用的SQL的更多相关文章
- SQL Server中常用的SQL语句(转):
SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- 面试、笔试中常用的SQL语句(数据库知识必杀)一共50个!!!
Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 ...
- 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)
动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...
- mysql 中常用的 sql 语句
SQL分类: DDL-----数据定义语言(CREATE--创建,ALTER--修改. DROP--删除表,DECLARE--声明) DML-----数据定义语言(SELECT--查询,DELECT- ...
- 编码中常用的SQL语法
蓝色标注的都是比较常见的SQL ====================== 开发中常见的SQL: left join , right join 防止丢弃数据 inner join CASE WHNE ...
- SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 简单概述一下本讲 别名,sql片段简单写一下,模糊查询多写一点 一.别名 <typeAliases> ...
- 工作中常用的sql语句以及知识整理
一.常用的sql语句 1.建表语句 create table tabname(colname1 type1 [not null][primary key], colname2 type2,...) 根 ...
随机推荐
- 【转】JAVA处理线程超时
在实际业务中,由其是多线程并开业务中,经常会遇到某个线程执行超时.而程序如果不捕获这类情况,就会导致程序一直处于等待状态,从而影响后续线程的运行.比如说网络通迅.单任务下的复杂数据库查询等,通常处理这 ...
- mac下通过mdfind命令搜索文件
mdfind命令就是Spotlight功能的终端界面,这意味着如果Spotlight被禁用,mdfind命令也将无法工作.mdfind命令非常迅速.高效.最基本的使用方法是: mdfind -name ...
- iOS 面试题、知识点 之一
最近面试,发现这些题个人遇到的几率大一些,与大家分享一下,分三文给大家: 当然Xcode新版本与之前一版本的区别,以及iOS新特性是必要了解的吧. Xcode8 和iOS 10 在之前文章有发过,感兴 ...
- mysql 先分组在排序
mysql语句的语法模板: select distinct <select_list> from <left_table><join_type> join < ...
- Office 365实现单点登录系列(1)—域环境搭建
Hello 小伙伴们, 2018新年快乐,作为2018年首篇文章,怎么能不给大家带来点干货呢?这篇文章其实我9月底的时候已经在MSDN上发布过了,为表诚意,我更新了这篇文章,并把它组成了一个系列,2. ...
- ffmpeg常用命令---转
1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpeg -i input_file -acod ...
- 4.Nginx的URL重写应用
Nginx的URL重写应用 nginx的URL重写模块是用得比较多的模块之一,所以我们需要好好地掌握运用.常用的URL重写模块命令有if,rewrite,set,break等. if命令 if用于判断 ...
- Amazon email system中使用的字体
- 几种加密算法的java实现包括MD5、RSA、SHA256
SHA加密: package com; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException ...
- SSH KEY 批量分发
代码 #!/bin/sh . /etc/init.d/functions ];then echo "sh $0 arg0" exit fi for ip in 172.23.216 ...