前言:Mybatis-Plus的強大之處在於結合JPA、Mybatis兩者特性,通俗點來說就是結合了Hibernate和Mybatis兩者的特性,達到快速開發的目的
優缺點對比:Hibernate不夠靈活,但是不用編寫SQL,Mybatis夠靈活,但是要編寫大量的SQL語句。只有充分兩者的長處,才能提高開發效率。(ps:JPA的一種實現就是Hibernate,這裏說法不做區分)
持久層開發一般選用的策略:
(1)Mybatis+逆向工程(若字段改變,就需要重新生成逆向工程代碼,也不夠靈活)
(2)Mybatis+JPA (這種比較常用,Mybatis操作聯表以及複雜的SQL,JPA操作單表)
(3)Mybatis-Plus:一站式開發
下面就以Mybatis-Plus的開發模式給大家做出一些講解(基於spring boot):
項目目錄結構:
entity:帶有註解的實體類
pojo:最簡單的實體類
mapper:DAO層
department表結構
employee表結構(dep_id是外鍵)
首先添加Mybatis-Plus依賴
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3.3</version>
</dependency>
application.properties配置文件相關配置(這裏最好不要去掉serverTimezone=GMT,有可能報會錯)
#配置mysql數據庫
spring.datasource.url= jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#配置mybatis-plus的配置文件以及映射文件
mybatis-plus.config-location=classpath:/mybatis/mybatis-config.xml
mybatis-plus.mapper-locations=classpath:/mybatis/mapper/*.xml
#顯示sql語句
logging.level.cn.zdxh.mp.mapper=debug
Employee實體類
@TableName:指定表名,如果表名和實體類一致,可以省略(可選)
@TableId:指定主鍵名,以及主鍵生成策略,這裏是自動增長(必選)
@TableField:指定字段名,同理,一致可省略(可選)
ps:mybatis-plus2.3以後,自動開啓駝峯命名法
//表名
@TableName("employee")
public class Employee {
//主鍵,以及生成策略
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
//員工姓名
@TableField //可省略
private String empName;
//員工性別
private String empSex;
//員工電話
private String empPhone;
//員工對應部門的外鍵id
private Integer depId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSex() {
return empSex;
}
public void setEmpSex(String empSex) {
this.empSex = empSex;
}
public String getEmpPhone() {
return empPhone;
}
public void setEmpPhone(String empPhone) {
this.empPhone = empPhone;
}
public Integer getDepId() {
return depId;
}
public void setDepId(Integer depId) {
this.depId = depId;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", empName='" + empName + '\'' +
", empSex='" + empSex + '\'' +
", empPhone='" + empPhone + '\'' +
", depId=" + depId +
'}';
}
}
Department實體類
//表名
@TableName("department")
public class Department {
//主鍵,以及生成策略
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
//部門名稱
private String departmentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", departmentName='" + departmentName + '\'' +
'}';
}
}
EmployeeCustomer實體類(普通的pojo,沒做註解)
public class EmployeeCustomer {
//主鍵
private Integer id;
//員工姓名
private String empName;
//員工性別
private String empSex;
//員工電話
private String empPhone;
//一個員工對應一個部門
private Department department;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSex() {
return empSex;
}
public void setEmpSex(String empSex) {
this.empSex = empSex;
}
public String getEmpPhone() {
return empPhone;
}
public void setEmpPhone(String empPhone) {
this.empPhone = empPhone;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "EmployeeCustomer{" +
"id=" + id +
", empName='" + empName + '\'' +
", empSex='" + empSex + '\'' +
", empPhone='" + empPhone + '\'' +
", department=" + department +
'}';
}
}
EmployeeMapper接口
只需要繼承一個標記接口BaseMapper<T>,並指定目的對象的泛型,即可進行簡單增刪改查
public interface EmployeeMapper extends BaseMapper<Employee> {
//通過員工id查詢員工的信息,以及所在部門的信息
public EmployeeCustomer selectEmployeeDetailById(Integer id);
}
DepartmentMapper接口
public interface DepartmentMapper extends BaseMapper<Department>{
}
來看看BaseMapper接口究竟爲我們準備了哪些通用方法
多達20個,足以滿足一般的增刪改查
其中在EmployeeMapper.xml中自定義了聯表查詢的方法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zdxh.mp.mapper.EmployeeMapper">
<resultMap id="selectEmpDet" type="cn.zdxh.mp.pojo.EmployeeCustomer">
<id property="id" column="id"/>
<result property="empName" column="emp_name"/>
<result property="empSex" column="emp_sex"/>
<result property="empPhone" column="emp_phone"/>
<association property="department" javaType="cn.zdxh.mp.entity.Department">
<id property="id" column="id"/>
<result property="departmentName" column="department_name"/>
</association>
</resultMap>
<select id="selectEmployeeDetailById" parameterType="integer" resultMap="selectEmpDet">
SELECT
e.id,
e.emp_name,
e.emp_sex,
e.emp_phone,
d.id,
d.department_name
FROM employee e
LEFT JOIN department d
ON e.dep_id=d.id
WHERE e.id=#{id}
</select>
</mapper>
最後不要忘記了添加mapper包掃描
下面就可以用測試類進行測試了
測試xml中自定義的方法:
@Test
public void testXmlSelect() {
EmployeeCustomer employeeCustomer = employeeMapper.selectEmployeeDetailById(1);
System.out.println(employeeCustomer);
}
測試BaseMapper中提供的方法:
@Test
public void testBaseSelect(){
Employee employee = employeeMapper.selectById(1);
System.out.println(employee);
}
@Test
public void testBaseInsert(){
Department department=new Department();
department.setDepartmentName("市場部");
departmentMapper.insert(department);
}
以上測試方法統統沒問題
不僅如此,還可以進行有條件的查詢、分頁查詢
條件查詢:EntityWrapper條件構造器
@Test
public void testBaseSelect2(){
List<Employee> employees = employeeMapper.selectList(
new EntityWrapper<Employee>()
.eq("emp_name", "ning")//注意是列名
);
System.out.println(employees);
}
條件構造器有相當多的構造方法可以調用,具體使用請參照官方文檔
分頁查詢同理:Page<>對象
注意:默認的分頁查詢是邏輯分頁,若要進行物理分頁,還要添加分頁插件PaginationInterceptoer才行
總結:Mybatis-Plus真的好用