插入

插入数据

<insert id="insertUser2" parameterType="UserEntity">
insert into user values(null,#{userName},#{passWord} ,#{age})
</insert>

批量插入

<insert id="insertForeach" parameterType="java.util.List" >
insert into user values
<foreach item="item" index="index" collection="list" separator="," >
(#{item.id},#{item.userName},#{item.passWord},#{item.age})
</foreach>
</insert>

删除

删除数据

<delete id="delUser" parameterType="int">
DELETE from user where id = #{id};
</delete>

批量删除

<delete id="deleteForeach" parameterType="java.util.List">
delete from user where id in
<foreach item="item" collection="list" open="(" separator="," close=")">#{item}</foreach>
</delete>

更新

更新数据

<update id="updateUser" parameterType="UserEntity">
UPDATE user set username = #{userName},password=#{passWord} where id = #{id}
</update> //选择局部更新
<update id="updateSet" parameterType="UserEntity" >
  update user
  <set>
    <if test="userName!=null">username=#{userName},</if>
    <if test="passWord!=null">password=#{passWord}</if>
  </set>
  where id=#{id}
</update>

批量更新

<update id="updateForeach" parameterType="java.util.List">
<foreach collection="list" item = "item" separator=";">
update user
<set>
<if test="item.userName !=null">username=#{item.userName},</if>
<if test="item.passWord !=null">password=#{item.passWord}</if>
</set>
where id=#{item.id}
</foreach>
</update>

查询

查询全部,模糊查询,相应变量查询

<select id="selectAll" resultType="com.cc.bean.UserEntity">
select <include refid="userSqlAll"></include> from user
</select> <select id="selectLike" parameterType="string" resultType="com.cc.bean.UserEntity">
select <include refid="userSqlAll"></include> from user where username like concat("%",#{string},"%");
</select> <update id="updateUser" parameterType="UserEntity">
UPDATE user set username = #{userName},password=#{passWord} where id = #{id}
</update>

可变变量查询

<select id="selectWhere" parameterType="UserEntity" resultMap="mapUser">
select <include refid="userSqlAll"></include> from user
<where>
<if test="id>0">and id = #{id}</if>
<if test="userName1=null">and username=#{userName}</if>
<if test="passWord!=null">and password=#{passWord}</if>
</where> </select> <select id="selectTrim" parameterType="UserEntity" resultMap="mapUser">
select <include refid="userSqlAll"></include> from user
<trim prefix="where" prefixOverrides="and|or">
<if test="id>0">and id = #{id}</if>
<if test="userName1=null">and username=#{userName}</if>
<if test="passWord!=null">and password=#{passWord}</if>
</trim>
</select> <select id="selectIf" parameterType="UserEntity" resultMap="mapUser">
select <include refid="userSqlAll"></include> from user where 1 = 1
<if test="userName!=null">and username=#{userName}</if>
<if test="passWord!=null">and password=#{passWord}</if>
</select> <select id="selectChoose" parameterType="UserEntity" resultMap="mapUser">
select <include refid="userSqlAll"></include> from user where 1 = 1
<choose>
<when test="userName!=null">and username=#{userName}</when>
<when test="passWord!=null">and password=#{passWord}</when>
<otherwise>and 1 = 1</otherwise>
</choose>
</select>

批量查询

<select id="selectForeachIn" parameterType="java.util.List" resultMap="mapUser">
select <include refid="userSqlAll"></include> from user where id in
<foreach item="item" collection="list" open="(" separator="," close=")">#{item}</foreach>
</select>

注意事项

resultMap进行实体类和数据库表名映射
<resultMap type="UserEntity" id="mapUser">
<id property="id" column="id"></id>
<!-- 非主键 -->
<result property="userName" column="username"></result>
<result property="passWord" column="password"></result>
<result property="age" column="age"></result>
</resultMap> sql标签和include标签简写sql
<sql id="userSqlAll">id,username,password,age</sql>
<include refid="userSqlAll"></include> 每一个标签的id值对应一个dao层的接口方法

使用mybatis完成增删改查的更多相关文章

  1. 学习MyBatis必知必会(5)~了解myBatis的作用域和生命周期并抽取工具类MyBatisUtil、mybatis执行增删改查操作

    一.了解myBatis的作用域和生命周期[错误的使用会导致非常严重的并发问题] (1)SqlSessionFactoryBuilder [ 作用:仅仅是用来创建SqlSessionFactory,作用 ...

  2. MyBatis的增删改查。

    数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改,并且对程序接口做了一些调整,以及对一些问题进行了解答. 1.调整后的结构图: 2.连接数据库文件配置分离: 一般的程序都会把连 ...

  3. MyBatis批量增删改查操作

      前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...

  4. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。

    1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...

  5. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查

    使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...

  6. 从0开始完成SpringBoot+Mybatis实现增删改查

    1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...

  7. Spring Boot入门系列(六)如何整合Mybatis实现增删改查

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  8. Mybatis实例增删改查(二)

    创建实体类: package com.test.mybatis.bean; public class Employee { private Integer id; private String las ...

  9. mybatis的增删改查返回值小析(六)

    本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...

  10. ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)

    ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...

随机推荐

  1. 设计模式-Iterator

    本文参(chao)考(xi)<图解设计模式> 结城浩 (作者) 杨文轩 (译者) 1.Iterator 模式 迭代器作用于集合,是用来遍历集合元素的对象. 迭代器模式提供一种方法顺序访问一 ...

  2. [Linux] RTC 读写指令及测试程序

    CPU:RK3288 系统:Linux IC:hym8563 在 Linux 系统中,指令 date 和 hwclock 都可以读写时间 date:读写系统时间,写时间需要管理员权限 hwclock: ...

  3. 小福bbs-冲刺日志(第五天)

    [小福bbs-冲刺日志(第五天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 UI改进,前端页面改进,后端部分功能测试交予UI 作业的正 ...

  4. 高通平台打开 dynamic debug方法【学习笔记】

    一.首先在内核的配置文件添加如下的配置: CONFIG_DEBUG_FS=y CONFIG_DYNAMIC_DEBUG=y 二.找到自己平台对应的 device/qcom/xxxx/init.targ ...

  5. ubuntu18.04 qemu环境搭建【学习笔记】

    一.准备工具 1.1 安装相关工具 sudo apt-get install qemu libncurses5-dev gcc-arm-linux-gnueabi build-essential 1. ...

  6. android x86 安装

    1.下载页面 http://www.android-x86.org 下载了: android-x86-8.1-r2.iso 用Win32DiskImager制作usb启动盘. 参考: https:// ...

  7. python中的request模块

    本文转自:https://www.cnblogs.com/ydy11/p/8902631.html(版权归属原作者,因觉得写得非常好,故引用) python模块之request模块的理解 reques ...

  8. Windows 操作系统 快捷键

    窗口放大缩小:  + ↑↓ 最小化窗口: ALT + Esc 关闭窗口: Alt + F4 搜索功能:  + 直接输入搜索内容 打开文件管理器:  + E 在文件管理器中切换: Tab

  9. 【转载】 180623 Conda install 本地压缩包文件tar.bz2

    原文地址“ https://blog.csdn.net/qq_33039859/article/details/80785535 ----------------------------------- ...

  10. Python“文件操作”Excel篇(上)

    大家好,我们今天来一起探索一下用Python怎么操作Excel文件.与word文件的操作库python-docx类似,Python也有专门的库为Excel文件的操作提供支持,这些库包括xlrd.xlw ...