插入

插入数据

<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. oracle清理归档日志(缓存)

    1.用RMAN连接目标DB: rman target / RMAN target sys/*****@orcl 2.在RMAN命令窗口中,输入如下命令(清理所有的归档日志): crosscheck a ...

  2. Seurat V3.0

    最新版V3文档:https://satijalab.org/seurat/vignettes.html 不要再用V2的版本了,V3已经涵盖了V2所有的功能. 最新版3.0已经发布了,有重大更新,以前的 ...

  3. JVM 自定义类加载器在复杂类情况下的运行分析

    一.自定义类加载器在复杂类情况下的运行分析 1.使用之前创建的类加载器 public class MyTest16 extends ClassLoader{ private String classN ...

  4. pt-table-checksum解读【转】

    pt-table-checksum是目前可以说是最好的查看主从一致性的工具 先来个使用例子,有助快速上手使用 在主库执行: mysql>GRANT SELECT, PROCESS, SUPER, ...

  5. Github: 提升Github下载速度

    通过修改hosts文件来提速(该方法也可加速其他因为CDN被屏蔽导致访问慢的网站) 第一步:获取GitHub的IP地址 通过访问:http://tool.chinaz.com/dns或者https:/ ...

  6. Nessus提示API Disabled错误

    Nessus提示API Disabled错误   在浏览器中打开Nessus,如果长时间没有操作,就会造成会话过期失效.这时,如果直接访问,就会提示API Disabled错误.这个时候,需要刷新页面 ...

  7. django实战总结

    1.创建app命令 django-admin.py startapp app_name 2.数据库脚本命令 # 1. 创建更改的文件 python manage.py makemigrations # ...

  8. linux传输文件lrzsz

    linux传输文件

  9. 安卓 android studio 报错 Lint found fatal errors while assembling a release target

    报错截图如下: 解决方法:在app的build.gradle中添加如下代码 android{ lintOptions { checkReleaseBuilds false abortOnError f ...

  10. intellij maven配置与使用

    目录 intellij maven配置与使用 Maven 常用设置介绍 Maven 骨架创建 Java Web 项目 Maven 组件来管理项目 @(目录) intellij maven配置与使用 M ...