MyBatis Plus 入門使用

一、MyBatis Plus 介紹

MyBatis Plus 是國內人員開發的 MyBatis 增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。

MyBatis Plus 的核心功能有:支持通用的 CRUD、代碼生成器與條件構造器。

  • 通用 CRUD:定義好 Mapper 接口後,只需要繼承 BaseMapper 接口即可獲得通用的增刪改查功能,無需編寫任何接口方法與配置文件
  • 條件構造器:通過 EntityWrapper (實體包裝類),可以用於拼接 SQL 語句,並且支持排序、分組查詢等複雜的 SQL
  • 代碼生成器:支持一系列的策略配置與全局配置,比 MyBatis 的代碼生成更好用

BaseMapper 接口中通用的 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 文件

 


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


br> PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">



數據源 db.properties

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

Spring 配置文件 applicationContext.xml

 







class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">




































三、快速體驗 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 分析日誌

MyBatis Plus 入門使用


上面幾個例子中,並沒有在 EmployeeMapper 接口中定義任何方法,也沒有在配置文件中編寫 SQL 語句,而是通過繼承 BaseMapper接口獲得通用的的增刪改查方法,複雜的 SQL 也可以使用條件構造器拼接。

通過這兩種方式已經能夠滿足很多的開發需求了,不過複雜的業務需求還是要編寫 SQL 語句的,流程和 MyBatis 一樣。

PS:

完整的代碼(mybatis-plus-demo 目錄)傳到了 GitHub,

https://github.com/charlesProject/spring-note


分享到:


相關文章: