插入

插入数据

<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. GO语言网络编程

    socket编程 Socket是BSD UNIX的进程通信机制,通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.Socket可以理解为TCP/IP网络的API,它 ...

  2. #C++初学记录(acm试题#预处理)

    C - Lucky 7 in the Pocket BaoBao loves number 7 but hates number 4, so he refers to an integer as a ...

  3. 深度学习面试题24:在每个深度上分别卷积(depthwise卷积)

    目录 举例 单个张量与多个卷积核在深度上分别卷积 参考资料 举例 如下张量x和卷积核K进行depthwise_conv2d卷积 结果为: depthwise_conv2d和conv2d的不同之处在于c ...

  4. JVM 扩展类加载器2

    1.创建Sample public class MyTest22 { static { System.out.println("MyTest22 initializer"); } ...

  5. onenote 每天输入网络密码

    1.问题:只局限 内网 笔记本的弹出输入远程内网服务器用户名密码的情况,每次重启电脑后又会要求输入,否则同步失败 2.解决 控制面板-windows用户-凭据管理器-添加凭据-从上到下一次输入  ip ...

  6. word: 插入或修改文字时后面的字消失 解决办法

    在编辑Word文档中的文字时,我们有时需要插入或修改文字,可是在插入或修改时会发现改动处后面的文字会消失.比如插入或修改3个字,后面的文字随之也会消失3个,这时该怎么办呢? 点击-“文件”-“选项”- ...

  7. mongodb批量update更新数据

    需要先查找出相关的记录,然后循环处理更新数据.如下案例,更新所有status=1的数据的gender值为2 db.getCollection('test').find({"status&qu ...

  8. 绕过WAF、安全狗知识整理

    0x01 前言 目前市场上的WAF主要有以下几类 1. 以安全狗为代表的基于软件WAF 2. 百度加速乐.安全宝等部署在云端的WAF 3. 硬件WAF WAF的检测主要有三个阶段,我画了一张图进行说明 ...

  9. 【转】把sqlite3数据导入到MySQL中

    之前我们默认使用的是SQLite数据库,我们开发完成之后,里面有许多数据.如果我们想转换成Mysql数据库,那我们先得把旧数据从SQLite导出,然后再导入到新的Mysql数据库里去. 1.SQLit ...

  10. UniPush使用指南

    原贴:https://ask.dcloud.net.cn/article/35622 UniPush使用指南 分类:uni-app Push unipush 从HBuilderX 1.7.2起,uni ...