Mybatis中常用的SQL
2024-09-29 15:57:53
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>
最新文章
- 用Python生成组织机构代码,附源码
- vim用法小节
- oracle分层查询中的start with和connect by(树结构查询)
- Java [Leetcode 263]Ugly Number
- HTML之Data URL(转)
- Lua 数据库访问(转)
- openstack controller ha测试环境搭建记录(十五)——创建实例
- ●POJ 2007 Scrambled Polygon
- Python并发编程之实战异步IO框架:asyncio 下篇(十一)
- 20175213 2018-2019-2 《Java程序设计》第4周学习总结
- 网络赛 I题 Max answer 单调栈+线段树
- Ubuntu 18.04搭建Git服务器
- House of Spirit学习调试验证与实践
- limit小结
- hdwiki5.1 词条参考资料都是相同的问题解决
- fread/fwrite
- Hive 的查询结果直接导入到 MySQL 中的方法
- Appium(Python)测试混血App
- Android图片加载框架之Picasso
- virtualbox虚拟机ubuntu操作系统,设置网络互通、访问,能访问虚拟机swoole的http服务
热门文章
- Jenkins in OpenCASCADE
- CentOS 7 学习(一) 配置LAMP和Nginx
- 解决model 里 NSInteger类型
- Android破解学习之路(五)——Android游戏 割绳子:魔法 + 在游戏加入Toast弹窗提示
- JavaScript构造函数、继承的理解
- python中的virtualenv是干嘛的?
- 一、源代码-面向CLR的编译器-托管模块-(元数据&;IL代码)
- SQL Server(解决问题)已成功与服务器建立连接,但是在登录过程中发生错误。(provider: Shared Memory Provider, error:0 - 管道的另一端上无任何进程。
- asp.net core webapi 服务端配置跨域
- lambda 与内置函数,以及一些补充