MyBatis Plus 入门使用详细教程

作者:留兰香丶 时间:2023-08-23 05:48:33 

一、MyBatis Plus 介绍

MyBatis Plus 是国内人员开发的 MyBatis 增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

MyBatis Plus 的核心功能有:支持通用的 CRUD、代码生成器与条件构造器。

通用 CRUD:定义好 Mapper 接口后,只需要继承 BaseMapper<T> 接口即可获得通用的增删改查功能,无需编写任何接口方法与配置文件条件构造器:通过 EntityWrapper<T> (实体包装类),可以用于拼接 SQL 语句,并且支持排序、分组查询等复杂的 SQL代码生成器:支持一系列的策略配置与全局配置,比 MyBatis 的代码生成更好用

BaseMapper<T> 接口中通用的 CRUD 方法:

MyBatis Plus 入门使用详细教程

二、MyBatis Plus 集成 Spring

数据表结构


DROP TABLE IF EXISTS `tbl_employee`;
CREATE TABLE `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;


pom 文件


<dependencies>
<!-- MP -->
<dependency>
 <groupId>com.baomidou</groupId>
 <artifactId>mybatis-plus</artifactId>
 <version>2.3</version>
</dependency>
<!-- 测试 -->
<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
</dependency>
<!-- 数据源 -->
<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>druid</artifactId>
 <version>1.1.10</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.39</version>
</dependency>
<!-- Spring 相关 -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>4.3.9.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-orm</artifactId>
 <version>4.3.9.RELEASE</version>
</dependency>
</dependencies>


MyBatis 全局配置文件 mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 不作任何配置 -->
<configuration />


数据源 db.properties


jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password=1234


Spring 配置文件 applicationContext.xml


<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- MP 提供的 MybatisSqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean"
 class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- mybatis 全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.jas.bean"></property>
<!-- 注入全局MP策略配置 -->
<property name="globalConfig" ref="globalConfiguration"></property>
<!-- 插件注册 -->
<property name="plugins">
 <list>
 <!-- 注册分页插件 -->
 <bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />
 <!-- 注入 SQL 性能分析插件,建议在开发环境中使用,可以在控制台查看 SQL 执行日志 -->
 <bean class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
  <property name="maxTime" value="1000" />
  <!--SQL 是否格式化 默认false-->
  <property name="format" value="true" />
 </bean>
 </list>
</property>
</bean>

<!-- 定义 MybatisPlus 的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 在 2.3 版本以后,dbColumnUnderline 默认值是 true -->
<property name="dbColumnUnderline" value="true"></property>
<!-- 全局的主键策略 -->
<property name="idType" value="0"></property>
<!-- 全局的表前缀策略配置 -->
<property name="tablePrefix" value="tbl_"></property>
</bean>

<!-- 配置mybatis 扫描mapper接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jas.mapper"></property>
</bean>


三、快速体验 MyBatis Plus

实体类 Employee


@TableName(value = "tbl_employee")
public class Employee {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "last_name")
private String lastName;
private String email;
private Integer gender;
private Integer age;

public Employee() {
super();
}

public Employee(Integer id, String lastName, String email, Integer gender, Integer age) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.age = age;
}
// 省略 set、get 与 toString() 方法


mapper 接口


/**
* 不定义任何接口方法
*/
public interface EmployeeMapper extends BaseMapper<Employee> {}


在测试类中生成测试的 mapper 对象


private ApplicationContext context =
 new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

private EmployeeMapper employeeMapper =
 context.getBean("employeeMapper", EmployeeMapper.class);


简单查询测试


@Test
public void getEmpByIdTest() {
Employee employee = employeeMapper.selectById(1);

System.out.println(employee);
}


分页查询测试


@Test
public void getEmpByPage() {
Page<?> page = new Page<>(1, 5);
List<Employee> list = employeeMapper.selectPage(page, null);

System.out.println("总记录数:" + page.getTotal());
System.out.println("总页数" + page.getPages());
System.out.println(list);
}


条件构造器测试


@Test
public void getEmpByName() {
EntityWrapper<Employee> wrapper = new EntityWrapper<>();

// 'last_name' 与 'age' 对应数据库中的字段
wrapper.like("last_name", "张");
wrapper.eq("age", 20);

List<Employee> list = employeeMapper.selectList(wrapper);
System.out.println(list);
}


控制台输出的 SQL 分析日志

MyBatis Plus 入门使用详细教程

上面几个例子中,并没有在 EmployeeMapper 接口中定义任何方法,也没有在配置文件中编写 SQL 语句,而是通过继承 BaseMapper<T> 接口获得通用的的增删改查方法,复杂的 SQL 也可以使用条件构造器拼接。

通过这两种方式已经能够满足很多的开发需求了,不过复杂的业务需求还是要编写 SQL 语句的,流程和 MyBatis 一样。

PS:
完整的代码(mybatis-plus-demo 目录)传到了 GitHub,包括 SQL 表结构与数据文件,点我前往~

来源:https://blog.csdn.net/codejas/article/details/81054791

标签:MyBatis,Plus,入门,使用
0
投稿

猜你喜欢

  • android中图形图像处理之drawable用法分析

    2023-01-13 07:45:03
  • C#实现随鼠标移动窗体实例

    2022-05-16 11:09:31
  • Spring Boot Actuator管理日志的实现

    2023-02-12 10:45:40
  • Android蓝牙的开启和搜索设备功能开发实例

    2022-11-07 20:42:17
  • C# TabControl手动触发DrawItem的实现

    2023-12-27 17:01:27
  • Java实现简单邮件发送功能

    2023-08-15 00:53:23
  • Spring Security使用中Preflight请求和跨域问题详解

    2021-06-12 11:04:15
  • C#实现剪刀石头布游戏

    2021-11-10 05:19:32
  • Kotlin基础通关之字符串与数字类型

    2023-06-22 13:27:35
  • android 仿微信demo——注册功能实现(服务端)

    2023-10-30 09:58:23
  • Android实现取消GridView中Item选中时默认的背景色

    2023-02-26 05:53:30
  • 解决idea默认带的equals和hashcode引起的bug

    2023-12-22 19:02:43
  • SpringBoot用@Async注解实现异步任务

    2023-08-07 06:36:09
  • Android实现点击切换视图并跳转传值

    2022-07-14 07:23:28
  • android studio生成aar包并在其他工程引用aar包的方法

    2021-07-23 21:28:30
  • Andriod studio 打包aar 的方法

    2023-12-13 19:32:36
  • springboot读取resources下文件的方式详解

    2022-05-21 16:14:48
  • Android关于获取时间的记录(小结)

    2021-10-28 04:55:12
  • Java ArrayList实现删除指定位置的元素

    2023-11-25 05:34:13
  • springboot 整合 seata的配置过程

    2023-01-13 01:28:33
  • asp之家 软件编程 m.aspxhome.com