使用mybatis完成增删改查
插入
插入数据
<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完成增删改查的更多相关文章
- 学习MyBatis必知必会(5)~了解myBatis的作用域和生命周期并抽取工具类MyBatisUtil、mybatis执行增删改查操作
一.了解myBatis的作用域和生命周期[错误的使用会导致非常严重的并发问题] (1)SqlSessionFactoryBuilder [ 作用:仅仅是用来创建SqlSessionFactory,作用 ...
- MyBatis的增删改查。
数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改,并且对程序接口做了一些调整,以及对一些问题进行了解答. 1.调整后的结构图: 2.连接数据库文件配置分离: 一般的程序都会把连 ...
- MyBatis批量增删改查操作
前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。
1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查
使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...
- 从0开始完成SpringBoot+Mybatis实现增删改查
1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...
- Spring Boot入门系列(六)如何整合Mybatis实现增删改查
前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...
- Mybatis实例增删改查(二)
创建实体类: package com.test.mybatis.bean; public class Employee { private Integer id; private String las ...
- mybatis的增删改查返回值小析(六)
本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
随机推荐
- ZR#1009
ZR#1009 解法: 因为无敌的SR给了一个大暴力算法,所以通过打表发现了了一些神奇的性质,即第一行和第一列的对应位置数值相等. 我们可以通过手算得出 $ F(n) = \frac{n(n + 1) ...
- 急急急,tp5的验证码不显示
本地环境phpstudy,使用composer安装tp5,按照看云<ThinkPHP5.0完全开发手册>验证码配置,就是不显示验证码. 使用:<div>{:captcha_im ...
- 【转】JVM类装载机制的解析,热更新的探讨
引言 如有错误,请批评指正. Java是一种动态连接的语言.所谓动态连接,大概可以这么解释. 首先,Java可以大概想象成是编译解释执行的.对于一个*.java的文件,通过javac将会编译成一个*. ...
- C# 复制数组容易踩到的坑--引用类型与值类型
原文链接:https://my.oschina.net/u/3744313/blog/1794235 笔者近期做的项目里大量使用了数组,而在使用过程中,笔者曾经遇到了一个比较低级的问题:如何将一个数组 ...
- Linux命令:ipcs/ipcrm命令
ipcs/ipcrm命令 是linux/uinx上提供关于一些进程间通信方式的信息,包括共享内存,消息队列,信号 多进程间通信常用的技术手段包括共享内存.消息队列.信号量等等,Linux系统下自带的 ...
- windows中kill端口为8080的进程(或子进程)
1.netstat -aon|findstr "8080" 查出8080端口被9436进程占用 2.查看PID对应的进程 tasklist|findstr "9436&q ...
- [转]Windows系统下批量重命名文件(bat命令版本)
原文地址:https://jingyan.baidu.com/article/6dad507524bdcba122e36e44.html 我们有时候会遇到大量文件需要重命名,Windows系统下右键菜 ...
- Android studio 运行打包 Ionic 项目
1.创建项目 ionic start myapp tabs 2.cd 到项目文件夹中 3.ionic cordova platfrom add android 执行这个命令后建议修改一下应用包名称,参 ...
- jmeter中特殊的时间处理方式
需求: 1.获取当前时间的年月日时分秒毫秒 2.生成上一个月的随机某天的一个时间 3.生成一个年月日时分秒毫秒的一个时间戳 1.__time : 获取时间戳.格式化时间 ${__time(yyyy-M ...
- 基于C#在WPF中使用斑马打印机进行打印【转】——不支持XPS的打印机
https://www.cnblogs.com/zhaobl/p/4666002.html