springboot集成mybatis-plus

mybatis-plus特性(可以看官网)

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库
  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

我之所以用这个完全感觉这个很简便,代码写起来很简洁,除了查询(简单的查询还是可以的要是复杂的查询还是老老实实的创建个xml 文件吧),剩下的增删改,都可以用mybatis-plus 底层自带的语法去做这个事情

废话不多说直接撸代码

mybatis-plus 所需要的包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.6</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatisplus-spring-boot-starter</artifactId>
    <version>1.0.5</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>2.3.3</version>
</dependency>
application.properties 配置一下数据库连接 和mybatis -plus 一些配置
server.port=8090

logging.config=classpath:log4j2.xml

# 数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tom?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# MyBatis 配置
mybatis-plus.mapper-locations=classpath:/mybatis/*Mapper.xml
mybatis-plus.global-config.id-type=2
mybatis-plus.global-config.field-strategy=2
mybatis-plus.global-config.db-column-underline=true
mybatis-plus.global-config.refresh-mapper=true
mybatis-plus.global-config.logic-delete-value=0
mybatis-plus.global-config.logic-not-delete-value=1
mybatis-plus.global-config.sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false

项目的目录结构

 

springboot 项目启动处加上扫描 (这个用springboot 的相信都知道的)

实体类如下

注意:这里要用驼峰命名 我是懒省事 直接就一个单词

假如你数据库中字段是 student_name  这里你要写 studentName, 小驼峰 要和数据库对应上

mapper 层是要继承mybatis -plus 的basemapper

 

Service 层继承为我们提供的IService就行了

@Service

public interface StudentService extends IService<StudentDTO> {

}

ServiceImple

@Service
public class StudenService Impl extends ServiceImpl<BaseMapper, StudentDTO> implements StudentService {

}

这里我们业务层和mapper 层已经写完了

控制层

@RestController
@RequestMapping("students/")
public class TestController {
    @Autowired
    private StudentMapper studentMapper;


    @PostMapping("/students")
    public IResponseStatus add(@RequestBody @Validated StudentDTO studentDTO){
        studentDTO.setName("测试的数据");
        studentMapper.insert(studentDTO);
        studentMapper.add("222");
        return  CommonStatus.ADD_OK;
    }

    @PutMapping("/students")
    public IResponseStatus mpdify(@RequestBody StudentDTO studentDTO){
        studentMapper.updateById(studentDTO);
        return  CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/students/{id}")
    public IResponseStatus del(@PathVariable String id){
        StudentDTO student=new StudentDTO();
        student.setId(id);
        studentMapper.deleteById(student);
        return  CommonStatus.DELETE_OK;
    }

}

有兴趣的同学可以看一下

假如我们的业务不涉及到查询 是不是或者简单的查询我们就不用创建xml 文件了 直接可以用底层自带的方法就可以完全解决问题了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值