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,...) 根 ...
随机推荐
- MPSOC之8——启动及错误处理
有了BOOT.BIN(fsbl+pmu+atl+uboot).uImage.uramdisk.image.gz,dtb文件,就可以启动了.把上述文件统统拷贝到SD卡,并设置开发板为SD卡启动. 0. ...
- MPSOC之5——开发流程BOOT.BIN
需要把若干文件打成大包,烧写到flash或者sd卡中,才能启动运行. 1.petalinux打包 petalinux-packet打包时,需要petalinux的工程,限制太死了,不用. 2 wind ...
- 查看SQL Server当前会话的隔离级别
查看SQL Server当前会话的隔离级别 DBCC USEROPTIONS
- 带你深度解析Maven
一.What`s Maven? Maven是基于项目对象模型(POM project object model),可以通过一小段描述信息(配置)来管理项目的构建,报告和文档的软件项目管理工具,简单的说 ...
- getchar() 、 scanf() 、流与缓冲区
C中的缓冲区一直是debug的重灾区,今天在写一个命令行界面的时候又遇到了这个问题,所以来总结一波. 两函数的不同之处 scanf() 会把 stdinBuff 中的特定格式数据取出,非特定格式数据则 ...
- 查看windows、linux的SN
gwmi win32_bios [root@live-al-ops-pxe-2 ~]# dmidecode | grep Number | sed -n '1p' Serial Number: ...
- [数据结构]C语言链表实现
我学数据结构的时候也是感觉很困难,当我学完后我发现了之所以困难时因为我没有系统的进行学习,而且很多教授都只是注重数据结构思想,而忽略了代码方面,为此我写了这些博文给那些试图自学数据结构的朋友,希望你们 ...
- Linux中dos2unix批量转换
有时候遇到多层目录下的文件格式需要转换,dos2unix 没有-r之类的递归指令,所以需要与find还有管道结合. find -type f | xargs dos2unix -o
- touchstart和touchend事件
touchstart和touchend事件 移动互联网是未来的发展趋势,现在国内很多互联网大佬都在争取移动这一块大饼,如微信及支付宝是目前比较成功的例子,当然还有各种APP和web运用. 由于公司的需 ...
- 【读书笔记】《Effective Java》——目录
第二章——创建和销毁对象 第1条:考虑用静态工厂方法替代构造器 第2条:遇到多个构造器参数时要考虑用构建器 第3条:用私有构造器或者枚举类型强化Singleton属性 第4条:通过私有构造器强化不可实 ...