namespace="com.dao.StudentDao"

1.基本增删改查:

(1)增加:

<insert id="insertStudent" parameterType="student" keyProperty="id" useGeneratedKeys="true">

insert into student(age,name) values(#{age},#{name})

</insert>

(2)删除:

<delete id="delStudent" parameterType="int">
delete from student where id=#{id}
</delete>

(3)修改:

<update id="updateStudent3" parameterType="student">
update student set name=#{name},age=#{age} where id=#{id}
</update>

(4)查询:

<select id="selStudent" resultType="student">
select id,age,name from student
</select>

2.条件操作:

(1)动态SQL引用:

<sql id="condition">
<where>
<if test="id!=null and id!=0">
and id=#{id}
</if>
<if test="name!=null and name!=''">
and name like #{name}
</if>
<if test="age!=null and age!=0">
age=#{age}
</if>
</where>
</sql>

<select id="selStudent3" resultType="student" parameterType="student">
select id,name,age from student <include refid="condition"/>
</select>

(2).trim动态条件查询 (注意:前缀后缀)

<select id="selStudent4" resultType="student" parameterType="student">
select id,name,age from student
<trim prefix="where" prefixOverrides="and |or">
<if test="id!=null and id!=0">
id=#{id}
</if>
<if test="age!=null and age!=0">
age=#{age}
</if>
<if test="name!=null and name!=''">
name=#{name}
</if>
</trim>
</select>

(3)foreach子句 (IN)

<select id="selStudent5" resultType="student">
select id,name,age from student where id in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>

(4)set动态条件修改

<update id="updateStudent" parameterType="student">
update student
<set>
<if test="name!=null and name!=''">name=#{name},</if>
<if test="age!=null and age!=0">age=#{age},</if>
</set>
where id=#{id}
</update>

(5)trim动态条件修改

<update id="updateStudent2" parameterType="student">
update student
<trim prefix="set" suffixOverrides=",">
<if test="name!=null and name!=''">name=#{name},</if>
<if test="age!=null and age!=0">age=#{age},</if>
</trim>
where id=#{id}
</update>

两种调用方式:

String resource="mybatis-config.xml";

InputStream is = Resources.getResourceAsStream(resource);

factory = new SqlSessionFactoryBuilder().build(is);

SqlSession session = factory.openSession();

//第一种

//List<Student> list = session.selectList("com.dao.StudentDao.selStudent");

//第二种(注意:与StudentDao接口方法对应起来)

StudentDao sd = session.getMapper(StudentDao.class);

MyBatis的基本增删改查及条件操作及main方法调用相关推荐

  1. Mybatis基础:增删改查、模糊查询、多条件查询

    Mybatis基础:增删改查.模糊查询.多条件查询http://www.bieryun.com/3132.html 1.新建测试数据库,根据实体类属性创建 2.实体类 [java] view plai ...

  2. SpringBoot + MyBatis + MySQL +IDEA2021 增删改查 2021-06-04

    SpringBoot + MyBatis + MySQL +IDEA2021 增删改查 1.环境介绍 Windows 10 JDK 1.8 MySQL 8 IDEA 2021.1.2 2.开始操作.新 ...

  3. myBatis 简单的增删改查

    myBatis 简单的增删改查 前面已经讲到用接口的方式编程.这种方式,要注意的一个地方就是.在User.xml 的配置文件中, mapper namespace="com.fjh.inte ...

  4. SpringBoot+Mysql+MDUI实现数据的增删改查和列表操作及单,多文件上传实例

    SpringBoot+Mysql+MDUI实现数据的增删改查和列表操作及单,多文件上传实例 开源地址:https://gitee.com/jfkjrym/demo.git evan原创内容!evan原 ...

  5. mybatis教程--实现增删改查的入门教程

    1 开发环境 jdk1.7.0_72 eclipse:eclipse-3.7-indigo mysql:mysql5.1 1.1 创建数据库 先导入sql_table.sql,再导入sql_data. ...

  6. 【Mybatis 之应用篇】1_Mybatis简介、第一个Mybatis程序和增删改查在Mybatis中的使用方式

    文章目录 Mybatis 一.简介 1.持久化 2.持久层 3.为什么需要Mybatis? 二.第一个Mybatis程序 1.创建一个数据库 2.配置pom.xml文件 3.创建实体类 4.创建一个M ...

  7. MyBatis批量的增删改查操作

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

  8. MyBatis(2.1) 增删改查 mysql java

    增删改查 1.查询数据 2.插入 3.更改 4.删除 5.总代码 1.查询数据 (1)在UserMapper.xml文件中添加 <select id="findByName" ...

  9. SpringBoot+mybatis+postgreSQL+thymeleaf增删改查

    本篇是用SpringBoot搭建一个增删改查的demo,编辑器是idea,适合初学者学习.源码在GitHub上,https://github.com/jiangttian/springboot/tre ...

  10. SpringBoot+Mybatis+thymeleaf实现增删改查

    一.今天用SpringBoot整合thymeleaf做一套增删改查 1.导入依赖 <dependency><groupId>org.mybatis.spring.boot< ...

最新文章

  1. Thymeleaf 学习笔记 (5)
  2. Zend Studio 安装 PyDev 插件后报错
  3. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
  4. A Data Access Layer to persist business objects using attributes and reflection - Part II [无常译]...
  5. c#利用定时器自动备份数据库(mysql)
  6. 迷你版Spring MVC 实现
  7. kali 运行java_kali linux运行java程序
  8. java development kit64_Java SE Development Kit (JDK) v14.0 x64
  9. Spring构建微服务
  10. unity: GetLocalizedString can only be called from the main thread 异常解决
  11. 测试的主要评测方法(3)
  12. OpenVAS安装说明
  13. 在ubuntu 64位的机器上执行arm-linux-gcc提示 no such file or directory
  14. 对服务器上出现大量的SYN_RCVD状态的TCP连接的问题分析
  15. vc调试总是提示项目已过期
  16. 最后采用加权求和的方式得到样本的_一种婴幼儿视力自动检测方法与流程
  17. C语言循环结构作业总结
  18. 2021年汽车修理工(中级)考试题库及汽车修理工(中级)实操考试视频
  19. 机器学习特征工程之特征缩放+无量纲化:非线性缩放(例如,sigmoid、tanh、arctan等)
  20. 计算机外存断电会丢失吗,外储存器断电后信息会丢失吗

热门文章

  1. 计算机怎么看网络密码,怎么查看电脑网络连接密码 - 卡饭网
  2. 《数据结构》算法与算法设计
  3. 几何画板如何生成三角函数图像
  4. 深入浅出设计模式(一):单例模式
  5. Java虚拟机之HotSpot虚拟机
  6. 【备忘】老罗Android开发视频教程[共9大知识点] 下载
  7. R 大数值型数据转换
  8. 计算机体系结构与组成的区别
  9. PHP调用shell命令
  10. MySQL|Aborted connection 日志分析