Mybatis学习日志(二)
学习了动态SQL
where+if;choose+when+otherwise;updata+set+if+where

<select id="getCustomerif" resultType="com.swpu.domain.Customer" parameterType="com.swpu.domain.Customer"> select * from customer <where> <if test=" name!= null and name != '' "> name like concat('%',#{name},'%') </if> <if test=" job != null and job != '' "> and job like concat('%',#{job},'%') </if> </where> </select>

<select id="getCustomerchoose" resultType="com.swpu.domain.Customer" parameterType="com.swpu.domain.Customer"> select * from customer <choose> <when test="name!= null and name != '' "> name like concat('%',#{name},'%') </when> <when test=" job != null and job != '' "> and job like concat('%',#{job},'%') </when> <otherwise> where phone is not null and phone!='' </otherwise> </choose> </select>
需要注意:在select语句末或者otherwise语句头要加where 否则报错SQL语句错误。

<update id="setCustomer" parameterType="com.swpu.dao.CustomerDao"> update customer <set> <if test="name!= null and name != '' "> name = #{name} </if> <if test=" job != null and job != '' "> , job = #{job} </if> </set> where id=#{id} </update>
需要注意:多个if语句后续不能用and,只能用',' ,否则会报错:Truncated incorrect DOUBLE value【and将字符串视为double类型处理了】
foreach示例:

<select id="getCustomerforeach" resultType="com.swpu.domain.Customer"> select * from customer <where> <foreach collection="list" item="id" open="id in(" separator="," close=")"> #{id} </foreach> </where> </select>

<insert id="insertCustomers" parameterType="com.swpu.domain.Customer"> insert customer values <foreach collection="list" item="Customer" separator=","> (null,#{Customer.name},#{Customer.job},#{Customer.phone},#{Customer.age}) </foreach> </insert>

<delete id="deleteCustomers"> delete from customer <where> <foreach collection="list" item="id" open="id in(" separator="," close=")"> #{id} </foreach> </where> </delete>
以防万一,这里把普通的CUDR语句也写上了

<!-- 1--> <select id="getAllUsers" resultType="com.swpu.domain.User"> select * from user </select> <!-- 2--> <select id="getUserById" resultType="com.swpu.domain.User" parameterType="int"> select * from user where id=#{id} </select> <!-- 3--> <select id="getUsersByName" resultType="com.swpu.domain.User">-- parameterType="String"> select * from user where name like concat("%",#{name},"%") </select> <!-- 4--> <insert id="InsertUser" parameterType="com.swpu.domain.User"> insert into user(name,sex,address) values (#{name}, #{sex},#{address}) </insert> <!-- 5--> <update id="updateUser" parameterType="com.swpu.domain.User"> update user set name=#{user.name},sex=#{user.sex},address=#{user.address} where id=#{id} </update> <!-- 6--> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete>
【Over】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!