一、项目创立
能够挑选SpringIntializr创立也能够运用Maven办法创立
选好之后点击下一步
这儿只需求选中最基本的依赖项,后续依赖我们会在pom.xml中引进
二、引进依赖
spring Initializr创立项目需求在pom.xml文件的标签中增加如下依赖
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 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.4.2</version>
</dependency>
如果是Maven办法创立项目,若想运用SpringBoot还需增加SpringBoot相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>MybatisPlus-Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MybatisPlus-Demo</name>
<description>MybatisPlus-Demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 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.4.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、创立数据库及引进数据库
1. 创立数据库
若已经有数据库则跳过至步骤二
我这儿运用的是Navicat创立数据库
完成数据库创立后增加数据表
-- 判断要创立的表称号是否已经存在
DROP TABLE IF EXISTS student;
CREATE TABLE student(
id BIGINT(20) NOT NULL COMMENT '主键ID,学号',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '名字',
age INT(11) NULL DEFAULT NULL COMMENT '年纪',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
-- 设置id主键
PRIMARY KEY (id),
-- 在id字段创立一般索引
index(id)
);
-- 刺进数据
INSERT INTO student (id, name, age, email)
VALUES
(1001, 'Jone', 18, 'test1@baomidou.com'),
(1002, 'Jack', 20, 'test2@baomidou.com'),
(1003, 'Tom', 28, 'test3@baomidou.com'),
(1004, 'Sandy', 21, 'test4@baomidou.com'),
(1005, 'Billie', 24, 'test5@baomidou.com');
创立表时具体索引参考->文档地址
2. 引进数据库
2.1 衔接数据库
数据库衔接句子在resource资源目录下的application.properties文件中书写
在MySQL8.0版别后,衔接句子发生了变化(相关文档)。
MySQL8.0版别后的句子书写
# 数据库衔接装备
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisPlus_demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 装备日志 sql句子在履行时不可见,所认为了便于过错排查,需求经过日志打印句子。
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
留意:如果你是5.*数据库版别,就需求将com.mysql.cj.jdbc.Driver改为:com.mysql.jdbc.Driver。
yml文件书写句子
server:
port: 8080
spring:
application:
name: mybatis-plus
datasource:
url: jdbc:mysql://www.xlz.com/mybatis-plus? useEncoding=utf8mb4&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
四、主动创立对应实体类及接口和xml文件
1 在idea中引进数据源
进入数据源后填写相关信息,点击下方测验链接通往后,点击确定按钮
2 运用Mybatis-generator主动创立
在导入完数据源后,找到要映射的数据表。右键挑选mybatis-generator选项
进入mybatis-generate tool,填写对应信息
点击ok后,在指定途径总计生成三个文件(文件因途径不同,生成方位不同)。至此主动装备完成。
\
也能够运用句子生成
代码生成器(旧) | MyBatis-Plus
五、接口测验
一般创立SpringBoot项目中有内置Junit5的,这时能够经过运用快捷键alt+Insert快捷生成测验
书写如下代码,测验是否有数据输出。
@SpringBootTest
class StudentMapperTest{
@Autowired
StudentMapper studentMapper;
@Test
void selectByPrimaryKey (){
Student student = studentMapper.selectByPrimaryKey (1001L);
System.out.println (student);
}
}
六、日志打印
日志首要体现在当运行程序的时分,将会在控制台将会打印日志信息
运用在 application.xml 文件中加入
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
\
七、MybatisPlus常用注解
7.1 @MapperScan
效果: 为了集成Spring而写的注解,该注解首要是扫描某个包目录下的Mapper,将Mapper接口类交给Spring进行管理。
运用方位: 在Springboot启动类上增加
示例:
@SpringBootApplication
@MapperScan("com.example.mybatisplusdemo")
public class MybatisPlusDemoApplication{
public static void main (String[] args){
SpringApplication.run (MybatisPlusDemoApplication.class , args);
}
}
7.2 @Mapper
效果: 在接口类上增加了@Mapper,在运行时会生成相应的接口完成类,这个要一个一个去加,不如用@MapperScan全部扫描增加
增加方位: 接口类上面
示例:
@Mapper
public interface StudentMapper extends BaseMapper<Student>{
...
}
7.3 @TableName
效果: 用于标识实体类对应的表名
增加方位: 实体类之上
示例:
@TableName("user")
public class User {
...
}
7.4 @Data
效果: 简化开发,运用这个注解,就能够省略getter()、setter()、toString()、重写该类的equals()和hashCode()办法
运用方位: 实体类之上
示例:
@Data
public class User {
...
}
7.5 @TableId
效果:
用于标识主键列,MyBatis-Plus在完成增删改查时,会默许将id作为主键列,在刺进数据时,若对应字段为空,则默许运用基于雪花算法的战略生成id。
当运用@TableId(value = “id”)句子时,若实体类和表中表明主键的不是id,而是其他字段,例如代码中的uid,MyBatis-Plus会主动识别uid为主键列,不然就会报错。
当运用@TableId(value = “id”,type = IdType.AUTO)句子时,代表着运用数据库的自增战略,留意,该类型请保证数据库设置了id自增,不然无效!
@TableId的功用,也能够写在application.yml装备文件中,装备如下:
mybatis-plus:
global-config:
banner: false
db-config:
# 装备MyBatis-Plus操作表的默许前缀
table-prefix: "t_"
# 装备MyBatis-Plus的主键战略
id-type: auto
# 装备MyBatis日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
运用方位: 主键映射字段之上
示例:
/**
* student
* @author
*/
@Data
@TableName("mybatisplus_demo")
public class Student implements Serializable {
/**
* 主键ID,学号
*/
@TableId(value = "id",type = IdType.AUTO )
private Long id;
}
7.6 @TableField
效果: 用于保证实体类中的特点名与数据表中字段名一致。
注: 若实体类中的特点运用的是驼峰命名风格,而表中的字段运用的是下划线命名风格。
例如实体类特点userName,表中字段user_name,此刻MyBatis-Plus会主动将下划线命名风格转化为驼峰命名风格
若实体类中的特点和表中的字段不满意上述条件,例如实体类特点name,表中字段username,此刻需求在实体类特点上运用@TableField(“username”)设置特点所对应的字段名
运用方位: 实体类除主键字段外其他映射字段之上
@Data
@TableName("mybatisplus_demo")
public class Student implements Serializable {
/**
* 主键ID,学号
*/
@TableId(value = "id",type = IdType.AUTO )
private Long id;
/**
* 名字
*/
@TableField(value = "name")
private String name;
}
7.7 @TableLogic
效果: 逻辑删去,并不会永久删去数据,实体类加上这个注解再履行删去办法的时分会变成修正。前台再依据所加注解字段进行显隐即可到达逻辑删去效果。
@Data
@TableName("mybatisplus_demo")
public class Student implements Serializable {
/**
* 主键ID,学号
*/
@TableId(value = "id",type = IdType.AUTO )
private Long id;
/*
删去标识
@TableLogic(value=“原值”,delval=“修正值”)
注解参数
value = “” 未删去的值,默许值为0
delval = “” 删去后的值,默许值为1
*/
@TableField(value = "del")
@TableLogic(value = "0",delval = "1")
private Integer del;
}
\
调用BaseMapper的deleteById(id)或许调用IService的removeById(id)
效果:
没有@TableLogic注解调用deleteById/removeById,直接删去数据。
SQL:delete from table where id = 1
有注解走Update办法
SQL:Update table set isDelete = 1 where id = 1
\
7.8 @Param
效果:用于标识传入参数的称号
运用方位:形参列表
示例:
@Mapper
public interface StudentMapper extends BaseMapper<Student>{
int insert(@Param (value = "record") Student record);
}
与之对应的XML文件就能够运用${record}变量
7.9 @Version
效果: 能够在实体 bean 中运用@Version 注解,经过这种办法可增加对乐观锁定的支撑,一个类中只能有一个@Version注解。
不可选特点 | String | ||
---|---|---|---|
可选特点 | int | Integer | Long |
@Data
@TableName("mybatisplus_demo")
public class Student implements Serializable {
/**
* 主键ID,学号
*/
@TableId(value = "id",type = IdType.AUTO )
private Long id;
/**
* 版别号
*/
@Version (value = "version")
private int version;
}
7.10 @EnumValue 和 JsonValue
@JsonValue
能够用在get办法或许特点字段上,一个类只能用一个,当加上@JsonValue注解时,该类的json化结果,只有这个get办法的回来值,而不是这个类的特点键值对.
事例: 男,女,后台存储的是1,2,前端展现的是男女
第一步:
创立枚举类,在需求存储数据库的特点上增加 @EnumValue注解,在需求前端展现的特点上增加 @JsonValue注解;
package com.demo.mybatisplus.constant;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
public enum SexEnum {
MAN(1, "男"),
WOMAN(2, "女");
@EnumValue
private Integer key;
@JsonValue
private String display;
SexEnum(Integer key, String display) {
this.key = key;
this.display = display;
}
public Integer getKey() {
return key;
}
public String getDisplay() {
return display;
}
}
第二步:
application.properties文件里增加装备,界说扫描枚举类的包途径;
#装备枚举 支撑通配符 * 或许 ; 切割
mybatis-plus.type-enums-package=com.demo.mybatisplus.constant
#mybatis-plus.configuration.default-enum-type-handler=org.apache.ibatis.type.EnumOrdinalTypeHandler
application.yml则装备
#装备枚举 支撑通配符 * 或许 ; 切割
mybatis-plus:
type-enums-package: com.demo.mybatisplus.constant
configuration:
default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
第三步:
pojo中的sex特点设置为枚举SexEnum;
@ApiModelProperty(value = "性别")
@TableField("sex")
private SexEnum sex;
测验:
@Test
public void insert() {
UserInfo userInfo = new UserInfo();
userInfo.setAge(22);
userInfo.setName("李四");
userInfo.setSex(SexEnum.WOMAN);
userInfoMapper.insert(userInfo);
System.out.println(userInfo);
}
留意事项:
- @EnumValue符号的枚举类特点的类型要和数据库字段的类型对应,不然在查询数据的时分无法转化为枚举类型,并显现为null;
- 如果查询的时分,数据库字段的值匹配不到枚举,程序运行时并不会报错,而是显现为null;
- 在保存的时分,前端需求传递@JsonValue符号的枚举类特点的值,即”男/女”;由于Enum的特点ordinal(int),在测验过程中,传枚举值在枚举类中的界说次序(或许称为索引,次序从0开始),也能够转换为相应的枚举值,比如:上面界说的SexEnum枚举,前端传0或许”0″,会转换成MAN,传1或许”1″会转换成WOMAN;传其他值会报反常:com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type com.demo.mybatisplus.constant.SexEnum from String “3”: not one of the values accepted for Enum class: [女, 男]或com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type com.demo.mybatisplus.constant.SexEnum from number 3: index value outside legal index range [0..2];
八、MybatisPlus常用办法
8.1 条件结构器 Wrapper
\
介绍 :
- 上图绿色框为抽象类abstract
- 蓝色框为正常class类,可new目标
- 黄色箭头指向为父子类联系,箭头指向为父类
wapper介绍 :
- Wrapper : 条件结构抽象类,最顶端父类,抽象类中供给4个办法西面贴源码展现
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- AbstractLambdaWrapper : Lambda 语法运用 Wrapper一致处理解析 lambda 获取 column。
- LambdaQueryWrapper :看称号也能明白就是用于Lambda语法运用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- QueryWrapper : Entity 目标封装操作类,不是用lambda语法
- UpdateWrapper : Update 条件封装,用于Entity目标更新操作
\
常用条件
比较巨细: (=,<>,>,>=,<,<=)
eq(R column, Object val); // 等价于 =,例: eq("name", "老王") ---> name = '老王'
ne(R column, Object val); // 等价于 <>,例: ne("name", "老王") ---> name <> '老王'
gt(R column, Object val); // 等价于 >,例: gt("name", "老王") ---> name > '老王'
ge(R column, Object val); // 等价于 >=,例: ge("name", "老王") ---> name >= '老王'
lt(R column, Object val); // 等价于 <,例: lt("name", "老王") ---> name < '老王'
le(R column, Object val); // 等价于 <=,例: le("name", "老王") ---> name <= '老王'
规模:(between、not between、in、not in)
between(R column, Object val1, Object val2); // 等价于 between a and b, 例: between("age", 18, 30) ---> age between 18 and 30
notBetween(R column, Object val1, Object val2); // 等价于 not between a and b, 例: notBetween("age", 18, 30) ---> age not between 18 and 30
in(R column, Object... values); // 等价于 字段 IN (v0, v1, ...),例: in("age",{1,2,3}) ---> age in (1,2,3)
notIn(R column, Object... values); // 等价于 字段 NOT IN (v0, v1, ...), 例: notIn("age",{1,2,3}) ---> age not in (1,2,3)
inSql(R column, Object... values); // 等价于 字段 IN (sql 句子), 例: inSql("id", "select id from table where id < 3") ---> id in (select id from table where id < 3)
notInSql(R column, Object... values); // 等价于 字段 NOT IN (sql 句子)
模糊匹配:(like)
// 等价于 LIKE '%值%',例: like("name", "王") ---> name like '%王%'
like(R column, Object val);
// 等价于 NOT LIKE '%值%',例: notLike("name", "王") ---> name not like '%王%'
notLike(R column, Object val);
// 等价于 LIKE '%值',例: likeLeft("name", "王") ---> name like '%王'
likeLeft(R column, Object val);
// 等价于 LIKE '值%',例: likeRight("name", "王") ---> name like '王%'
likeRight(R column, Object val);
空值比较:(isNull、isNotNull)
isNull(R column); // 等价于 IS NULL,例: isNull("name") ---> name is null
isNotNull(R column); // 等价于 IS NOT NULL,例: isNotNull("name") ---> name is not null
分组、排序:(group、having、order)
groupBy(R... columns); // 等价于 GROUP BY 字段, ..., 例: groupBy("id", "name") ---> group by id,name
orderByAsc(R... columns); // 等价于 ORDER BY 字段, ... ASC, 例: orderByAsc("id", "name") ---> order by id ASC,name ASC
orderByDesc(R... columns); // 等价于 ORDER BY 字段, ... DESC, 例: orderByDesc("id", "name") ---> order by id DESC,name DESC
having(String sqlHaving, Object... params); // 等价于 HAVING ( sql句子 ), 例: having("sum(age) > {0}", 11) ---> having sum(age) > 11
拼接、嵌套 sql:(or、and、nested、apply)
or(); // 等价于 a or b, 例:eq("id",1).or().eq("name","老王") ---> id = 1 or name = '老王'
or(Consumer<Param> consumer); // 等价于 or(a or/and b),or 嵌套。例: or(i -> i.eq("name", "李白").ne("status", "活着")) ---> or (name = '李白' and status <> '活着')
and(Consumer<Param> consumer); // 等价于 and(a or/and b),and 嵌套。例: and(i -> i.eq("name", "李白").ne("status", "活着")) ---> and (name = '李白' and status <> '活着')
nested(Consumer<Param> consumer); // 等价于 (a or/and b),一般嵌套。例: nested(i -> i.eq("name", "李白").ne("status", "活着")) ---> (name = '李白' and status <> '活着')
apply(String applySql, Object... params); // 拼接sql(若不运用 params 参数,或许存在 sql 注入),例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08") ---> date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
last(String lastSql); // 无视优化规则直接拼接到 sql 的最终,或许存若在 sql 注入。
exists(String existsSql); // 拼接 exists 句子。例: exists("select id from table where age = 1") ---> exists (select id from table where age = 1)
QueryWrapper 条件:
select(String... sqlSelect); // 用于界说需求回来的字段。例: select("id", "name", "age") ---> select id, name, age
select(Predicate<TableFieldInfo> predicate); // Lambda 表达式,过滤需求的字段。
lambda(); // 回来一个 LambdaQueryWrapper
UpdateWrapper 条件:
set(String column, Object val); // 用于设置 set 字段值。例: set("name", null) ---> set name = null
etSql(String sql); // 用于设置 set 字段值。例: setSql("name = '老李头'") ---> set name = '老李头'
lambda(); // 回来一个 LambdaUpdateWrapper```
8.2 Page<>类
特点源码
public class Page<T> implements IPage<T> {
private static final long serialVersionUID = 8545996863226528798L;
// 用来存放查询出来的数据
protected List<T> records;
// 回来记载的总数
protected long total;
// 每页显现条数,默许 10
protected long size;
// 当前页,默许1
protected long current;
// 排序字段信息
protected List<OrderItem> orders;
// 主动优化 COUNT SQL,默许true
protected boolean optimizeCountSql;
// 是否进行 count 查询,默许true
protected boolean isSearchCount;
// 是否射中count缓存,默许false
protected boolean hitCount;
protected String countId;
protected Long maxLimit;
}
\
8.3 mapper层(接口界说层)能够用BaseMapper<>
例如
内部办法总览
办法具体介绍
汇总
【增加数据:(增)】
int insert(T entity); // 刺进一条记载
注:
T 表明恣意实体类型
entity 表明实体目标
【删去数据:(删)】
int deleteById(Serializable id); // 依据主键 ID 删去
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 依据 map 界说字段的条件删去
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); // 依据实体类界说的 条件删去目标
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 进行批量删去
注:
id 表明 主键 ID
columnMap 表明表字段的 map 目标
wrapper 表明实体目标封装操作类,能够为 null。
idList 表明 主键 ID 调集(列表、数组),不能为 null 或 empty
【修正数据:(改)】
int updateById(@Param(Constants.ENTITY) T entity); // 依据 ID 修正实体目标。
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); // 依据 updateWrapper 条件修正实体目标
注:
update 中的 entity 为 set 条件,能够为 null。
updateWrapper 表明实体目标封装操作类(能够为 null,里边的 entity 用于生成 where 句子)
【查询数据:(查)】
T selectById(Serializable id); // 依据 主键 ID 查询数据
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 进行批量查询
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 依据表字段条件查询
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 依据实体类封装目标 查询一条记载
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询记载的总条数
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询一切记载(回来 entity 调集)
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询一切记载(回来 map 调集)
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询一切记载(但只保存第一个字段的值)
<E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询一切记载(回来 entity 调集),分页
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查询一切记载(回来 map 调集),分页
注:
queryWrapper 表明实体目标封装操作类(能够为 null)
page 表明分页查询条件
增
刺进一条记载
int insert(T entity);
@Test
public void Insert_T(){
Student student=new Student ();
student.setAge (18);
student.setEmail ("165@163.com");
student.setName ("柳宗元");
int insert = studentMapper.insert (student);
System.out.println (insert==0?"刺进失利":"刺进成功");
}
删
依据主键id删去
int deleteById(Serializable id);
依据 map 界说字段的条件删去
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
@Test
public void deleteByMap_T(){
// 通用删去操作 deleteByMap map要写列名条件 不能是实体特点名
Map<String, Object> map=new HashMap<> ();
// 删去name列中,内容为Jone的记载
map.put ("name","Jone");
int count = studentMapper.deleteByMap (map);
System.out.println (count==0?"删去失利":"删去成功");
}
依据实体类界说的条件删去目标
// 依据实体类界说的 条件删去目标
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
进行批量删去
// 进行批量删去
// idList 表明主键 ID 调集(列表、数组),不能为 null 或 empty
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
@Test
public void deleteBatchIds_T(){
int count = studentMapper.deleteBatchIds (Stream.of (1012,1013).collect(Collectors.toList ()));
System.out.println (count==0?"删去失利":"删去成功");
}
改
依据 ID 修正实体目标。
int updateById(@Param(Constants.ENTITY) T entity);
@Test
public void updateByid_T(){
Student student=new Student ();
student.setAge (19);
student.setEmail ("1623@163.com");
student.setName ("白居易修正测验");
student.setId (1030L);
int count = studentMapper.updateById (student);
System.out.println (count==0?"修正失利":"修正成功");
}
依据 updateWrapper 条件修正实体目标
\
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
查
依据 主键 ID 查询数据
T selectById(Serializable id);
//示例
@Test
public void selectById_T(){
Student student = studentMapper.selectById (1030L);
System.out.println (student);
}
进行批量查询
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
@Test
public void selectBatchIds_T(){
List<Student> students = studentMapper.selectBatchIds (Stream.of (1012 , 1013).collect (Collectors.toList ()));
students.forEach (System.out :: println);
}
/*
* ==> Preparing: SELECT id,name,age,email FROM student WHERE id IN ( ? , ? )
* ==> Parameters: 1012(Integer), 1013(Integer)
* <== Columns: id, name, age, email
* <== Row: 1012, 柳宗元, 18, 165@163.com
* <== Row: 1013, 柳宗元, 18, 165@163.com
* <== Total: 2
*/
依据表字段称号查询
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
@Test
public void selectByMap(){
HashMap<String, Object> map = new HashMap<> ();
map.put ("name","柳宗元");
map.put ("age","18");
List<Student> students = studentMapper.selectByMap (map);
}
/*
* ==> Preparing: SELECT id,name,age,email FROM student WHERE name = ? AND age = ?
* ==> Parameters: 柳宗元(String), 18(String)
* <== Columns: id, name, age, email
* <== Row: 1007, 柳宗元, 18, 165@163.com
* <== Row: 1008, 柳宗元, 18, 165@163.com
* <== Row: 1009, 柳宗元, 18, 165@163.com
* <== Total: 3
*/
查询记载总条数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void selectCount_T(){
// queryWrapper 表明实体目标封装操作类(能够为 null)
Integer integer = studentMapper.selectCount (null);
System.out.println (integer);
}
/*
* ==> Preparing: SELECT COUNT( * ) FROM student
* ==> Parameters:
* <== Columns: COUNT( * )
* <== Row: 54
* <== Total: 1
*/
查询一切记载 List
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/*
* 查询一切name=柳宗元的记载
*/
@Test
public void selectList_T(){
List<Student> students = studentMapper.selectList (new QueryWrapper<Student> ().eq ("name" , "柳宗元").);
students.forEach (System.out :: println);
}
/*
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name = ?)
* ==> Parameters: 柳宗元(String)
* <== Columns: id, name, age, email
* <== Row: 1007, 柳宗元, 18, 165@163.com
* <== Row: 1008, 柳宗元, 18, 165@163.com
* <== Row: 1009, 柳宗元, 18, 165@163.com
* <== Row: 1010, 柳宗元, 18, 165@163.com
* <== Total: 4
*/
查询一切记载 Map
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void selectMaps_T(){
List<Map<String, Object>> maps = studentMapper.selectMaps (new QueryWrapper<Student> ().eq ("name" , "柳宗元"));
maps.forEach (item-> {
item.forEach ((key, value)-> System.out.println (key+" --- "+value));
System.out.println ();
});
/*
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name = ?)
* ==> Parameters: 柳宗元(String)
* <== Columns: id, name, age, email
* <== Row: 1007, 柳宗元, 18, 165@163.com
* <== Row: 1008, 柳宗元, 18, 165@163.com
* <== Row: 1009, 柳宗元, 18, 165@163.com
* <== Row: 1010, 柳宗元, 18, 165@163.com
* <== Total: 4
*/
控制台打印信息
System.out.println (key+” — “+value));
/*
* name ---- 柳宗元
* id ---- 1007
* age ---- 18
* email ---- 165@163.com
*
* name ---- 柳宗元
* id ---- 1008
* age ---- 18
* email ---- 165@163.com
*
* name ---- 柳宗元
* id ---- 1009
* age ---- 18
* email ---- 165@163.com
*/
查询一切记载(但只保存第一个字段的值)
建表时,一般 id 作为首字段。能够回来一切id
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void selectObjs_T(){
List<Object> objects = studentMapper.selectObjs (new QueryWrapper<Student> ().likeRight ("name" , "白居易"));
objects.forEach (System.out :: println);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: id, name, age, email
* <== Row: 1027, 白居易0, 19, 1623@163.com
* <== Row: 1028, 白居易1, 19, 1623@163.com
* <== Row: 1029, 白居易2, 19, 1623@163.com
* <== Row: 1030, 白居易修正测验, 19, 1623@163.com
* <== Row: 1031, 白居易4, 19, 1623@163.com
* <== Row: 1032, 白居易5, 19, 1623@163.com
* <== Row: 1033, 白居易6, 19, 1623@163.com
* <== Total: 7
*/
控制台打印数据
objects.forEach (System.out :: println);
/**
* 1027
* 1028
* 1029
* 1030
* 1031
* 1032
* 1033
*/
查询一切记载 分页 List
<E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
Page<Student> studentPage = studentMapper.selectPage (new Page<> (2 , 4) ,
new QueryWrapper<Student> ().likeRight ("name" , "白居易"));
查询一切记载 分页 Map
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void selectMapsPage_T(){
Page<Map<String, Object>> mapPage = studentMapper.selectMapsPage (new Page<> (2 , 4) ,new QueryWrapper<Student> ().likeRight ("name" , "白居易"));
}
8.4 service层(接口办法)能够用IService<>
介绍:
运用 代码生成器 生成的 service 接口中,其继承了 IService 接口。
IService 内部进一步封装了 BaseMapper 接口的办法(当然也供给了更具体的办法)。
运用时,能够经过 生成的 mapper 类进行 CRUD 操作,也能够经过 生成的 service 的完成类进行 CRUD 操作。(当然,自界说代码履行也可)
此处简略介绍一下 IService 中封装的常用办法。
内部办法总览
办法具体介绍
汇总
【增加数据:(增)】
default boolean save(T entity); // 调用 BaseMapper 的 insert 办法,用于增加一条数据。
boolean saveBatch(Collection<T> entityList, int batchSize); // 批量刺进数据
注:
entityList 表明实体目标调集
batchSize 表明一次批量刺进的数据量,默许为 1000
【增加或修正数据:(增或改)】
boolean saveOrUpdate(T entity); // id 若存在,则修正, id 不存在则新增数据
default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper); // 先依据条件测验更新,然后再履行 saveOrUpdate 操作
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize); // 批量刺进并修正数据
【删去数据:(删)】
default boolean removeById(Serializable id); // 调用 BaseMapper 的 deleteById 办法,依据 id 删去数据。
default boolean removeByMap(Map<String, Object> columnMap); // 调用 BaseMapper 的 deleteByMap 办法,依据 map 界说字段的条件删去
default boolean remove(Wrapper<T> queryWrapper); // 调用 BaseMapper 的 delete 办法,依据实体类界说的 条件删去目标。
default boolean removeByIds(Collection<? extends Serializable> idList); // 用 BaseMapper 的 deleteBatchIds 办法, 进行批量删去。
【修正数据:(改)】
default boolean updateById(T entity); // 调用 BaseMapper 的 updateById 办法,依据 ID 挑选修正。
default boolean update(T entity, Wrapper<T> updateWrapper); // 调用 BaseMapper 的 update 办法,依据 updateWrapper 条件修正实体目标。
boolean updateBatchById(Collection<T> entityList, int batchSize); // 批量更新数据
【查找数据:(查)】
default T getById(Serializable id); // 调用 BaseMapper 的 selectById 办法,依据 主键 ID 回来数据。
default List<T> listByIds(Collection<? extends Serializable> idList); // 调用 BaseMapper 的 selectBatchIds 办法,批量查询数据。
default List<T> listByMap(Map<String, Object> columnMap); // 调用 BaseMapper 的 selectByMap 办法,依据表字段条件查询
default T getOne(Wrapper<T> queryWrapper); // 回来一条记载(实体类保存)。
Map<String, Object> getMap(Wrapper<T> queryWrapper); // 回来一条记载(map 保存)。
default int count(Wrapper<T> queryWrapper); // 依据条件回来 记载数。
default List<T> list(); // 回来一切数据。
default List<T> list(Wrapper<T> queryWrapper); // 调用 BaseMapper 的 selectList 办法,查询一切记载(回来 entity 调集)。
default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 调用 BaseMapper 的 selectMaps 办法,查询一切记载(回来 map 调集)。
default List<Object> listObjs(); // 回来全部记载,但只回来第一个字段的值。
default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper); // 调用 BaseMapper 的 selectPage 办法,分页查询
default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper); // 调用 BaseMapper 的 selectMapsPage 办法,分页查询
注:
get 用于回来一条记载。
list 用于回来多条记载。
count 用于回来记载总数。
page 用于分页查询。
【链式调用:】
default QueryChainWrapper<T> query(); // 一般链式查询
default LambdaQueryChainWrapper<T> lambdaQuery(); // 支撑 Lambda 表达式的修正
default UpdateChainWrapper<T> update(); // 一般链式修正
default LambdaUpdateChainWrapper<T> lambdaUpdate(); // 支撑 Lambda 表达式的修正
注:
query 表明查询
update 表明修正
Lambda 表明内部支撑 Lambda 写法。
形如:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);
增
刺进一条记载
default boolean save(T entity);
@Test
public void save_T(){
Student stu=new Student ();
stu.setName ("李清照");
stu.setAge (21);
stu.setEmail ("ssm@163.com");
// 调用 BaseMapper 的 insert 办法,用于增加一条数据。
boolean save = iStudentService.save (stu);
System.out.println (save);
}
/**
* ==> Preparing: INSERT INTO student ( name, age, email ) VALUES ( ?, ?, ? )
* ==> Parameters: 李清照(String), 21(Integer), ssm@163.com(String)
* <== Updates: 1
*/
刺进多条数据
boolean saveBatch(Collection<T> entityList, int batchSize);
@Test
public void saveBatch(){
List<Student> students = new ArrayList<>();
students.add (new Student ("刘咏",22,"123@123.com"));
students.add (new Student ("刘咏",22,"234@123.com"));
students.add (new Student ("刘咏",22,"345@123.com"));
students.add (new Student ("刘咏",22,"456@123.com"));
// entityList 表明实体目标调集
// batchSize 表明一次批量刺进的数据量,默许为 1000
boolean b = iStudentService.saveBatch (students);
System.out.println (b);
}
/*
* ==> Preparing: INSERT INTO student ( name, age, email ) VALUES ( ?, ?, ? )
* ==> Parameters: 刘咏(String), 22(Integer), 123@123.com(String)
* ==> Parameters: 刘咏(String), 22(Integer), 234@123.com(String)
* ==> Parameters: 刘咏(String), 22(Integer), 345@123.com(String)
* ==> Parameters: 刘咏(String), 22(Integer), 456@123.com(String)
*/
删
依据主键删去数据
// 调用 BaseMapper 的 deleteById 办法,依据 id 删去数据。
default boolean removeById(Serializable id);
@Test
public void removeById_T(){
// 调用 BaseMapper 的 deleteById 办法,依据 id 删去数据。
boolean start = iStudentService.removeById (1062);
System.out.println (start);
}
依据map界说条件删去记载
default boolean removeByMap(Map<String, Object> columnMap);
@Test
public void removeByMap_T(){
boolean b = iStudentService.removeByMap (
// 删去name = 刘咏 , id = 1059 的记载
// ImmutableMap 生成的Map为不可变Map
ImmutableMap.of ("name" , "刘咏" , "id" , "1059")
);
System.out.println (b);
}
/*
* ==> Preparing: DELETE FROM student WHERE name = ? AND id = ?
* ==> Parameters: 刘咏(String), 1059(String)
* <== Updates: 1
*/
删去多条数据
default boolean removeByIds(Collection<? extends Serializable> idList);
@Test
public void removeByIds_T(){
// 用 BaseMapper 的 deleteBatchIds 办法, 进行批量删去。
boolean start = iStudentService.removeByIds (Stream.of (1065,1064,1063,1062).collect(Collectors.toList()));
System.out.println (start);
}
/*
* ==> Preparing: DELETE FROM student WHERE id IN ( ? , ? , ? , ? )
* ==> Parameters: 1065(Integer), 1064(Integer), 1063(Integer), 1062(Integer)
* <== Updates: 3
*/
依据自界说条件删去数据
default boolean remove(Wrapper<T> queryWrapper);
@Test
public void remove_T(){
boolean start = iStudentService.remove (new QueryWrapper<Student> ().eq ("id" , 1049));
System.out.println (start);
}
/**
* ==> Preparing: DELETE FROM student WHERE (id = ?)
* ==> Parameters: 1049(Integer)
* <== Updates: 1
*/
改
依据 主键 修正记载
default boolean updateById(T entity);
@Test
public void updateById_T(){
Student student = new Student ();
student.setId (1024L);
student.setName ("09:26修正测验");
// 调用 BaseMapper 的 updateById 办法,依据 ID 挑选修正。
boolean b = iStudentService.updateById (student);
System.out.println (b);
/*
* ==> Preparing: UPDATE student SET name=? WHERE id=?
* ==> Parameters: 09:26修正测验(String), 1024(Long)
* <== Updates: 1
*/
}
修正多条数据
boolean updateBatchById(Collection<T> entityList, int batchSize);
// 批量更新数据
@Test
public void updateBatchById_T(){
Student student = new Student ();
student.setId (1025L);
student.setEmail ("369@124");
student.setAge (16);
student.setName ("updateBatchById_T() 修正测验");
Student student1 = new Student ();
student1.setId (1026L);
student1.setEmail ("369@124");
student1.setAge (16);
student1.setName ("updateBatchById_T() 修正测验");
Student student2 = new Student ();
student2.setId (1027L);
student2.setEmail ("369@124");
student2.setAge (16);
student2.setName ("updateBatchById_T() 修正测验");
List<Student> students = new ArrayList<> ();
students.add(student2);
students.add (student1);
students.add (student);
boolean b = iStudentService.updateBatchById (students);
System.out.println (b);
}
/*
* ==> Preparing: UPDATE student SET name=?, age=?, email=? WHERE id=?
* ==> Parameters: updateBatchById_T() 修正测验(String), 16(Integer), 369@124(String), 1027(Long)
* ==> Parameters: updateBatchById_T() 修正测验(String), 16(Integer), 369@124(String), 1026(Long)
* ==> Parameters: updateBatchById_T() 修正测验(String), 16(Integer), 369@124(String), 1025(Long)
*/
依据自界说条件修正
default boolean update(T entity, Wrapper<T> updateWrapper);
// 调用 BaseMapper 的 update 办法,依据 updateWrapper 条件修正实体目标。
@Test
public void update(){
Student student = new Student ();
student.setId (1027L);
student.setEmail ("369@124");
student.setAge (16);
student.setName ("update() 修正测验");
boolean b = iStudentService.update (student , new UpdateWrapper<Student> ().eq ("id" , "1027"));
System.out.println (b);
}
/*
* ==> Preparing: UPDATE student SET name=?, age=?, email=? WHERE (id = ?)
* ==> Parameters: update() 修正测验(String), 16(Integer), 369@124(String), 1027(String)
* <== Updates: 1
*/
查
依据主键查询
default T getById(Serializable id);
@Test
public void getById_T(){
System.out.println (iStudentService.getById (1034L));
}
/*
* ==> Preparing: SELECT id,name,age,email FROM student WHERE id=?
* ==> Parameters: 1034(Long)
* <== Columns: id, name, age, email
* <== Row: 1034, 白居易7, 19, 1623@163.com
* <== Total: 1
*/
依据主键批量查询
paramType :List
return :List
default List<T> listByIds(Collection<? extends Serializable> idList);
// 调用 BaseMapper 的 selectBatchIds 办法,批量查询数据。
@Test
public void listByIds_T(){
iStudentService.listByIds (Stream.of (1034L,1035L,1036L).collect(Collectors.toList()))
.forEach (System.out :: println);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE id IN ( ? , ? , ? )
* ==> Parameters: 1034(Long), 1035(Long), 1036(Long)
* <== Columns: id, name, age, email
* <== Row: 1034, 白居易7, 19, 1623@163.com
* <== Row: 1035, 白居易8, 19, 1623@163.com
* <== Row: 1036, 白居易9, 19, 1623@163.com
* <== Total: 3
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@554188ac]
* Student [Hash = 240195991, id=1034, name=白居易7, age=19, email=1623@163.com, serialVersionUID=1]
* Student [Hash = 240226743, id=1035, name=白居易8, age=19, email=1623@163.com, serialVersionUID=1]
* Student [Hash = 240257495, id=1036, name=白居易9, age=19, email=1623@163.com, serialVersionUID=1]
*/
依据Map条件查询
paramType :Map
return :List
default List<T> listByMap(Map<String, Object> columnMap);
// 调用 BaseMapper 的 selectByMap 办法,依据表字段条件查询
@Test
public void listByMap_T(){
iStudentService.listByMap (
ImmutableMap.of ("name","柳宗元","age","18")
).forEach (System.out :: println);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE name = ? AND age = ?
* ==> Parameters: 柳宗元(String), 18(String)
* <== Columns: id, name, age, email
* <== Row: 1007, 柳宗元, 18, 165@163.com
* <== Row: 1008, 柳宗元, 18, 165@163.com
* <== Row: 1009, 柳宗元, 18, 165@163.com
* ......
* <== Total: 17
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@553da911]
* Student [Hash = 374796304, id=1007, name=柳宗元, age=18, email=165@163.com, serialVersionUID=1]
* Student [Hash = 374826095, id=1008, name=柳宗元, age=18, email=165@163.com, serialVersionUID=1]
* Student [Hash = 374855886, id=1009, name=柳宗元, age=18, email=165@163.com, serialVersionUID=1]
*/
查找首条记载
default T getOne(Wrapper<T> queryWrapper);
public T getOne(Wrapper<T> queryWrapper, boolean throwEx);
//即getOne办法默许throwEx参数为true
default T getOne(Wrapper<T> queryWrapper) {
return this.getOne(queryWrapper, true);
}
//由回来值可知,
//当传入布尔值为true时 , 调用baseMapper.selectOne()办法。即当回来记载数不为 1 时,抛出反常。
//当传入布尔值为false时, 则取首条记载
public T getOne(Wrapper<T> queryWrapper, boolean throwEx) {
return throwEx ? this.baseMapper.selectOne(queryWrapper) : SqlHelper.getObject(this.log, this.baseMapper.selectList(queryWrapper));
}
@Test
public void getOne_T(){
//回来数据不唯一,抛出反常
System.out.println (iStudentService.getOne (new QueryWrapper<Student> ().likeRight ("name" , "白居易") ));
/**
*org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 28
*/
}
@Test
public void getOne_T(){
System.out.println (iStudentService.getOne (new QueryWrapper<Student> ().likeRight ("name" , "白居易") , false));
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: id, name, age, email
* <== Row: 1028, 白居易1, 19, 1623@163.com
* .....
* <== Row: 1055, 白居易28, 19, 1623@163.com
* <== Row: 1056, 白居易29, 19, 1623@163.com
* <== Total: 28
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@340d6d89]
* Warn: execute Method There are 28 results.
* Student [Hash = 240011479, id=1028, name=白居易1, age=19, email=1623@163.com, serialVersionUID=1]
*/
}
查找首条记载 Map
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 回来一条记载(map 保存)。
@Test
public void getMap_T(){
iStudentService.getMap (new QueryWrapper<Student> ().likeRight ("name" , "白居易")).forEach (
(key,value)->{
System.out.println (key+"---"+value);
}
);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: id, name, age, email
* <== Row: 1028, 白居易1, 19, 1623@163.com
* <== Row: 1029, 白居易2, 19, 1623@163.com
* <== Row: 1030, 白居易修正测验, 19, 1623@163.com
* ......
* <== Total: 28
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@43cb5f38]
* Warn: execute Method There are 28 results.
* name---白居易1
* id---1028
* age---19
* email---1623@163.com
*/
回来记载总数(Count)
default int count(Wrapper<T> queryWrapper);
@Test
public void count_T(){
int count = iStudentService.count (new QueryWrapper<Student> ().likeRight ("name" , "白居易"));
System.out.println (count);
}
/**
* ==> Preparing: SELECT COUNT( * ) FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: COUNT( * )
* <== Row: 28
* <== Total: 1
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2cca611f]
* 28
*/
回来一切记载 List
default List<T> list();
@Test
public void List_T(){
iStudentService.list ().forEach (System.out :: println);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student
* ==> Parameters:
* <== Columns: id, name, age, email
*/
有条件的查询记载 List
default List<T> list(Wrapper<T> queryWrapper);
//调用 BaseMapper 的 selectList 办法,查询一切记载(回来 entity 调集)。
@Test
public void Lists_T(){
iStudentService.list (new QueryWrapper<Student> ().likeRight ("name" , "白居易"))
.forEach (System.out :: println);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: id, name, age, email
* <== Row: 1028, 白居易1, 19, 1623@163.com
* <== Row: 1029, 白居易2, 19, 1623@163.com
* ......
* <== Total: 28
*/
回来一切记载 Map
default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 调用 BaseMapper 的 selectMaps 办法,查询一切记载(回来 map 调集)。
@Test
public void listMaps_T(){
iStudentService.listMaps (new QueryWrapper<Student> ().likeRight ("name" , "白居易"))
.forEach (item->item.forEach ((key,value)->{
System.out.println (key+" --- "+value);
}));
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: id, name, age, email
* <== Row: 1028, 白居易1, 19, 1623@163.com
* <== Row: 1029, 白居易2, 19, 1623@163.com
* <== Row: 1030, 白居易修正测验, 19, 1623@163.com
* ......
* <== Total: 28
* name --- 白居易1
* id --- 1028
* age --- 19
* email --- 1623@163.com
* name --- 白居易2
* id --- 1029
* age --- 19
* email --- 1623@163.com
* name --- 白居易修正测验
* id --- 1030
* age --- 19
* email --- 1623@163.com
*/
获取一切首字段的值 List
default List<Object> listObjs();
default List<Object> listObjs(Function<? super Object, Object>);
default List<Object> listObjs(Wrapper<Student>);
default List<Object> listObjs(Wrapper<Student>, Function<? super Object, Object>);
// 回来全部记载,但只回来第一个字段的值。
@Test
public void listObjs_T(){
iStudentService.listObjs (new QueryWrapper<Student> ().likeRight ("name" , "白居易"))
.forEach (System.out :: println);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE (name LIKE ?)
* ==> Parameters: 白居易%(String)
* <== Columns: id, name, age, email
* <== Row: 1028, 白居易1, 19, 1623@163.com
* <== Row: 1029, 白居易2, 19, 1623@163.com
* <== Row: 1030, 白居易修正测验, 19, 1623@163.com
* .....
* <== Total: 28
*
* 1028
* 1029
* .....
*/
分页查询 IPage
default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper);
// 调用 BaseMapper 的 selectPage 办法,分页查询
分页查询 IPage<Map<String,Object>>
default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper);
// 调用 BaseMapper 的 selectMapsPage 办法,分页查询
增加或修正
有则修正,无则新增
boolean saveOrUpdate(T entity);
public boolean saveOrUpdate(T entity) {
if (null == entity) {
return false;
} else {
TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
String keyProperty = tableInfo.getKeyProperty();
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
return !StringUtils.checkValNull(idVal) && !Objects.isNull(this.getById((Serializable)idVal)) ? this.updateById(entity) : this.save(entity);
}
}
@Test
public void saveOrUpdate_T(){
iStudentService.saveOrUpdate (new Student ("saveOrUpdate_T()",13,"13@13"));
}
/**
* ==> Preparing: INSERT INTO student ( name, age, email ) VALUES ( ?, ?, ? )
* ==> Parameters: saveOrUpdate_T()(String), 13(Integer), 13@13(String)
* <== Updates: 1
*/
先依据条件测验更新,然后再履行 saveOrUpdate 操作
default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper); // 先依据条件测验更新,然后再履行 saveOrUpdate 操作
@Test
public void saveOrUpdate_T2(){
iStudentService.saveOrUpdate (new Student ("saveOrUpdate_T()",13,"13@13"),
new UpdateWrapper<Student> ().eq ("age",14).likeRight ("name","白居易"));
}
/**
* ==> Preparing: UPDATE student SET name=?, age=?, email=? WHERE (age = ? AND name LIKE ?)
* ==> Parameters: saveOrUpdate_T()(String), 13(Integer), 13@13(String), 14(Integer), 白居易%(String)
* <== Updates: 1
*/
批量刺进或修正数据
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
String keyProperty = tableInfo.getKeyProperty();
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(this.getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
}, (sqlSession, entity) -> {
ParamMap<T> param = new ParamMap();
param.put("et", entity);
sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
});
}
@Test
public void saveOrUpdateBatch_T(){
Student student = new Student ();
student.setId (1025L);
student.setEmail ("369@124");
student.setAge (16);
student.setName ("saveOrUpdateBatch_T() 修正测验");
Student student1 = new Student ();
student1.setId (1026L);
student1.setEmail ("369@124");
student1.setAge (16);
student1.setName ("saveOrUpdateBatch_T() 修正测验");
Student student2 = new Student ();
student2.setId (1200L);
student2.setEmail ("369@124");
student2.setAge (16);
student2.setName ("saveOrUpdateBatch_T() 修正测验");
Student student4 = new Student ();
student2.setEmail ("369@124");
student2.setAge (16);
student2.setName ("saveOrUpdateBatch_T() 修正测验");
List<Student> students = new ArrayList<> ();
students.add(student2);
students.add (student1);
students.add (student);
//增加student4后报错,或许id不能为null?可是saveOrUpdate中为空则履行刺进
//students.add (student4);
boolean b = iStudentService.saveOrUpdateBatch (students);
}
/**
* ==> Preparing: SELECT id,name,age,email FROM student WHERE id=?
* ==> Parameters: 1200(Long)
* <== Total: 0
* ==> Preparing: INSERT INTO student ( name, age, email ) VALUES ( ?, ?, ? )
* ==> Parameters: saveOrUpdateBatch_T() 修正测验(String), 16(Integer), 369@124(String)
* ==> Preparing: SELECT id,name,age,email FROM student WHERE id=?
* ==> Parameters: 1026(Long)
* <== Columns: id, name, age, email
* <== Row: 1026, saveOrUpdateBatch_T() 修正测验, 16, 369@124
* <== Total: 1
* ==> Preparing: UPDATE student SET name=?, age=?, email=? WHERE id=?
* ==> Parameters: saveOrUpdateBatch_T() 修正测验(String), 16(Integer), 369@124(String), 1026(Long)
* ==> Preparing: SELECT id,name,age,email FROM student WHERE id=?
* ==> Parameters: 1025(Long)
* <== Columns: id, name, age, email
* <== Row: 1025, saveOrUpdateBatch_T() 修正测验, 16, 369@124
* <== Total: 1
* ==> Preparing: UPDATE student SET name=?, age=?, email=? WHERE id=?
* ==> Parameters: saveOrUpdateBatch_T() 修正测验(String), 16(Integer), 369@124(String), 1025(Long)
*/
8.5 链式调用
default QueryChainWrapper<T> query(); // 一般链式查询
default LambdaQueryChainWrapper<T> lambdaQuery(); // 支撑 Lambda 表达式的修正
default UpdateChainWrapper<T> update(); // 一般链式修正
default LambdaUpdateChainWrapper<T> lambdaUpdate(); // 支撑 Lambda 表达式的修正
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);
8.6 各操作对应SQL句子参考
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.enums;
public enum SqlMethod {
INSERT_ONE("insert", "刺进一条数据(挑选字段刺进)", "<script>\nINSERT INTO %s %s VALUES %s\n</script>"),
UPSERT_ONE("upsert", "Phoenix刺进一条数据(挑选字段刺进)", "<script>\nUPSERT INTO %s %s VALUES %s\n</script>"),
DELETE_BY_ID("deleteById", "依据ID 删去一条数据", "<script>\nDELETE FROM %s WHERE %s=#{%s}\n</script>"),
DELETE_BY_MAP("deleteByMap", "依据columnMap 条件删去记载", "<script>\nDELETE FROM %s %s\n</script>"),
DELETE("delete", "依据 entity 条件删去记载", "<script>\nDELETE FROM %s %s %s\n</script>"),
DELETE_BATCH_BY_IDS("deleteBatchIds", "依据ID调集,批量删去数据", "<script>\nDELETE FROM %s WHERE %s IN (%s)\n</script>"),
LOGIC_DELETE_BY_ID("deleteById", "依据ID 逻辑删去一条数据", "<script>\nUPDATE %s %s WHERE %s=#{%s} %s\n</script>"),
LOGIC_DELETE_BY_MAP("deleteByMap", "依据columnMap 条件逻辑删去记载", "<script>\nUPDATE %s %s %s\n</script>"),
LOGIC_DELETE("delete", "依据 entity 条件逻辑删去记载", "<script>\nUPDATE %s %s %s %s\n</script>"),
LOGIC_DELETE_BATCH_BY_IDS("deleteBatchIds", "依据ID调集,批量逻辑删去数据", "<script>\nUPDATE %s %s WHERE %s IN (%s) %s\n</script>"),
UPDATE_BY_ID("updateById", "依据ID 挑选修正数据", "<script>\nUPDATE %s %s WHERE %s=#{%s} %s\n</script>"),
UPDATE("update", "依据 whereEntity 条件,更新记载", "<script>\nUPDATE %s %s %s %s\n</script>"),
LOGIC_UPDATE_BY_ID("updateById", "依据ID 修正数据", "<script>\nUPDATE %s %s WHERE %s=#{%s} %s\n</script>"),
SELECT_BY_ID("selectById", "依据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s} %s"),
SELECT_BY_MAP("selectByMap", "依据columnMap 查询一条数据", "<script>SELECT %s FROM %s %s\n</script>"),
SELECT_BATCH_BY_IDS("selectBatchIds", "依据ID调集,批量查询数据", "<script>SELECT %s FROM %s WHERE %s IN (%s) %s</script>"),
SELECT_ONE("selectOne", "查询满意条件一条数据", "<script>%s SELECT %s FROM %s %s %s\n</script>"),
SELECT_COUNT("selectCount", "查询满意条件总记载数", "<script>%s SELECT COUNT(%s) FROM %s %s %s\n</script>"),
SELECT_LIST("selectList", "查询满意条件一切数据", "<script>%s SELECT %s FROM %s %s %s\n</script>"),
SELECT_PAGE("selectPage", "查询满意条件一切数据(并翻页)", "<script>%s SELECT %s FROM %s %s %s\n</script>"),
SELECT_MAPS("selectMaps", "查询满意条件一切数据", "<script>%s SELECT %s FROM %s %s %s\n</script>"),
SELECT_MAPS_PAGE("selectMapsPage", "查询满意条件一切数据(并翻页)", "<script>\n %s SELECT %s FROM %s %s %s\n</script>"),
SELECT_OBJS("selectObjs", "查询满意条件一切数据", "<script>%s SELECT %s FROM %s %s %s\n</script>");
private final String method;
private final String desc;
private final String sql;
private SqlMethod(String method, String desc, String sql) {
this.method = method;
this.desc = desc;
this.sql = sql;
}
public String getMethod() {
return this.method;
}
public String getDesc() {
return this.desc;
}
public String getSql() {
return this.sql;
}
}
注:数据库引进数据
/*
Navicat Premium Data Transfer
Source Server : imooc
Source Server Type : MySQL
Source Server Version : 80021
Source Host : localhost:3306
Source Schema : mybatisplus_demo
Target Server Type : MySQL
Target Server Version : 80021
File Encoding : 65001
Date: 23/06/2022 11:34:24
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID,学号',
`name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名字',
`age` int NULL DEFAULT NULL COMMENT '年纪',
`email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`) USING BTREE,
INDEX `id`(`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1063 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1002, 'Jack', 20, 'test2@baomidou.com');
INSERT INTO `student` VALUES (1003, 'Tom', 28, 'test3@baomidou.com');
INSERT INTO `student` VALUES (1004, 'Sandy', 21, 'test4@baomidou.com');
INSERT INTO `student` VALUES (1005, 'Billie', 24, 'test5@baomidou.com');
INSERT INTO `student` VALUES (1007, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1008, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1009, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1010, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1011, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1012, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1013, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1014, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1015, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1016, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1017, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1018, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1019, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1020, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1021, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1022, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1023, '柳宗元', 18, '165@163.com');
INSERT INTO `student` VALUES (1024, '09:26修正测验', 18, '165@163.com');
INSERT INTO `student` VALUES (1025, 'saveOrUpdateBatch_T() 修正测验', 16, '369@124');
INSERT INTO `student` VALUES (1026, 'saveOrUpdateBatch_T() 修正测验', 16, '369@124');
INSERT INTO `student` VALUES (1027, 'saveOrUpdateBatch_T() 修正测验', 16, '369@124');
INSERT INTO `student` VALUES (1028, '白居易1', 19, '1623@163.com');
INSERT INTO `student` VALUES (1029, '白居易2', 19, '1623@163.com');
INSERT INTO `student` VALUES (1030, '白居易修正测验', 19, '1623@163.com');
INSERT INTO `student` VALUES (1031, 'saveOrUpdate_T()', 13, '13@13');
INSERT INTO `student` VALUES (1032, '白居易5', 19, '1623@163.com');
INSERT INTO `student` VALUES (1033, '白居易6', 19, '1623@163.com');
INSERT INTO `student` VALUES (1034, '白居易7', 19, '1623@163.com');
INSERT INTO `student` VALUES (1035, '白居易8', 19, '1623@163.com');
INSERT INTO `student` VALUES (1036, '白居易9', 19, '1623@163.com');
INSERT INTO `student` VALUES (1037, '白居易10', 19, '1623@163.com');
INSERT INTO `student` VALUES (1038, '白居易11', 19, '1623@163.com');
INSERT INTO `student` VALUES (1039, '白居易12', 19, '1623@163.com');
INSERT INTO `student` VALUES (1040, '白居易13', 19, '1623@163.com');
INSERT INTO `student` VALUES (1041, '白居易14', 19, '1623@163.com');
INSERT INTO `student` VALUES (1042, '白居易15', 19, '1623@163.com');
INSERT INTO `student` VALUES (1043, '白居易16', 19, '1623@163.com');
INSERT INTO `student` VALUES (1044, '白居易17', 19, '1623@163.com');
INSERT INTO `student` VALUES (1045, '白居易18', 19, '1623@163.com');
INSERT INTO `student` VALUES (1046, '白居易19', 19, '1623@163.com');
INSERT INTO `student` VALUES (1047, '白居易20', 19, '1623@163.com');
INSERT INTO `student` VALUES (1048, '白居易21', 19, '1623@163.com');
INSERT INTO `student` VALUES (1050, '白居易23', 19, '1623@163.com');
INSERT INTO `student` VALUES (1051, '白居易24', 19, '1623@163.com');
INSERT INTO `student` VALUES (1052, '白居易25', 19, '1623@163.com');
INSERT INTO `student` VALUES (1053, '白居易26', 19, '1623@163.com');
INSERT INTO `student` VALUES (1054, '白居易27', 19, '1623@163.com');
INSERT INTO `student` VALUES (1055, '白居易28', 19, '1623@163.com');
INSERT INTO `student` VALUES (1056, '白居易29', 19, '1623@163.com');
INSERT INTO `student` VALUES (1057, '李清照', 21, 'ssm@163.com');
INSERT INTO `student` VALUES (1058, '李清照', 21, 'ssm@163.com');
INSERT INTO `student` VALUES (1066, '刘咏', 22, '456@123.com');
INSERT INTO `student` VALUES (1067, '李清照', 21, 'ssm@163.com');
INSERT INTO `student` VALUES (1068, '刘咏', 22, '123@123.com');
INSERT INTO `student` VALUES (1069, '刘咏', 22, '234@123.com');
INSERT INTO `student` VALUES (1070, '刘咏', 22, '345@123.com');
INSERT INTO `student` VALUES (1071, '刘咏', 22, '456@123.com');
INSERT INTO `student` VALUES (1072, 'saveOrUpdate_T()', 13, '13@13');
INSERT INTO `student` VALUES (1073, 'saveOrUpdate_T()', 13, '13@13');
INSERT INTO `student` VALUES (1075, 'saveOrUpdateBatch_T() 修正测验', 16, '369@124');
SET FOREIGN_KEY_CHECKS = 1;
九、参考文章
blog.csdn.net/qq_46201721…
blog.csdn.net/weixin_4462…
blog.csdn.net/qq_39513430…
blog.csdn.net/qq_46697271…