中国建设银行官方网站下载,购物网站优惠券怎么做,西部数码域名注册,正规的网站制作平台结构直接看目录 前言
MyBatis-Plus 是一个 MyBatis 的增强工具#xff0c;在 MyBatis 的基础上只做增强不做改变#xff0c;为简化开发、提高效率而生。
愿景
我们的愿景是成为 MyBatis 最好的搭档#xff0c;就像 魂斗罗 中的 1P、2P#xff0c;基友搭配#xff0c;效…结构直接看目录 前言
MyBatis-Plus 是一个 MyBatis 的增强工具在 MyBatis 的基础上只做增强不做改变为简化开发、提高效率而生。
愿景
我们的愿景是成为 MyBatis 最好的搭档就像 魂斗罗 中的 1P、2P基友搭配效率翻倍。 特性
无侵入只做增强不做改变引入它不会对现有工程产生影响如丝般顺滑损耗小启动即会自动注入基本 CURD性能基本无损耗直接面向对象操作强大的 CRUD 操作内置通用 Mapper、通用 Service仅仅通过少量配置即可实现单表大部分 CRUD 操作更有强大的条件构造器满足各类使用需求支持 Lambda 形式调用通过 Lambda 表达式方便的编写各类查询条件无需再担心字段写错支持主键自动生成支持多达 4 种主键策略内含分布式唯一 ID 生成器 - Sequence可自由配置完美解决主键问题支持 ActiveRecord 模式支持 ActiveRecord 形式调用实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作支持全局通用方法注入 Write once, use anywhere 内置代码生成器采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码支持模板引擎更有超多自定义配置等您来使用内置分页插件基于 MyBatis 物理分页开发者无需关心具体操作配置好插件之后写分页等同于普通 List 查询分页插件支持多种数据库支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库内置性能分析插件可输出 SQL 语句以及其执行时间建议开发测试时启用该功能能快速揪出慢查询内置全局拦截插件提供全表 delete 、 update 操作智能分析阻断也可自定义拦截规则预防误操作 SpringBoot使用
将以下内容引入pom.xml dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.2/version/dependency以下内容可自选
spring:#配置数据源信息datasource:type: com.zaxxer.hikari.HikariDataSource#配置数据库连接信息driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhostusername: rootpassword: root#mybatisplus添加日志功能
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.stdOutImpl
global-config:db-config:#配置Mybatis-plus操作表的默认前缀table-prefix: t_#配置Mybatis-plus的主键策略id-type: auto
配置文件注意事项 驱动类driver-class-name spring boot 2.0内置jdbc5驱动驱动类使用driver-class-name: com.mysql.jdbc.Driver spring boot 2.1及以上内置jdbc8驱动驱动类使用 driver-class-name: com.mysql.cj.jdbc.Driver 否则运行测试用例的时候会有 WARN 信息 连接地址url MySQL5.7版本的url jdbc:mysql://localhost:3306/?characterEncodingutf-8useSSLfalse MySQL8.0版本的url jdbc:mysql://localhost:3306/? serverTimezoneGMT%2B8characterEncodingutf-8useSSLfalse 注意事项 // 这个属性用于指定 MyBatis Mapper XML 文件的位置例如mybatis-plus: mapper-locations: classpath*:/mapper/*Mapper.xml mybatis-plus.mapper-locations // 这个属性用于设置是否显示 MyBatis-Plus 的启动 banner。在示例中该属性被设置为 false表示禁用启动 banner mybatis-plus.global-config.banner // 这个属性用于设置主键 ID 的生成策略。在示例中auto 表示使用数据库自动生成的主键 ID mybatis-plus.global-config.db-config.id-typeauto // 这些属性用于设置全局的 SQL 过滤策略。在示例中not_empty 表示只在参数值非空时才会生成对应的 SQL 条件 mybatis-plus.global-config.db-config.where-strategynot_empty mybatis-plus.global-config.db-config.insert-strategynot_empty mybatis-plus.global-config.db-config.update-strategynot_null // 这个属性用于设置在 SQL 中处理空值时使用的 JDBC 类型。在示例中null 表示使用 NULL 类型 mybatis-plus.configuration.jdbc-type-for-nullnull // 这个属性用于设置是否在设置属性为 NULL 时调用对应的 setter 方法。在示例中该属性被设置为 true表示在设置属性为 NULL 时会调用 setter 方法 mybatis-plus.configuration.call-setters-on-nullstrue 引导类中添加 MapperScan 注解扫描 Mapper 文件夹
SpringBootApplication
MapperScan(com.baomidou.mybatisplus.samples.quickstart.mapper)
public class MPApplication {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}建立实体类
自行建立一个实体可以是最简单的 学生姓名班级学号 建立Mapper层
继承BaseMapper后面的实体写自己的实体
Mapper
public interface UserMapper extends BaseMapperUserEntity{}建立Service层
建立Service接口
public interface UserService extends IServiceUserEntity {}
建立Service接口实现类ServiceImpl Slf4jServicepublic class UserServiceImpl extends ServiceImplUserMapper, UserEntity implements UserService {}
Service Interface接口内容说明
插入功能save
// 插入一条记录选择字段策略插入
boolean save(T entity);
// 插入批量
boolean saveBatch(CollectionT entityList);
// 插入批量
boolean saveBatch(CollectionT entityList, int batchSize);
功能描述插入记录根据实体对象的字段进行策略性插入。返回值 boolean表示插入操作是否成功。参数说明
类型参数名描述Tentity实体对象CollectionTentityList实体对象集合intbatchSize插入批次数量
示例
// 假设有一个 User 实体对象
User user new User();
user.setName(John Doe);
user.setEmail(john.doeexample.com);
boolean result userService.save(user); // 调用 save 方法
if (result) {System.out.println(User saved successfully.);
} else {System.out.println(Failed to save user.);
} 生成的 SQL: INSERT INTO user (name, email) VALUES (John Doe, john.doeexample.com)
如果存在就更新不存在就新增 saveOrUpdate
功能描述 根据实体对象的主键 ID 进行判断存在则更新记录否则插入记录。返回值 boolean表示插入或更新操作是否成功。参数说明
类型参数名描述Tentity实体对象WrapperTupdateWrapper实体对象封装操作类 UpdateWrapperCollectionTentityList实体对象集合intbatchSize插入批次数量
// TableId 注解属性值存在则更新记录否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, WrapperT updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(CollectionT entityList);
// 批量修改插入
boolean saveOrUpdateBatch(CollectionT entityList, int batchSize);
示例
// 假设有一个 User 实体对象其中 id 是 TableId 注解的属性
User user new User();
user.setId(1);
user.setName(John Doe);
user.setEmail(john.doeexample.com);
boolean result userService.saveOrUpdate(user); // 调用 saveOrUpdate 方法
if (result) {System.out.println(User updated or saved successfully.);
} else {System.out.println(Failed to update or save user.);
}
生成的 SQL假设 id 为 1 的记录已存在:
UPDATE user SET name John Doe, email john.doeexample.com WHERE id 1
生成的 SQL假设 id 为 1 的记录不存在:
INSERT INTO user (id, name, email) VALUES (1, John Doe, john.doeexample.com)删除功能remove
// 根据 queryWrapper 设置的条件删除记录
boolean remove(WrapperT queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件删除记录
boolean removeByMap(MapString, Object columnMap);
// 删除根据ID 批量删除
boolean removeByIds(Collection? extends Serializable idList);
功能描述 通过指定条件删除符合条件的记录。返回值 boolean表示删除操作是否成功。参数说明
类型参数名描述WrapperTqueryWrapper实体包装类 QueryWrapperSerializableid主键 IDMapString, ObjectcolumnMap表字段 map 对象Collection? extends SerializableidList主键 ID 列表
示例:
删除
// 假设有一个 QueryWrapper 对象设置删除条件为 name John Doe
QueryWrapperUser queryWrapper new QueryWrapper();
queryWrapper.eq(name, John Doe);
boolean result userService.remove(queryWrapper); // 调用 remove 方法
if (result) {System.out.println(Record deleted successfully.);
} else {System.out.println(Failed to delete record.);
} 生成的 SQL:
DELETE FROM user WHERE name John Doe
根据ID删除
// 假设要删除 ID 为 1 的用户
boolean result userService.removeById(1); // 调用 removeById 方法
if (result) {System.out.println(User deleted successfully.);
} else {System.out.println(Failed to delete user.);
} 生成的 SQL:
DELETE FROM user WHERE id 1
批量删除
// 假设有一组 ID 列表批量删除用户
ListInteger ids Arrays.asList(1, 2, 3);
boolean result userService.removeByIds(ids); // 调用 removeByIds 方法
if (result) {System.out.println(Users deleted successfully.);
} else {System.out.println(Failed to delete users.);
} 生成的 SQL:
DELETE FROM user WHERE id IN (1, 2, 3)
更新功能update
// 根据 UpdateWrapper 条件更新记录 需要设置sqlset
boolean update(WrapperT updateWrapper);
// 根据 whereWrapper 条件更新记录
boolean update(T updateEntity, WrapperT whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(CollectionT entityList);
// 根据ID 批量更新
boolean updateBatchById(CollectionT entityList, int batchSize);
功能描述 通过指定条件更新符合条件的记录。返回值 boolean表示更新操作是否成功。参数说明
类型参数名描述WrapperTupdateWrapper实体对象封装操作类 UpdateWrapperTentity实体对象CollectionTentityList实体对象集合intbatchSize更新批次数量 根据元素进行更新
// 假设有一个 UpdateWrapper 对象设置更新条件为 name John Doe更新字段为 email
UpdateWrapperUser updateWrapper new UpdateWrapper();
updateWrapper.eq(name, John Doe).set(email, john.doenewdomain.com);
boolean result userService.update(updateWrapper); // 调用 update 方法
if (result) {System.out.println(Record updated successfully.);
} else {System.out.println(Failed to update record.);
} 生成的 SQL:
UPDATE user SET email john.doenewdomain.com WHERE name John Doe
根据ID进行更新
// 假设有一个 User 实体对象设置更新字段为 email根据 ID 更新
User updateEntity new User();
updateEntity.setId(1);
updateEntity.setEmail(updated.emailexample.com);
boolean result userService.updateById(updateEntity); // 调用 updateById 方法
if (result) {System.out.println(Record updated successfully.);
} else {System.out.println(Failed to update record.);
} 生成的 SQL:
UPDATE user SET email updated.emailexample.com WHERE id 1
查询功能get
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper查询一条记录。结果集如果是多个会抛出异常随机取一条加上限制条件 wrapper.last(LIMIT 1)
T getOne(WrapperT queryWrapper);
// 根据 Wrapper查询一条记录
T getOne(WrapperT queryWrapper, boolean throwEx);
// 根据 Wrapper查询一条记录
MapString, Object getMap(WrapperT queryWrapper);
// 根据 Wrapper查询一条记录
V V getObj(WrapperT queryWrapper, Function? super Object, V mapper);
功能描述 根据指定条件查询符合条件的记录。返回值 查询结果可能是实体对象、Map 对象或其他类型。参数说明
类型参数名描述Serializableid主键 IDWrapperTqueryWrapper实体对象封装操作类 QueryWrapperbooleanthrowEx有多个 result 是否抛出异常Tentity实体对象Function? super Object, Vmapper转换函数
根据id进行查询
// 假设要查询 ID 为 1 的用户
User user userService.getById(1); // 调用 getById 方法
if (user ! null) {System.out.println(User found: user);
} else {System.out.println(User not found.);
} 生成的 SQL:
SELECT * FROM user WHERE id 1
根据对象进行查询
// 假设有一个 QueryWrapper 对象设置查询条件为 name John Doe
QueryWrapperUser queryWrapper new QueryWrapper();
queryWrapper.eq(name, John Doe);
User user userService.getOne(queryWrapper); // 调用 getOne 方法
if (user ! null) {System.out.println(User found: user);
} else {System.out.println(User not found.);
}
生成的SQL
SELECT * FROM user WHERE name John Doe LIMIT 1
查询记录功能list // 查询所有
ListT list();
// 查询列表
ListT list(WrapperT queryWrapper);
// 查询根据ID 批量查询
CollectionT listByIds(Collection? extends Serializable idList);
// 查询根据 columnMap 条件
CollectionT listByMap(MapString, Object columnMap);
// 查询所有列表
ListMapString, Object listMaps();
// 查询列表
ListMapString, Object listMaps(WrapperT queryWrapper);
// 查询全部记录
ListObject listObjs();
// 查询全部记录
V ListV listObjs(Function? super Object, V mapper);
// 根据 Wrapper 条件查询全部记录
ListObject listObjs(WrapperT queryWrapper);
// 根据 Wrapper 条件查询全部记录
V ListV listObjs(WrapperT queryWrapper, Function? super Object, V mapper);
功能描述 查询符合条件的记录。返回值 查询结果可能是实体对象、Map 对象或其他类型。参数说明
类型参数名描述WrapperTqueryWrapper实体对象封装操作类 QueryWrapperCollection? extends SerializableidList主键 ID 列表MapString, ObjectcolumnMap表字段 map 对象Function? super Object, Vmapper转换函数
示例
查询全部
// 查询所有用户
ListUser users userService.list(); // 调用 list 方法
for (User user : users) {System.out.println(User: user);
}
生成的SQL
SELECT * FROM user
根据实体进行查询
// 假设有一个 QueryWrapper 对象设置查询条件为 age 25
QueryWrapperUser queryWrapper new QueryWrapper();
queryWrapper.gt(age, 25);
ListUser users userService.list(queryWrapper); // 调用 list 方法
for (User user : users) {System.out.println(User: user);
}
生成的SQL
SELECT * FROM user WHERE age 25
批量查询用户
// 假设有一组 ID 列表批量查询用户
ListInteger ids Arrays.asList(1, 2, 3);
CollectionUser users userService.listByIds(ids); // 调用 listByIds 方法
for (User user : users) {System.out.println(User: user);
}
生成的SQL
SELECT * FROM user WHERE id IN (1, 2, 3)
分页查询
// 无条件分页查询
IPageT page(IPageT page);
// 条件分页查询
IPageT page(IPageT page, WrapperT queryWrapper);
// 无条件分页查询
IPageMapString, Object pageMaps(IPageT page);
// 条件分页查询
IPageMapString, Object pageMaps(IPageT page, WrapperT queryWrapper);
功能描述 分页查询符合条件的记录。返回值 分页查询结果包含记录列表和总记录数。参数说明
类型参数名描述IPageTpage翻页对象WrapperTqueryWrapper实体对象封装操作类 QueryWrapper
示例
分页查询
// 假设要进行无条件的分页查询每页显示10条记录查询第1页
IPageUser page new Page(1, 10);
IPageUser userPage userService.page(page); // 调用 page 方法
ListUser userList userPage.getRecords();
long total userPage.getTotal();
System.out.println(Total users: total);
for (User user : userList) {System.out.println(User: user);
} 生成的SQL
SELECT * FROM user LIMIT 10 OFFSET 0