1. 添加相关依赖

 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion><exclusion><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId></exclusion></exclusions></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1</version></dependency><!--mysql依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--lombok用来简化实体类--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency></dependencies>

2. 编写配置文件


spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false&serverTimezone=UTCusername: rootpassword: 090xxxx
server:port: 8074
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: auto
3. 编写实体类
package com.atguigu.pojo;
@Data
public class User {private Long id;private String name;private Integer age;private String email;//插入时自动填充@TableField(fill = FieldFill.INSERT)private Date createTime;//插入与更新时自动填充@TableField(fill = FieldFill.INSERT_UPDATE)private Date updateTime;@Version //乐观锁@TableField(fill=FieldFill.INSERT) //插入时自动填充private Integer version;//逻辑删除@TableLogicprivate Integer deleted;
}

4. 编写映射文件

package com.atguigu.mapper;@Repository
public interface UserMapper extends BaseMapper<User> {}

5.编写配置MP插件

package com.atguigu.config;import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @FileName: MpConfig* @Author Steven* @Date: 2021/3/12*/
@Configuration
@MapperScan("com.atguigu.mapper")
public class MpConfig {/*** 乐观锁插件* /@Beanpublic OptimisticLockerInterceptor optimisticLockerInterceptor(){return new OptimisticLockerInterceptor();}/*** 分页插件* @return 返回分页拦截器*/@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();}}

6.编写配置自动填充类

/*** @FileName: MyMetaObjectHandler* @Author Steven* @Date: 2021/3/12*/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {this.setFieldValByName("createTime",new Date(),metaObject);this.setFieldValByName("updateTime",new Date(),metaObject);this.setFieldValByName("version",1,metaObject);}@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("updateTime",new Date(),metaObject);}
}

编写测试类

