学自狂神说
1.MybatisPlus
1.使用springboot初始化, 导入依赖
数据库驱动 mysql-connector-java
lombok
mybatiplus 导入mybatisPlus后不用导入mybatis3.0.5版本
说明问题,使用mybatisPlus简化代码,不要导入两个mybatis
连接数据库 mysql5与8驱动不同,且8要加时区serverTimezone=GMT%2B8
下面是mysql8 其中的useSSL是安全协议, useUnicode与characterEncoding是一起的表示编码解码格式
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
// mybatisplus的操作 先建一个springboot项目
// 再 在对应的Mapper上面继承基本的类BaseMapper
// 注意这里面的泛型是 可以指定的
@Repository // 表示持久层
public interface UserMapper extends BaseMapper<User>{
// 所有的单表的crud就编写完成了
}
usermapper.selectList(null) // 查询全部
MybatisPlus中的Wrapper是条件构造器
// 快速遍历
users.forEach(System.out::println)
2.配置日志
#默认的日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3.CRUD扩展
insert插入
数据库插入的默认值为全局唯一id,(当你没设置ID时),默认雪花算法(mybatisPlus)
注意雪花算法是一个long型的ID
// 对应数据库的主键(uuid, 自增, 雪花算法, redis, zookeeper)
//ID_WORKER-默认的是全局唯一Id aoto-自增(要求数据库字段也是自增的) , none-表示未设置主键, input-手动输入(必须自己写id,不然为null), uuid-全局唯一 ,ID_WORKER_STR-ID_WORKER的字符串表示法
@TableId(type=IdType.ID_WORKER)
private Long id;
更新操作
所有的sql都是自动帮你动态配置
自动填充
创建时间, 修改时间.这些操作都是自动化完成的, 我们不希望手动更新
方式一数据库解决
1.在表中新增字段create_time , update_time 可以设置默认值自动更新
方式二: 代码级别的操作
加入注解
@TableField(fill = FieldFill.INSERT) // 利用反射
private Date createTime
// 字段填充内容
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime
编写处理器自己处理
implement MetaObjectHandler 会与上面的对应起来
乐观锁(加version), 悲观锁
测试MP乐观锁的插件
给数据库中增加version字段
实体类对应的字段
@Version // 乐观锁注解 适用于并发场景下 private Integer version;
注册组件
-
测试
模拟单线程, 成功
-
模拟多线程失败时
查询操作
批量查询
条件查询 利用map
分页查询
1.原始的limit
2.pageHelper分页插件
3.Mybatisplus也有分页插件
3.1配置拦截器 可以在官网上看
3.2直接使用 本质还是使用limit
page可以看总数什么的, 可以自己看
删除操作
逻辑删除,利用字段标识(deleted=0->delete=1),进行假删除
数据库表中添加字段deleted
实体类添加属性
@TableLogic // 逻辑删除
private Integer deleted;
3.配置
// 逻辑删除组件
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}
配置properties
# 配置逻辑删除, 没有删除为1, 删除了为0
测试, 删除的方法不变, 但是他会自动变为update操作
5.条件构造器wrapper
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.isNotNull("name")
.ge("age", 12); // ge表示大于等于
UserMapper.selectList(wrapper).forEach(System.out::println);
wrapper.eq("name","花花"); // 表示name='花花'
userMapper.selectOne(wrapper);
between使用
like使用
map也可以foreach
子查询中使用模糊查询
排序
mybatis-plus的依赖
总的configuration
properties
代码生成器
public class Code {
public static void main(String[] args) {
//需要构建一个 代码自动生成器 对象
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
//配置策略
//1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("ChanV");
gc.setOpen(false);
gc.setFileOverride(false); //是否覆盖
gc.setServiceName("%sService"); //去Service的I前缀
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
//2、设置数据源
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("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//3、包的配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("blog");
pc.setParent("com.chanv");
pc.setEntity("pojo");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
//4、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("user"); //设置要映射的表名
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true); //自动lombok
strategy.setLogicDeleteFieldName("deleted");
//自动填充配置
TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
TableFill updateTime = new TableFill("update_time", FieldFill.UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(createTime);
tableFills.add(updateTime);
strategy.setTableFillList(tableFills);
//乐观锁
strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true); //localhost:8080/hello_id_2
mpg.setStrategy(strategy);
mpg.execute(); //执行代码构造器
}
}