点击进入我的个人博客
文章目录
一. 概述二. 配置1. 在pom.xml文件中引入相关依赖2. 在配置文件application.yml中添加相关配置3. 启动类中添加增加@MapperScan注解扫描
三. MVC应用规范1.实体类要遵守的规范2.持久层dao要遵守的规范3.业务层service要遵守的规范4.控制层controller要遵守的规范
四. 条件查询和分页查询1.条件查询2.分页
五. 具体方法使用
一. 概述
是对 Mybatis框架的二次封装和扩展纯正血统:完全继承原生 Mybatis 的所有特性最少依赖:仅仅依赖 Mybatis以及Mybatis-Spring性能损耗小:启动即会自动注入基本CURD,性能无损耗,直接面向对象操作自动热加载: Mapper对应的xml可以热加载,大大减少重启Web服务器时间,提升开发效率全局拦截:提供全表 delete、update操作智能分析阻断避免 Sql注入:内置Sql注入内容剥离器,预防Sql注入攻击
二. 配置
1. 在pom.xml文件中引入相关依赖
<dependency>
<groupId>com.baomidou
</groupId>
<artifactId>mybatisplus-spring-boot-starter
</artifactId>
<version>${mybatisplus-spring-boot-starter.version}
</version>
</dependency>
<dependency>
<groupId>com.baomidou
</groupId>
<artifactId>mybatis-plus
</artifactId>
<version>${mybatisplus.version}
</version>
</dependency>
2. 在配置文件application.yml中添加相关配置
mybatis-plus:
typeAliasesPackage: com.tensquare.article.pojo
global-config:
id-type: 1
db-column-underline: false
refresh-mapper: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
lazyLoadingEnabled: true
multipleResultSetsEnabled: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3. 启动类中添加增加@MapperScan注解扫描
@SpringBootApplication
@MapperScan("com.tensquare_article.dao")
public class ArticleApplication {
public static void main(String
[] args
) {
SpringApplication
.run(ArticleApplication
.class, args
);
}
@Bean
public IdWorker
createIdWorker(){
return new IdWorker (1,1 );
}
}
三. MVC应用规范
1.实体类要遵守的规范
@TableName(“表名”)实现Serializable序列化接口@TableId(type = IdType.INPUT)指定主键id
@TableName("tb_article")
@Data
public class Article implements Serializable {
@TableId(type
= IdType
.INPUT
)
private String id
;
private String columnid
;
private String userid
;
private String title
;
private String content
;
private String image
;
private Date createtime
;
private Date updatetime
;
private String ispublic
;
private String istop
;
private Integer visits
;
private Integer thumbup
;
private Integer comment
;
private String state
;
private String channelid
;
private String url
;
private String type
;
}
2.持久层dao要遵守的规范
只要继承BaseMapper<T>接口就行了dao接口获得继承方法,mabatis-plus自动生成dao实现类
public interface ArticleDao extends BaseMapper<Article> {
}
3.业务层service要遵守的规范
跟以往使用mybatis一样,主要是在调用dao的方法时要调用mybatis-plus为我们提供的方法 案例:通过id修改记录
public void updateById(Article article
) {
Wrapper
<Article> wrapper
= new EntityWrapper<> ( );
wrapper
.eq
( "id",article
.getId
() );
articleDao
.update
( article
, wrapper
);
}
4.控制层controller要遵守的规范
没有任何变化,跟使用mybatis一摸一样
四. 条件查询和分页查询
1.条件查询
使用mybatis-plus提供的EntityWrapper对象封装where查询条件
EntityWrapper wrapper
= new EntityWrapper<Article>();
wrapper
.eq("id", article
.getId());
wrapper
.eq(null
!= map
.get(field
), field
, map
.get(field
));
2.分页
使用mybatis-plus提供的Page对象
@PostMapping("/search/{page}/{size}")
public String
findByPage(@PathVariable Integer page
,
@PathVariable Integer size
,
@RequestBody Map
<String,Object> map
){
Page
<Article> pageData
= articleService
.findByPage(page
,size
,map
);
return "分页查询成功";
}
向mybatis-plus中注入PaginationInterceptor插件
新建 config包,创建MybatisPlusConfig对象,添加下面的代码
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor
paginationInterceptor() {
return new PaginationInterceptor();
}
}
重点:
service层分页代码逻辑
public Page
<Article> findByPage(Integer page
, Integer size
, Map
<String, Object> map
) {
Wrapper
<Article> wrapper
= new EntityWrapper<> ( );
Set
<String> keys
= map
.keySet
( );
for (String key
: keys
) {
wrapper
.eq
( map
.get
( key
)!=null
,key
, map
.get
( key
));
}
Page
<Article> pageData
= new Page<> ( page
,size
);
List
<Article> articleList
= articleDao
.selectPage
( pageData
, wrapper
);
pageData
.setRecords
( articleList
);
return pageData
;
}
五. 具体方法使用
懒癌发作,直接搬运现成写的还不错的文章
链接地址:
官网 简书文章 csdn文章