Mybatis-plus 下
Mybatis-plus 下
查询操作
1.查询单个用户
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}

2.查询多个用户
@Test
public void testSelectById(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
}

3.条件查询
@Test
public void testSelectById(){
HashMap<String,Object> map = new HashMap<>();
//自定义查询条件
map.put("name","小张");
map.put("age",18);
List<User> users1 = userMapper.selectByMap(map);
users1.forEach(System.out::println);
}

分页查询
1.配置拦截器
@EnableTransactionManagement
@Configuration
@MapperScan("com.zc.mapper")
public class MyBatisPlusConfig {
//分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
2.书写方法
@Test
public void Page(){
//参数1: 第几页 ; 参数2 页面大小,几条数据
Page<User> page = new Page<>(1,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
//一共有多少条数据
System.out.println(page.getTotal());
}
此时是 第1页,每页有5条数据

删除操作
1.删除单个用户
@Test
public void DeleteById(){
userMapper.deleteById(1374350451559940099L);
}

2.删除多个用户
@Test
public void DeleteById(){
userMapper.deleteBatchIds(Arrays.asList(4,5));
}

3.按条件删除用户
@Test
public void DeleteById(){
HashMap<String,Object> map = new HashMap<>();
map.put("name","小李");
userMapper.deleteByMap(map);
}

逻辑删除
物理删除:从数据库中直接移除
逻辑删除:再数据库中没有被移除,而是通过一个变量来让他失效!deleted =0=> deleted =1
步骤:
1.数据表中增加一个 deleted 字段
注意,不要添加成delete字段,delete为关键字,会出错

2.实体类加入字段、注解
@TableLogic //逻辑删除
private Integer deleted;
3.配置文件
## 全局逻辑删除的实体字段名(如果配置这个,可以不添加实体类中的逻辑删除注解)
mybatis-plus.global-config.db-config.logic-delete-field=deleted
# 逻辑已删除值(默认为 1)
mybatis-plus.global-config.db-config.logic-delete-value=1
# 逻辑未删除值(默认为 0)
mybatis-plus.global-config.db-config.logic-not-delete-value=0
4.测试
删除操作:
@Test
public void TableLogic(){
userMapper.deleteById(1374350451559940101L);
}
虽然执行的是删除操作,但是实际上逻辑删除会更新deleted字段,而不是直接删除


查询操作:
既然没有删除该语句,那可以查询到吗?
@Test
public void TableLogic(){
userMapper.selectById(1374350451559940101L);
}

上图可以看出,他会查询deleted字段为0为前提的数据,如果被逻辑删除过的数据,deleted字段为1,查询不到。
条件构造器
当前我的数据库数据为:

测试一
name不为空,邮箱不为空,年龄大于等于12 的用户
@Test
void test1() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.isNotNull("name")
.isNotNull("email")
.ge("age",12);
userMapper.selectList(wrapper);
}

测试二
查询名字:小张
查询一个数据时,使用selectOne,查询多个数据,可以使用List或者Map
@Test
void test2() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","小张");
User user = userMapper.selectOne(wrapper);
System.out.println(user);
}

测试三
查询年龄10-15之间的
selectCount直接返回Integer类型,统计查到了的数据条数
@Test
void test3() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age",10,15);
Integer integer = userMapper.selectCount(wrapper);
System.out.println(integer);
}

测试四
模糊查询
- notLike():不包含
- likeRight():效果与 x% 相同
- likeLeft():效果与 %x 相同
@Test
void test4() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.notLike("name","邦")
.likeRight("email","t");
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
}

测试五
复杂查询
@Test
void test5() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.inSql("id","select id from user where id<3");
List<Object> objects = userMapper.selectObjs(wrapper);
objects.forEach(System.out::println);
}

测试六
orderBy() :正序排序
orderByDesc() :倒序排序
@Test
void test6() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}

代码自动生成器
1.导入依赖
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.0-RC2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!-- Swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2.配置自动生成代码
在Test文件夹中,创建一个新类:cccc
public class ZcCode {
public static void main(String[] args) {
//代码生成器
AutoGenerator mpg = new AutoGenerator();
//.....其余代码
//执行
mpg.execute();
}
}
全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath+"/src/main/java");
gc.setAuthor("MoYu"); //作者名
gc.setOpen(false); //是否打开资源管理器
gc.setFileOverride(false); //是否覆盖
gc.setServiceName("%sService"); //去Service的I前缀
gc.setIdType(IdType.ID_WORKER); //ID类型,设置的为 全局唯一ID
gc.setDateType(DateType.ONLY_DATE); //Date类型
gc.setSwagger2(true); //开启Swagget2
mpg.setGlobalConfig(gc);
包的配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(""); //Controller路径 xxx/实体类名
pc.setParent("com.zc"); //父类文件夹路径
pc.setEntity("entity"); //实体类文件夹
pc.setMapper("mapper"); //mapper文件夹
pc.setService("service"); //service文件夹
pc.setController("controller"); //controller文件夹
mpg.setPackageInfo(pc);
设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/mybatis-plus? useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("148729");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("user"); // 设置要映射的表名,可以多个表一直写 “xxxx”,"xxxx"
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true); // 自动lombok;
// 逻辑删除配置
strategy.setLogicDeleteFieldName("deleted");
// 自动填充配置
TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);
// 乐观锁
strategy.setVersionFieldName("version");strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
运行
这是生成的架构文件夹:

