MyBatis-Plus是MyBatis的增强版,对经常使用到的SQL中CRUD语法进行的封装,提高了开发效率,并且减少了性能的损耗和繁琐的配置
一. 概述
- 是对 Mybatis框架的二次封装和扩展
- 纯正血统:完全继承原生 Mybatis 的所有特性
- 最少依赖:仅仅依赖 Mybatis以及Mybatis-Spring
- 性能损耗小:启动即会自动注入基本CURD,性能无损耗,直接面向对象操作
- 自动热加载: Mapper对应的xml可以热加载,大大减少重启Web服务器时间,提升开发效率
- 全局拦截:提供全表 delete、update操作智能分析阻断
- 避免 Sql注入:内置Sql注入内容剥离器,预防Sql注入攻击
二. 配置
1. 在pom.xml文件中引入相关依赖
1 2 3 4 5 6 7 8 9 10 11 12
| <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中添加相关配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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注解扫描
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @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实现类
1 2
| public interface ArticleDao extends BaseMapper<Article> { }
|
3.业务层service要遵守的规范
- 跟以往使用
mybatis
一样,主要是在调用dao的方法时要调用mybatis-plus
为我们提供的方法
案例:通过id修改记录
1 2 3 4 5 6 7 8 9
| public void updateById(Article article) {
Wrapper<Article> wrapper = new EntityWrapper<> ( ); wrapper.eq ( "id",article.getId () ); articleDao.update ( article, wrapper); }
|
4.控制层controller要遵守的规范
四. 条件查询和分页查询
1.条件查询
- 使用mybatis-plus提供的
EntityWrapper
对象封装where查询条件
1 2 3 4 5 6 7 8 9 10
| EntityWrapper wrapper = new EntityWrapper<Article>(); wrapper.eq("id", article.getId());
wrapper.eq(null != map.get(field), field, map.get(field));
|
具体使用参考下面代码
2.分页
1 2 3 4 5 6 7 8 9
| @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 "分页查询成功"; }
|
1 2 3 4 5 6 7
| @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
|
重点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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; }
|
五. 具体方法使用
懒癌发作,直接搬运现成写的还不错的文章
链接地址: