1. 入门案例
1.1 导入相关坐标
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.5.3.1</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.2.16</ version>
</ dependency>
< dependency>
< groupId> com.mysql</ groupId>
< artifactId> mysql-connector-j</ artifactId>
< scope> runtime</ scope>
</ dependency>
1.2 编写配置文件
spring :
datasource :
type : com.alibaba.druid.pool.DruidDataSource
driver-class-name : com.mysql.cj.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/ssm_db
username : root
password : root
1.3 编写实体类
package com. zky. domain ;
@TableName ( "tbl_book" )
public class Book {
private Long id;
private String type;
private String name;
private String description;
. . .
}
1.4 dao层
package com. zky. dao ;
import com. baomidou. mybatisplus. core. mapper. BaseMapper ;
import com. zky. domain. Book ;
import org. apache. ibatis. annotations. Mapper ;
@Mapper
public interface BookDao extends BaseMapper < Book > {
}
2. MybatisPlus官网
3. 标准数据层开发
3.1 增删改查(CRUD)
@Autowired
private BookDao bookDao;
@Test
void testSave ( ) {
Book book = new Book ( ) ;
book. setName ( "mp测试" ) ;
book. setType ( "mp测试" ) ;
book. setDescription ( "mp测试" ) ;
bookDao. insert ( book) ;
}
@Test
void testDelete ( ) {
bookDao. deleteById ( 1631922089464193025L ) ;
}
@Test
void testUpdate ( ) {
Book book = new Book ( ) ;
book. setName ( "mp测试修改" ) ;
book. setId ( 1631921920366632961L ) ;
book. setDescription ( "mp测试修改" ) ;
bookDao. updateById ( book) ;
}
@Test
void testGetById ( ) {
Book book = bookDao. selectById ( 1631921920366632961L ) ;
System . out. println ( book) ;
}
@Test
void testGetAll ( ) {
List < Book > books = bookDao. selectList ( null ) ;
System . out. println ( books) ;
}
3.2 导入lombok
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
@TableName ( "tbl_book" )
@Data
public class Book {
private Long id;
private String type;
private String name;
private String description;
}
3.3 分页查询
3.3.1 配置拦截器
package com. zky. config ;
import com. baomidou. mybatisplus. extension. plugins. MybatisPlusInterceptor ;
import com. baomidou. mybatisplus. extension. plugins. inner. PaginationInnerInterceptor ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor ( ) {
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor ( ) ;
mpInterceptor. addInnerInterceptor ( new PaginationInnerInterceptor ( ) ) ;
return mpInterceptor;
}
}
3.3.2 测试
@Test
void testGetByPage ( ) {
IPage page = new Page ( 1 , 5 ) ;
bookDao. selectPage ( page, null ) ;
System . out. println ( "当前页码值 ==> " + page. getCurrent ( ) ) ;
System . out. println ( "每页显示数 ==> " + page. getSize ( ) ) ;
System . out. println ( "一共多少页 ==> " + page. getPages ( ) ) ;
System . out. println ( "一共多少条数据 ==> " + page. getTotal ( ) ) ;
System . out. println ( "数据 ==> " + page. getRecords ( ) ) ;
}
3.3.3 开启 mp 日志
mybatis-plus :
configuration :
log-impl : org.apache.ibatis.logging.stdout.StdOutImpl
4. DQL编程控制(查)
4.1 简化控制台输出
4.1.1 创建 localback.xml 文件
<? xml version="1.0" encoding="UTF- 8"? >
<configuration>
</configuration>
4.1.2 application.yaml
spring :
datasource :
type : com.alibaba.druid.pool.DruidDataSource
driver-class-name : com.mysql.cj.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/ssm_db
username : root
password : root
main :
banner-mode : off
mybatis-plus :
configuration :
log-impl : org.apache.ibatis.logging.stdout.StdOutImpl
global-config :
banner : false
4.2 按条件查询
@Test
void testGetAll ( ) {
LambdaQueryWrapper < Book > lqw = new LambdaQueryWrapper < > ( ) ;
lqw. lt ( Book :: getId , 10 )
. or ( )
. gt ( Book :: getId , 20 ) ;
List < Book > books = bookDao. selectList ( lqw) ;
System . out. println ( books) ;
}
4.3 条件查询 null 值处理
4.4 查询投影(as)
LambdaQueryWrapper < Book > lqw = new LambdaQueryWrapper < > ( ) ;
lqw. select ( Book :: getId , Book :: getName ) ;
List < Book > books = bookDao. selectList ( lqw) ;
System . out. println ( books) ;
QueryWrapper < Book > qw = new QueryWrapper < > ( ) ;
qw. select ( "id" , "name" ) ;
List < Book > books = bookDao. selectList ( qw) ;
System . out. println ( books) ;
QueryWrapper < Book > qw = new QueryWrapper < > ( ) ;
qw. select ( "count(*) as count" ) ;
List < Map < String , Object > > maps = bookDao. selectMaps ( qw) ;
System . out. println ( maps) ;
QueryWrapper < Book > qw = new QueryWrapper < > ( ) ;
qw. select ( "count(*) as count, type" ) ;
qw. groupBy ( "type" ) ;
List < Map < String , Object > > maps = bookDao. selectMaps ( qw) ;
System . out. println ( maps) ;
4.5 查询条件(where)
LambdaQueryWrapper < Book > lqw = new LambdaQueryWrapper < > ( ) ;
lqw. eq ( Book :: getId , 1 ) . eq ( Book :: getType , "计算机理论" ) ;
Book book = bookDao. selectOne ( lqw) ;
System . out. println ( book) ;
LambdaQueryWrapper < Book > lqw = new LambdaQueryWrapper < > ( ) ;
lqw. between ( Book :: getId , 10 , 20 ) ;
List < Book > books = bookDao. selectList ( lqw) ;
System . out. println ( books) ;
LambdaQueryWrapper < Book > lqw = new LambdaQueryWrapper < > ( ) ;
lqw. like ( Book :: getType , "营销" ) ;
lqw. likeLeft ( Book :: getType , "营销" ) ;
lqw. likeRight ( Book :: getType , "营销" ) ;
List < Book > books = bookDao. selectList ( lqw) ;
System . out. println ( books) ;
4.5 字段映射和表名映射
5. DML编程控制(增删改)
5.1 id生成策略控制
mybatis-plus :
global-config :
db-config :
id-type : assign_id
table-prefix : tbl_
5.2 多记录操作
@Test
void testDelete ( ) {
List < Long > list = new ArrayList < > ( ) ;
list. add ( 1631945023436132354L ) ;
list. add ( 1631945056801906690L ) ;
bookDao. deleteBatchIds ( list) ;
}
5.3 逻辑删除
5.3.1 数据库增加相应字段
5.3.2 实体类增加字段
5.3.3 全局配置删除字面值
mybatis-plus :
db-config :
logic-delete-value : deleted
logic-not-delete-value : 0
logic-delete-field : 1
5.4 乐观锁(并发)
5.4.1 数据库添加所标记字段
5.4.2 实体类中添加字段
5.4.3 配置乐观锁拦截器
package com. zky. config ;
import com. baomidou. mybatisplus. extension. plugins. MybatisPlusInterceptor ;
import com. baomidou. mybatisplus. extension. plugins. inner. OptimisticLockerInnerInterceptor ;
import com. baomidou. mybatisplus. extension. plugins. inner. PaginationInnerInterceptor ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor ( ) {
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor ( ) ;
mpInterceptor. addInnerInterceptor ( new PaginationInnerInterceptor ( ) ) ;
mpInterceptor. addInnerInterceptor ( new OptimisticLockerInnerInterceptor ( ) ) ;
return mpInterceptor;
}
}
5.4.4 必须获取version数据 !
6. 代码生成器
6.1 导入坐标
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-generator</ artifactId>
< version> 3.3.0</ version>
</ dependency>
< dependency>
< groupId> org.apache.velocity</ groupId>
< artifactId> velocity-engine-core</ artifactId>
< version> 2.3</ version>
</ dependency>
6.2 配置类
package com. zky ;
import com. baomidou. mybatisplus. annotation. IdType ;
import com. baomidou. mybatisplus. generator. AutoGenerator ;
import com. baomidou. mybatisplus. generator. config. DataSourceConfig ;
import com. baomidou. mybatisplus. generator. config. GlobalConfig ;
import com. baomidou. mybatisplus. generator. config. PackageConfig ;
import com. baomidou. mybatisplus. generator. config. StrategyConfig ;
public class CodeGenerator {
public static void main ( String [ ] args) {
AutoGenerator autoGenerator = new AutoGenerator ( ) ;
DataSourceConfig dataSource = new DataSourceConfig ( ) ;
dataSource. setDriverName ( "com.mysql.cj.jdbc.Driver" ) ;
dataSource. setUrl ( "jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC" ) ;
dataSource. setUsername ( "root" ) ;
dataSource. setPassword ( "root" ) ;
autoGenerator. setDataSource ( dataSource) ;
GlobalConfig globalConfig = new GlobalConfig ( ) ;
globalConfig. setOutputDir ( "D:\\IdeaProject\\mybatisPlusProject\\mybatisplus_02_dql\\src\\main\\java" ) ;
globalConfig. setOpen ( false ) ;
globalConfig. setAuthor ( "zky" ) ;
globalConfig. setFileOverride ( true ) ;
globalConfig. setMapperName ( "%sDao" ) ;
globalConfig. setIdType ( IdType . ASSIGN_ID ) ;
autoGenerator. setGlobalConfig ( globalConfig) ;
PackageConfig packageInfo = new PackageConfig ( ) ;
packageInfo. setParent ( "com.zky" ) ;
packageInfo. setEntity ( "domain" ) ;
packageInfo. setMapper ( "dao" ) ;
autoGenerator. setPackageInfo ( packageInfo) ;
StrategyConfig strategyConfig = new StrategyConfig ( ) ;
strategyConfig. setInclude ( "tbl_book" ) ;
strategyConfig. setTablePrefix ( "tbl_" ) ;
strategyConfig. setRestControllerStyle ( true ) ;
strategyConfig. setVersionFieldName ( "version" ) ;
strategyConfig. setLogicDeleteFieldName ( "deleted" ) ;
strategyConfig. setEntityLombokModel ( true ) ;
autoGenerator. setStrategy ( strategyConfig) ;
autoGenerator. execute ( ) ;
}
}
strategyConfig. setTablePrefix ( "tbl_" ) ;
strategyConfig. setRestControllerStyle ( true ) ;
strategyConfig. setVersionFieldName ( "version" ) ;
strategyConfig. setLogicDeleteFieldName ( "deleted" ) ;
strategyConfig. setEntityLombokModel ( true ) ;
autoGenerator. setStrategy ( strategyConfig) ;
autoGenerator. execute ( ) ;
}
}