生成之后的代码以及架构都是很规范的,大家可以自己测试一下
个人博客为:
MoYu's HomePage
MoYu's Gitee Blog
Mybatis-plus 下的更多相关文章
- idea插件(mybatis框架下mapper接口快速跳转对应xml文件)亲测好用!
我相信目前在绝大部分公司里,主要使用的框架是S(spring)S(spring MVC)M(mybatis),其中mybatis总体架构是编写mapper接口,框架扫描其对应的mapper.xml文件 ...
- Mybatis(下)
Mybatis(下) 一.MaBatis核心配置文件 Mybatis 中文文档 Mybatis 中文文档 1. properties 定义属性及读取属性文件,取的时候用 $(name) ,name 为 ...
- mybatis框架下解决数据库中表的列的字段名和实体类属性不相同的问题
导包.... 实体类中的属性,getter,setter,tostring,构造等方法就不写了 private int id; private String orderNo; private floa ...
- mybatis框架下使用generator插件自动生成domain/mapping/mapper
手动去创建domain/mapping/mapper费时费力还容易出错,用插件自动生成非常的方便. 这里以MySQL数据库为例,也可以改成Oracle,改成相应的驱动和URL即可. 下载generat ...
- Spring + MyBatis 框架下处理数据库异常
一.概述 使用JDBC API时,很多操作都要声明抛出java.sql.SQLException异常,通常情况下是要制定异常处理策略.而Spring的JDBC模块为我们提供了一套异常处理机制,这套异常 ...
- spring+springMVC+Mybatis架构下采用AbstractRoutingDataSource、atomikos、JTA实现多数据源灵活切换以及分布式事务管理
背景: 1.系统采用SSM架构.需要在10多个MYSQL数据库之间进行切换并对数据进行操作,上篇博文<springMVC+Mybatis(使用AbstractRoutingDataSource实 ...
- MyBatis Generator 下划线转驼峰命名
MyBatis Generator配置文件--指定生成实体类使用实际的表列名作为实体类的属性名 table标签下的设置属性useActualColumnNames用于指定生成实体类时是否使用实际的列名 ...
- 带着新人学springboot的应用03(springboot+mybatis+缓存 下)
springboot+mybatis+缓存,基本的用法想必是会了,现在说一说内部大概的原理. 稍微提一下mybatis,只要导入了mybatis的依赖,那么有个自动配置类就会生效,你可以去mybati ...
- mybatis框架下物理分页的实现(整个工程采用的是springmvc、spring、mybatis框架,数据库是mysql数据库)
(一)关于分页拦截器的简单理解 首先,要开发MyBatis的插件需要实现org.apache.ibatis.plugin.Interceptor接口,这个接口将会要求实现几个方法:intercept( ...
- java mybatis 框架下多种类型的参数传入到xml问题
由于公司要求,最近从.net向java 转,然后过程中遇到各种奇葩问题,特在此随记一番. 场景:一个方法中有两个参数,一个List的集合,一个int 类型的参数,最初我在xml的sql参数,无论定义成 ...
随机推荐
- Dyno-queues 分布式延迟队列 之 生产消费
Dyno-queues 分布式延迟队列 之 生产消费 目录 Dyno-queues 分布式延迟队列 之 生产消费 0x00 摘要 0x01 前情回顾 1.1 设计目标 1.2 选型思路 0x02 产生 ...
- vscode好用插件总结
做个记录:https://blog.csdn.net/xishining/article/details/90819481 1.Auto Rename Tag --自动重命名成对的HTML标记.假如你 ...
- 调度场算法&&中缀表达式=>后缀表达式
#include<stdio.h> #include<string.h> int main(void){ char ch,stro[1001],stack[1001]; int ...
- python:虚拟环境与pip
原生pip镜像下载速度较慢,配置使用国内镜像.这里选择清华镜像,文档地址:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ pip 镜像配置 临时使用: ...
- 《吃透MQ系列》核心基础全在这里了
这是<吃透XXX>技术系列的开篇,这个系列的思路是:先找到每个技术栈最本质的东西,然后以此为出发点,逐渐延伸出其他核心知识.所以,整个系列侧重于思考力的训练,不仅仅是讲清楚 What,而是 ...
- 番外----python入门----pip相关
pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 但是,由于pip使用的pip仓库默认为:http://pypi.python.org/ 是国外的 ...
- docker在vulhub中的使用命令合集
(1)docker ps(查询 docker 进程的所有容器) (2)docker info(查看docker详细信息) (3)service docker start(启动docke ...
- java 给时间增加时间得到一个新的时间(日期)
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd") LocalDate expirationDate String exp ...
- FreeBSD WIFI 配置
ee /boot/ loader.conf ee是个编辑器 中写入 rtwn_usb_load="YES" legal.realtek.license_ack=1 在 /etc/ ...
- SpringBoot自动配置原理源码级别分析
SpringBoot自动配置原理 前言 后面还会讲到SpringBoot自动配置原理,会主要讲解@EnableAutoConfiguratuon注解帮助我们做了什么事情,是如何自动把自动配置类扫描到容 ...