Mybatis-plus

1.什么是Mybatis-plus?

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

愿景

 

我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。

2.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、SQLServer 等多种数据库
  • 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

3.框架结构

 4.使用Mybatis-plus

       4.1 引入相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>最新版本</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

    4.2Mybatis-plus使用

         

#Druid
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/db6?serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=chuxin0920
spring.datasource.druid.initial-size=5
#mybatis??
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
@MapperScan("com.xal.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

    编写实体类 和mapper接口

5. 进行测试

 @Test
    public void test() {
        Student student = studentMapper.selectById(1);
        System.out.println(student);
    }

 6.进行连表分页查询

             6.1 创建Config

@Configuration
public class Config {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }


      6.2 在mapper创建方法

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    IPage<Student> findByPage(IPage<Student> iPage, @Param("ew") Wrapper<Student> wrapper);
}

 6.3 在mapper.xml编写sql语句

  <resultMap id="studnet1" type="com.xal.pojo.Student" autoMapping="true">
        <id column="sid" property="sid"/>
        <association property="aClass" javaType="com.xal.pojo.Class" autoMapping="true">
            <id property="cid" column="cid"/>
        </association>
    </resultMap>
    <sql id="student">
        SID
        ,SNAME,SAGE,C.CID,CNAME
    </sql>
    <select id="findByPage" resultMap="studnet1">
        select
        <include refid="student"/>
        from student s join Class c on s.cid=c.cid
        <if test="ew!=null">
            <where>
                ${ew.sqlSegment}
            </where>
        </if>
    </select>

          6.4       进行测试

  @Test
    public void findPage() {
        Page<Student> page = new Page<>(1, 3);//current:当前第几页  size:每页显示条数
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.gt("sid", 2);
        studentMapper.findByPage(page, wrapper);//把查询分页的结果封装到page对象中
        System.out.println("当前页的记录" + page.getRecords());//获取当前页的记录
        System.out.println("获取总页数" + page.getPages());//获取总页数
        System.out.println("获取总条数" + page.getTotal());//获取总条数

    }

7.总结

mybats-plus 简化了查询的语句   在进行单表查询时我们可以直接忽略sql语句  直接进行调用mapper层的调用  

studentMapper.selectList

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值