关键词:
MyBatis Plus介绍
MyBatis Plus (简称MP)是国内人员开发的 MyBatis 增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特征
无侵入:Mybatis-Plus 在 Mybatis 的基础上进行扩展,只做增强不做改变,引入 Mybatis-Plus 不会对您现有的 Mybatis 构架产生任何影响,而且 MP 支持所有 Mybatis 原生的特性
依赖少:仅仅依赖 Mybatis 以及 Mybatis-Spring
损耗小:启动即会自动注入基本CURD,性能基本无损耗,直接面向对象操作
预防Sql注入:内置Sql注入剥离器,有效预防Sql注入攻击
通用CRUD操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
多种主键策略:支持多达4种主键策略(内含分布式唯一ID生成器),可自由配置,完美解决主键问题
框架结构
Mybatis Plus使用
1.导入依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.0.1</version> </dependency>
2.MP整合的配置
<!-- 0.连接池属性设置读取指定的properties文件 --> <context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true"/> <!-- 1.将连接池放入spring容器 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置实体扫描路径,多个package可以用分号; 逗号, 分隔, 支持通配符*--> <!-- com.a.b.entity;com.a.c.entity;com.d.*.entity--> <property name="typeAliasesPackage" value="cn.xm.jwxt.bean.*"/> <property name="configuration" ref="mybatisConfig"/> <!-- MP 全局配置注入 --> <property name="globalConfig" ref="globalConfig"/> <property name="plugins"> <array> <!-- 分页插件配置 --> <bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"/> <!-- 性能拦截器,兼打印sql,不建议生产环境配置--> <bean id="performanceInterceptor" class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"/> </array> </property> </bean> <bean id="mybatisConfig" class="com.baomidou.mybatisplus.MybatisConfiguration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> <!-- 定义 MP 全局策略 --> <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration"> <property name="idType" value="2"/> <property name="dbType" value="mysql"/> <!-- 全局表为下划线命名设置 true --> <property name="dbColumnUnderline" value="true"/> </bean> <!-- 配置mybatis 扫描mapper接口的路径, 相当于注解@MapperScan,@MapperScan("com.baomidou.mybatisplus.test.h2.entity.mapper")--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.xm.jwxt.mapper"/> </bean>
3.实体
@TableName(value="employee")
public class Employee { private Integer id ; private String lastName; private String email ; private Integer gender; private Integer age ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", age=" + age + "]"; } }
4.mapper 接口
public interface EmployeeMapper extends BaseMapper<Employee> {}
5.新增
@Test public void testCommonInsert() { //初始化Employee对象 Employee employee = new Employee(); employee.setLastName("MP"); employee.setEmail("mp@atguigu.com"); employee.setGender(1); //insert方法在插入时,会根据实体类的每个属性进行非空判断,只有非空的属性对应的字段才会出现到SQL语句中 Integer result = employeeMapper.insert(employee); System.out.println("result: " + result ); //insertAllColumn方法在插入时,不管属性是否非空, 属性所对应的字段都会出现到SQL语句中. Integer result1 = employeeMapper.insertAllColumn(employee); System.out.println("result: " + result1 ); //获取当前数据在数据库中的主键值 Integer key = employee.getId(); System.out.println("key:" + key ); }
6.修改
@Test public void testCommonUpdate() { //初始化修改对象 Employee employee = new Employee(); employee.setId(7); employee.setLastName("王五"); employee.setEmail("xz@sina.com"); employee.setGender(0); //updateById方法在修改时,会根据实体类的每个属性进行非空判断,只有非空的属性对应的字段才会出现到SQL语句中 Integer result = employeeMapper.updateById(employee); System.out.println("result: " + result ); //updateAllColumnById方法在修改时,不管属性是否非空, 属性所对应的字段都会出现到SQL语句中. Integer result1 = employeeMapper.updateAllColumnById(employee); System.out.println("result: " + result1 ); }
7.查询
@Test public void testCommonSelect() { //1. 通过id查询 Employee employee = employeeMapper.selectById(7); System.out.println(employee); //2. 通过多个列进行查询。selectOne查询结果只能是一条,否则报错 Employee employee1 = new Employee(); employee.setLastName("王五"); employee.setGender(0); Employee result = employeeMapper.selectOne(employee1); System.out.println("result: " +result ); //3. 通过多个id进行查询 List<Integer> idList = new ArrayList<>(); idList.add(4); idList.add(5); idList.add(6); idList.add(7); List<Employee> emps = employeeMapper.selectBatchIds(idList); System.out.println(emps); //4. 通过Map封装条件查询。map中的key是数据库中字段名 Map<String,Object> columnMap = new HashMap<>(); columnMap.put("last_name", "Tom"); columnMap.put("gender", 1); List<Employee> emps1 = employeeMapper.selectByMap(columnMap); System.out.println(emps1); //5. 分页查询 List<Employee> emps2 = employeeMapper.selectPage(new Page<>(3, 2), null); System.out.println(emps2); }
8.删除
@Test public void testCommonDelete() { //1 .根据id进行删除 Integer result = employeeMapper.deleteById(13); System.out.println("result: " + result ); //2. 根据条件进行删除 Map<String,Object> columnMap = new HashMap<>(); columnMap.put("last_name", "MP"); columnMap.put("email", "mp@atguigu.com"); Integer result1 = employeeMapper.deleteByMap(columnMap); System.out.println("result: " + result1 ); //3. 批量删除 List<Integer> idList = new ArrayList<>(); idList.add(3); idList.add(4); idList.add(5); Integer result2 = employeeMapper.deleteBatchIds(idList); System.out.println("result: " + result2 ); }
[mybatisplus]入门案例(代码片段)
入门案例创建测试数据库和表CREATEDATABASE`mybatis_plus`/*!40100DEFAULTCHARACTERSETutf8mb4*/;use`mybatis_plus`;CREATETABLE`user`(`id`bigint(20)NOTNULLCOMMENT'主键ID',`name 查看详情
带你入门mybatisplus
文章目录一、环境搭建1.准备数据库环境2.创建SpringBoot工程二、编写代码1.配置application.yml2.启动类3.添加实体3.添加Mapper三、测试四、自定义文件一、环境搭建1.准备数据库环境创建表:CREATEDATABASE`mybatis_plus`/*!40100DEFAULTC... 查看详情
mybatisplus入门(代码片段)
一、认识MyBatis-Plus(简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。支持多种数据库:支持MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer等多种数据库。... 查看详情
mybatisplus
1.MybatisPlus入门1.1快速入门1.1.1新建工程新建模块,导入依赖坐标<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi=" 查看详情
mybatisplus——入门案例(代码片段)
MyBatisPlusMyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率开发方式基于MyBatis使用MyBatisPlus基于Spring使用MyBatisPlus基于SpringBoot使用MyBatisPlus SpringBoot整合MyBatis开发过程(复习)创建SpringBoot... 查看详情
mybatisplus快速入门(2021.07.16)(代码片段)
目录一、MyBatis-Plus入门1、简介2、创建并初始化数据库3、确认idea配置4、创建项目5、编写代码二、主键策略1、插入操作2、MP的主键策略三、自动填充和乐观锁1、更新操作2、自动填充3、乐观锁4、乐观锁实现流程四、查询1、查询... 查看详情
mybatisplus快速入门(2021.07.16)(代码片段)
目录一、MyBatis-Plus入门1、简介2、创建并初始化数据库3、确认idea配置4、创建项目5、编写代码二、主键策略1、插入操作2、MP的主键策略三、自动填充和乐观锁1、更新操作2、自动填充3、乐观锁4、乐观锁实现流程四、查询1、查询... 查看详情
2022年还有人不会用mybatisplus吗?(代码片段)
一、MyBatisPlus入门案例以及简介本文分享MyBatisPlus入门案例与简介,这个和其他课程都不太一样。我们学技术的时候通常是先分享概念,然后是入门案例。对于MyBatisPlus的学习,我们进行了顺序上的调整。主要原因是MyB... 查看详情
mybatisplus入门应用,通用iservice增删改查(代码片段)
先上依赖项目是springboot的,用的maven管理依赖<!--https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter--><dependency><groupId>com.baomidou</groupId><artifactId&g 查看详情
java--mybatisplus入门;与mybatis区别;简单使用(代码片段)
阅读前可先参考Java--MyBatis入门_MinggeQingchun的博客-CSDN博客_javamybatis一、MyBatis-PlusMyBatis-Plus(简称MP)是一个 Mybatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生MyBatis-Plus在MyBatis之上... 查看详情
mybatisplus
今日目标基于MyBatisPlus完成标准Dao的增删改查功能掌握MyBatisPlus中的分页及条件查询构建掌握主键ID的生成策略了解MyBatisPlus的代码生成器1,MyBatisPlus入门案例与简介这一节我们来学习下MyBatisPlus的入门案例与简介,这个和... 查看详情
mybatisplus快速入门常用设置(表映射主键策略日志)基本使用(代码片段)
(目录)MybatisPlus基础篇1.概述 MybatisPlus是一款Mybatis增强工具,用于简化开发,提高效率。它在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。 官网:https://mp.baomidou.com/2.快速入门2.0准备工作①准备数据CREATETABL... 查看详情
mybatisplus入门(代码片段)
文章目录MyBatisPlus概述1.1简介1.2特点2.入门案例2.1搭建环境2.2数据库和表2.3入门:查询所有3.基本操作3.1常见API3.2添加3.3更新3.4删除4查询4.1Map条件4.2QueryWrapper4.2.1**wrapper介绍**4.2.2条件查询4.3.3条件更新4.3分页4.3.1内置插件4.3.2配... 查看详情
springboot-mybatisplus笔记
1,课程回顾2,本章重点mybatisplus简介特点mybatisplus入门例子springboot整合mybatisplus及使用mybatisplus自动代码生成mybatisplus分页插件hutool工具包easycode代码生成springboot+mybatisplus3,具体内容3.1mybatisplus简介特征3.1.1简介& 查看详情
基于mybatisplus完成标准dao的增删改查功能(代码片段)
MyBatisPlus今日目标基于MyBatisPlus完成标准Dao的增删改查功能1,MyBatisPlus入门案例与简介这一节我们来学习下MyBatisPlus的入门案例与简介,这个和其他课程都不太一样,其他的课程都是先介绍概念,然后再写入门案例... 查看详情
springboot整合mybatisplus(代码片段)
...工具:IDEA基础环境:Maven+JDK8所用技术:SpringBoot、lombok、MybatisPlusSpringBoot版本:2.1.41.3涉及知识点MybatisPlus简介、特性、架构MybatisPlus快速入门MybatisPlus的基本CRUDMybatisPlus的高级查询:like 查看详情
mybatisplus快速入门(2021.07.16)(代码片段)
目录一、MyBatis-Plus入门1、简介2、创建并初始化数据库3、确认idea配置4、创建项目5、编写代码二、主键策略1、插入操作2、MP的主键策略三、自动填充和乐观锁1、更新操作2、自动填充3、乐观锁4、乐观锁实现流程四、查询1、查询... 查看详情
[study]mybatisplus(代码片段)
一、入门案例创建数据库与表CREATEDATABASEmybatis_plus;USEmybatis_plus;CREATETABLE`user`(`id`bigintNOTNULLAUTO_INCREMENT,`name`varchar(255)CHARACTERSETutf8mb4COLLATEutf8mb4_0900_ai_ciNULLDEFA 查看详情