Mybatis 的动态SQL,批量增删查改
个人博客网:https://wushaopei.github.io/ (你想要这里多有)
批量增删改的接口:
public interface BookService {
//批量增加
int saveList(List<Book> records);
//批量查找
List<Book> selectList(List<Integer> ids);
//批量删除
int deleteList(List<Integer> ids);
//批量修改
int updateList(List<Book> bookList);
}
接口实现类:
@Service
public class BookServiceImpl implements BookService{
@Autowired
BookMapper bookMapper;
@Override
public int saveList(List<Book> list) {
// TODO Auto-generated method stub
int count = bookMapper.inserts(list);
return count;
}
@Override
public List<Book> selectList(List<Integer> ids) {
// TODO Auto-generated method stub
List<Book> books = bookMapper.selectByIds(ids);
return books;
}
@Override
public int deleteList(List<Integer> ids) {
// TODO Auto-generated method stub
return bookMapper.deleteByPrimaryKeys(ids);
}
@Override
public int updateList(List<Book> bookList) {
// TODO Auto-generated method stub
return bookMapper.updateByPrimaryKeys(bookList);
}
}
对应的实体类 JavaBean :
public class Book {
public Book(Integer id, String name, String author, BigDecimal price, Integer sales, Integer stock) {
super();
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.sales = sales;
this.stock = stock;
}
............省略
mapper.xml 中SQL 语句的编写:
<!-- 批量根据id进行删除 -->
<delete id="deleteByPrimaryKeys" parameterType="java.util.List" >
delete from t_book
where id in
<foreach collection="list" item="id" open="(" close=")" separator="," >
#{id,jdbcType=INTEGER}
</foreach>
</delete>
<!-- 批量进行插入数据 -->
<insert id="inserts" parameterType="java.util.List">
insert into t_book (id,name,author,price,sales,stock) values
<foreach collection="list" item="Book" separator="," index="index">
(null, #{Book.name}, #{Book.author}, #{Book.price}, #{Book.sales}, #{Book.stock})
</foreach>
</insert>
<!-- 批量根据id进行修改 -->
<update id="updateByPrimaryKeys" parameterType="java.util.List" >
update t_book
<trim prefix="set" suffixOverrides=",">
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="Book" index="index">
<if test="Book.name!=null">
when id=#{Book.id} then #{Book.name}
</if>
</foreach>
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="Book" index="index">
<if test="Book.author!=null">
when id=#{Book.id} then #{Book.author}
</if>
</foreach>
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="Book" index="index">
<if test="Book.price!=null">
when id=#{Book.id} then #{Book.price}
</if>
</foreach>
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="Book" index="index">
<if test="Book.sales!=null">
when id=#{Book.id} then #{Book.sales}
</if>
</foreach>
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="Book" index="index">
<if test="Book.stock!=null">
when id=#{Book.id} then #{Book.stock}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="Book" index="index">
id=#{Book.id,jdbcType=INTEGER}
</foreach>
</update>
<!-- 批量根据id查找 -->
<select id="selectByIds" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, name, author, price, sales, stock
from t_book
where id in
<foreach collection="list" item="id" open="(" close=")" separator="," >
#{id,jdbcType=INTEGER}
</foreach>
</select>
测试: test 对批量操作进行测试是否成功:
/*
* 批量插入
* */
@Test
public void InsertBookServices()throws SQLException {
List<Book> bookList = new ArrayList<>();
bookList.add(new Book(null,"生活1","奕1君",new BigDecimal(1),1,1));
bookList.add(new Book(null,"生活2","奕2君",new BigDecimal(2),2,2));
bookList.add(new Book(null,"生活3","奕3君",new BigDecimal(3),3,3));
bookList.add(new Book(null,"生活4","奕4君",new BigDecimal(4),4,4));
bookList.add(new Book(null,"生活5","奕5君",new BigDecimal(5),5,5));
int count = bookService.saveList(bookList);
System.out.println(count);
}
/*
* 批量查询
* */
@Test
public void SelectBookService()throws SQLException {
List<Integer> Ids = new ArrayList();
Ids.add(1);
Ids.add(2);
Ids.add(3);
Ids.add(4);
List<Book> Books = bookService.selectList(Ids);
for(Book book : Books) {
System.out.println(book.toString());
}
}
/*
* 批量删除
* */
@Test
public void DeleteBookService()throws SQLException {
List<Integer> Ids = new ArrayList();
Ids.add(1);
Ids.add(2);
Ids.add(3);
Ids.add(4);
int counts = bookService.deleteList(Ids);
System.out.println(counts);
}
/*
* 批量更新
* */
@Test
public void UpdateBookService()throws SQLException {
List<Book> bookList = new ArrayList<>();
bookList.add(new Book(6,"生活6","奕6君",new BigDecimal(1),1,1));
// bookList.add(new Book(7,"生活7","奕7君",new BigDecimal(2),2,2));
bookList.add(new Book(8,"生活8","奕8君",new BigDecimal(3),3,3));
bookList.add(new Book(9,"生活9","奕9君",new BigDecimal(4),4,4));
bookList.add(new Book(10,"生活10","奕10君",new BigDecimal(5),5,5));
int count = bookService.updateList(bookList);
System.out.println(count);
}
链接:https://pan.baidu.com/s/1oAYg5X8eeqf18dUTU1bUpA
提取码:jznv
复制这段内容后打开百度网盘手机App,操作更方便哦
Mybatis 的动态SQL,批量增删查改的更多相关文章
- ASP.NET动态的网页增删查改
动态页面的增删查改,不多说了,直接上代码 跟前面的一般处理程序一样我上用的同一套三层,只是UI层的东西不一样,在纠结着要不要重新在上一次以前上过的代码: 纠结来纠结去,最后我觉得还上上吧,毕竟不上为我 ...
- MERGE批量增删查改数据
MERGE优点: 在批量处理数据的时候,我可以用到merge一次完成数据处理. 示例代码一: MERGE INTO student AS t using ( AS age) s ON t.Age=s. ...
- dbflow 批量 增删查改
@ModelContainer @Table(database = DemoDatabase.class) class Person extends BaseModel implements Seri ...
- 通过web sql实现增删查改
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Mybatis基础配置及增删查改操作
一.简介 平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类. 不仅如此,访问不同的表,还会 ...
- mybatis实现简单的增删查改
接触一个新技术,首先去了解它的一些基本概念,这项技术用在什么方面的.这样学习起来,方向性也会更强一些.我对于mybatis的理解是,它是一个封装了JDBC的java框架.所能实现的功能是对数据库进行增 ...
- 常用SQL语句(增删查改、合并统计、模糊搜索)
转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...
- Sql Server的艺术(一) 视图的增删查改
视图是从一个或者多个表中查询数据的另一种方式.利用视图可以集中.简化定制数据库,同时还能保障安全. 视图其结构和数据是建立在对应的查询基础上的.和表一样,视图也是包括几个被定义的数据列和多个数据行,但 ...
- SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)
SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...
随机推荐
- vue-cli3使用全局scss
在开发项目的时候,经常会出现多个元素样式相同,比如颜色相同.这里就需要我们设置公共样式,方便后期调试 一配置方法 1.在src/assets/styles目录下创建文件variable.scss // ...
- 近期总结的一些Java基础
1.面向过程:当需要实现一个功能的时候,每一个过程中的详细步骤和细节都要亲力亲为. 2.面向对象:当需要实现一个功能的时候,不关心详细的步骤细节,而是找人帮我做事. 3.类和对象的关系: a-类是 ...
- learn from collection framework design
最难忍受的痛苦,也许是想干一件事情而又不去干.--罗曼·罗兰 前言 本篇文章算是拾人牙慧吧,偶尔谷歌到一个能很好把collection framework design讲好的文档,一是为了总结提升,也 ...
- 【matlab 基础篇 02】基础知识一键扫盲,看完即可无障碍编程(超详细+图文并茂)
博主快速入门matlab,系统地整理一遍,如何你和我一样是一个新手,那么此文很适合你: 本人能力有限,文中难免有错误和纰漏之处,请大佬们不吝赐教 创作不易,如果本文帮到了您: 请帮忙点个赞
- 攻防世界-web-高手进阶区018-easytornado
1.查看主页面 2.查看其他页面,/welcome.txt http://111.198.29.45:39004/file?filename=/welcome.txt&filehash=9ae ...
- 数据库中取出YYYY-mm-dd H:i:s的数据怎么将其转化成YYYY/mm/dd格式,另外,怎么将一个数据表中的数据插入另一个数据表
sql语句是select left(replace(rq,'-','/'),10) as rq from 表名 tp5.1中的写法 $res = Db::table('表名') ->field ...
- 类型信息(反射,RTTI)
类型信息 1.java如何在运行时识别对象和类的信息 "传统的"RTTI run-time type identification ,假设我们在编译时已经知道了所有类型,在编译的时 ...
- nginx均衡负载
一直在担心session 问题,结果试了2个web 论坛,discuz 和phpbb ,前面用nginx 均衡负载,后端是apache httpd +php ,mysql 用同一个,修改一下confi ...
- 感觉shopex现在的升级方式太慢了
我是说产品的更新,484,485是一个经典的版本,那时候免费,shopex 系统市场占用率很高.但是485以后呢,只有小版本的更新,fxw ,ekd 都是改进版本吧,没用特别大幅度的更新.5年前,10 ...
- 轻松扩展机器学习能力:如何在Rancher上安装Kubeflow
随着机器学习领域不断发展,对于处理机器学习的团队来说,在1台机器上训练1个模型已经有些难以为继,并且现在业界的共识是机器学习已经不仅仅是简单的模型训练. 在模型训练之前.过程中和之后,需要进行许多活动 ...