资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

MyBatis Plus 入门使用详细教程

Java 更新时间: 发布时间: 计算机考试归档 最新发布

MyBatis Plus 入门使用详细教程

一、MyBatis Plus 介绍

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

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

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

baseMapper 接口中通用的 CRUD 方法:

二、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 文件

 
 
 
  com.baomidou
  mybatis-plus
  2.3
 
 
 
  junit
  junit
  4.12
 
 
 
  com.alibaba
  druid
  1.1.10
 
 
 
  mysql
  mysql-connector-java
  5.1.39
 
 
 
  org.springframework
  spring-context
  4.3.9.RELEASE
 
 
  org.springframework
  spring-orm
  4.3.9.RELEASE
 
 

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




数据源 db.properties

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

Spring 配置文件 applicationContext.xml


 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
  
  
  
  
  
   
   
   
  
  
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 

三、快速体验 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 {}

在测试类中生成测试的 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 list = employeeMapper.selectPage(page, null);
 
 System.out.println("总记录数:" + page.getTotal());
 System.out.println("总页数" + page.getPages());
 System.out.println(list);
 }

条件构造器测试

@Test
 public void getEmpByName() {
 EntityWrapper wrapper = new EntityWrapper<>();
 
 // 'last_name' 与 'age' 对应数据库中的字段 
 wrapper.like("last_name", "张");
 wrapper.eq("age", 20);
 
 List list = employeeMapper.selectList(wrapper);
 System.out.println(list);
 }

控制台输出的 SQL 分析日志

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

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

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

总结

转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/132159.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【MyBatis Plus 入门使用详细教程】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2