package com.atguigu;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MybatisTests1 {@AutowiredUserMapper userMapper;@Testpublic void listUser(){List<User> users = userMapper.selectList(null);System.out.println(users);}@Testpublic void insert(){User user=new User();user.setName("张宏");user.setAge(24);user.setEmail("test11@baomidou.com");userMapper.insert(user);}@Testpublic void update(){User user=new User();user.setId(1370148840212037635L);user.setName("吴婷");userMapper.updateById(user);}@Testpublic void testOptimisticLock(){User user = userMapper.selectById(1370148840212037636L);user.setName("张琳");int updateById = userMapper.updateById(user);System.out.println(updateById);}@Testpublic void testBatch(){List<User> userList = userMapper.selectBatchIds(Arrays.asList(1, 2, 3, 4));System.out.println(userList);}/*** 简单条件查询*/@Testpublic void testMap(){Map<String,Object> hashmap=new HashMap<>();hashmap.put("name","Jone");hashmap.put("age",18);List<User> userList = userMapper.selectByMap(hashmap);System.out.println(userList);}/*** 分页查询*/@Testpublic void testSelectPage(){Page<User> page=new Page<>(1,3);Page<User> userPage = userMapper.selectPage(page, null);//当前页long current = userPage.getCurrent();//查询当前数据集合List<User> userList = userPage.getRecords();userList.stream().map(User::getName).forEach(System.out::println);//记录总数long total = userPage.getTotal();//是否有上一页boolean hasPrevious = userPage.hasPrevious();//是否有下一页boolean hasNext = userPage.hasNext();System.out.println("当前页:"+current);System.out.println("当前数据集合:"+userList);System.out.println("总记录数:"+total);System.out.println("是否有上一页:"+hasPrevious);System.out.println("是否有下一页:"+hasNext);}@Testpublic void testSelectMap(){Page<Map<String,Object>> page=new Page<>(1,3);Page<Map<String,Object>> userPage= userMapper.selectMapsPage(page, null);//当前数据集合List<Map<String, Object>> records = userPage.getRecords();records.forEach(System.out::println);//当前页long current = userPage.getCurrent();//记录总数long total = userPage.getTotal();//是否有上一页boolean hasPrevious = userPage.hasPrevious();//是否有下一页boolean hasNext = userPage.hasNext();//总页数long size = userPage.getSize();System.out.println("当前页:"+current);System.out.println("是否有上一页:"+hasPrevious);System.out.println("是否有下一页:"+hasNext);System.out.println("总页数:"+size);System.out.println("总记录数:"+total);}/*** 根据ID删除数据*/@Testpublic void testDeleteById(){int result = userMapper.deleteById(1370148840212037634L);System.out.println(result);}/*** 根据ID批量删除数据*/@Testpublic void testDeleteByIds(){int deleteBatchIds = userMapper.deleteBatchIds(Arrays.asList(1370144581273784322L, 1370145757251784706L));System.out.println(deleteBatchIds);}/*** 简单条件删除*/@Testpublic void testDeleteByMap(){Map<String,Object> map=new HashMap<>();map.put("name","张宏");map.put("email","test11@baomidou.com");int result = userMapper.deleteByMap(map);System.out.println(result);}/*** 逻辑删除*/@Testpublic void testLogicDelete(){int id = userMapper.deleteById(1370148840212037633L);System.out.println(id);}/*** 逻辑删除查询*/@Testpublic void testLogicDeleteSelect(){List<User> userList = userMapper.selectList(null);System.out.println(userList);}@Testpublic void testQuery(){QueryWrapper<User> queryWrapper=new QueryWrapper<>();queryWrapper//isNotNull:不等于空.isNotNull("email")//ge:年龄大于等于18.ge("age",18)//gt:大于.gt("age",12)//le:小于等于.le("id",10)//lt:小于.lt("id",8)//eq:等于.eq("id",4)//ne:不等于.ne("age",29)//in:在某个范围内.in("id", Arrays.asList(1,2,3,4,5))//notIn:不在某个区间.notIn("age",Arrays.asList(9,10,11))//在某个区间内.between(true,"id",1,7)//模糊匹配类似于:%n%.like("name","n")//按年龄降序.orderByDesc("age")//按条件升序或降序.orderBy(false,true,"id");List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}}

MybatisPlus操作模板相关推荐

  1. Mybatis-plus操作json字段实战

    后端动态列设计与实现三部曲,这是最后一步,使用java语言,结合mybatis-plus神技操作json字段. 简单介绍下mybatis-plus,大厂中mybatis使用的非常多,而mybatis- ...

  2. 如何设计一门语言(四)——什么是坑(操作模板)

    其实我在写这个系列的第三篇文章的时候就已经发现,距离机器越远,也就是抽象越高的概念,坑的数量是越少的.但是这并不是说,距离机器越近的概念就越强大或者说越接近本质.这是广大的程序员对计算理论的一种误解. ...

  3. Vue.js-Day01-PM【事件绑定(事件传参、事件对象、传参+获取事件对象)、样式处理操作(模板、事件、属性绑定)、Tab切换(原生js实现、Vue.js实现)、js中的this详解关键字】

    Vue.js实训[基础理论(5天)+项目实战(5天)]博客汇总表[详细笔记] 目   录 4.事件绑定 4.1.事件绑定(点击.双击.鼠标移动) 点击按钮-最简单的事件绑定(无参函数) 格式 点击按钮 ...

  4. 面试题 02.01. 移除重复节点(链表删除操作模板)

    链表删除操作:(不带头节点的链表,加上虚拟头节点(dummyHead)删除head就不用单独讨论) 增加虚拟头节点和pre指针: 模板: //创建头节点 ListNode *dummyHead=new ...

  5. Hbase java API操作(模板代码)

    Hbase java API操作 1 创建maven工程 导入jar包 <repositories><repository><id>cloudera</id& ...

  6. Mybatis-Plus操作数据库

    目录 一.使用mybatis-Plus前期准备 二.使用mybatis-Plus分装方法 1.1.根据id进行增删改查 1.2.根据条件构造器进行删除.修改.查询 三.mybatis-Plus使用注解 ...

  7. C++高阶必会操作--模板元编程

    泛型编程大家应该都很熟悉了,主要就是利用模板实现"安全的宏",而模板元编程区别于我们所知道的泛型编程,它是一种较为复杂的模板,属于C++的高阶操作了,它最主要的优点就在于把计算过程 ...

  8. MybatisPlus操作

    1 MybatisPlus (MB)学习 1.1 User类(pojo) package com.jt.pojo;import com.baomidou.mybatisplus.annotation. ...

  9. mybatisplus 操作另一个数据库的数据_c#连接sql数据库以及操作数据库

    1.概述 http://ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据库: 第二,建立SqlCommand对象,负责SQL语句的执行 ...

最新文章

  1. 数据库模型设计——主键的设计
  2. jQuery 选择器和筛选器
  3. Jerry 的 SAP 技术交流群里讨论的技术问题都会同步到这个帖子里
  4. Cookie的设置获取和删除
  5. mysql ip to int_ip网段转换程序(把ip地址转换成相对就的整数)
  6. 自定义字体 (暂不支持中文)
  7. 鸿蒙宴原文及翻译,《鸿门宴》文言文原文及全文详细翻译
  8. Kaggle泰坦尼克数据科学解决方案
  9. mybatis从0到1--学mybatis看这一篇就足够
  10. c语言自行车租赁系统,winform 自行车租赁系统(含数据库)
  11. 计算机二级数据模拟表,2020年计算机二级《Access数据库程序设计》模拟题(5)...
  12. 计算机CPU的常见故障的排除,计算机CPU常见故障与排除.pdf
  13. 如何对一个GIF表情包进行压缩剪裁?
  14. 分析google关键词de工具
  15. html暴风粒子代码,魔兽世界课物品代码及gm指令大全(全部整理自网上).doc
  16. JAVA自学-day16-List的子类、泛型、增强for循环、静态导入、可变参数
  17. 光与夜之恋服务器维护,《光与夜之恋》2021年7月30日停服维护说明
  18. 抖音用什么编程语言_抖音app开发者的心路历程:论开发者的一些经验之谈
  19. 基于图数据库的新型肺炎传染图谱建模与分析
  20. 12、RH850 F1 FLASH存储器介绍

热门文章

  1. opencv计算机视觉学习笔记七
  2. 两个byte[]拼接
  3. 每日一句(2014-8-26)
  4. 浏览器的垃圾回收机制
  5. 多线程搜索磁盘上的文件
  6. 简单是一种美:提高项目成功率的一些方法
  7. php stripos 返回值,php函数stripos详解
  8. python解初中题_用python解一道数独小题
  9. java多线程编程_Java多线程编程实战指南+设计模式篇.pdf
  10. java简单课程设计_!高分跪求帮忙写一个简单小程序的JAVA课程设计报告(内详!!)...