通用 Mapper 筆記
搭建開發環境(spring boot)
數據庫表
導入依賴
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.4</version>
</dependency>
配置
server:
port: 8088
#jdbc配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
實體類
package com.wang.pojo;
import javax.persistence.*;
@Table(name="tabple_emp")
public class Employee {
private Integer empId;//emp_id
private String empName;//emp_name
private Double empSalary;//emp_salary_apple
private Integer empAge;//emp_age
public Employee() {
}
public Employee(Integer empId, String empName, Double empSalary, Integer empAge) {
super();
this.empId = empId;
this.empName = empName;
this.empSalary = empSalary;
this.empAge = empAge;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + ", empSalary=" + empSalary + ", empAge=" + empAge
+ "]";
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Double getEmpSalary() {
return empSalary;
}
public void setEmpSalary(Double empSalary) {
this.empSalary = empSalary;
}
public Integer getEmpAge() {
return empAge;
}
public void setEmpAge(Integer empAge) {
this.empAge = empAge;
}
}
dao接口
package com.wang.dao;
import com.wang.pojo.Employee;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface EmpLoyeeDao extends Mapper<Employee> {
}
service
package com.wang.service;
import com.wang.dao.EmpLoyeeDao;
import com.wang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
private EmpLoyeeDao empLoyeeDao;
public Employee getOne(Employee employeeQueryCondition) {
return empLoyeeDao.selectOne(employeeQueryCondition);
}
}
測試類
package mapper.test;
import com.wang.Application;
import com.wang.pojo.Employee;
import com.wang.service.EmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class Test1 {
@Autowired
private EmployeeService employeeService;
@Test
public void testSelectOne()
{
//1.創建封裝查詢條件的實體類
Employee employee = new Employee(null,"bob",null,null);
//2.執行查詢
Employee result = employeeService.getOne(employee);
System.out.println(result);
}
}
常用註解
@Table 註解
作用:建立實體類和數據庫表之間的對應關係。
默認規則:實體類類名首字母小寫作爲表名。Employee 類→employee 表。
用法:在@Table 註解的 name 屬性中指定目標數據庫表的表名
@Column
註解
作用:建立實體類字段和數據庫表字段之間的對應關係。
默認規則:
- 實體類字段:駝峯式命名
- 數據庫表字段:使用“_”區分各個單詞
用法:在@Column 註解的 name 屬性中指定目標字段的字段名
@Id
註解
通用 Mapper 在執行 xxxByPrimaryKey(key)方法時,有兩種情況。
情況 1:沒有使用@Id 註解明確指定主鍵字段
之所以會生成上面這樣的 WHERE 子句是因爲通用 Mapper 將實體類中的所有字段都拿來放在一起作爲聯合主鍵。
情況 2:使用@Id 主鍵明確標記和數據庫表中主鍵字段對應的實體類字段。
@GeneratedValue
註解
作用:讓通用 Mapper 在執行 insert 操作之後將數據庫自動生成的主鍵值回寫到實體類對象中。
自增主鍵用法:
@Transient
主鍵
用於標記不與數據庫表字段對應的實體類字段。
常用方法
selectOne
方法
通用 Mapper 替我們自動生成的 SQL 語句情況
實體類封裝查詢條件生成 WHERE 子句的規則
- 使用非空的值生成 WHERE 子句
- 在條件表達式中使用“=”進行比較
要求必須返回一個實體類結果,如果有多個,則會拋出異常
xxxByPrimaryKey
方法
需要使用@Id
主鍵明確標記和數據庫表主鍵字段對應的實體類字段,否則通用 Mapper 會將所有實體類字段作爲聯合主鍵。
xxxSelective
方法
非主鍵字段如果爲 null 值,則不加入到 SQL 語句中。
QBC 查詢
概念
QueryByCriteria
Criteria 是 Criterion 的複數形式。意思是:規則、標準、準則。在 SQL 語句中相當 於查詢條件。
QBC 查詢是將查詢條件通過 Java 對象進行模塊化封裝。
代碼示例
@Test
public void testSelectByExample()
{
//目標:WHERE (emp_salary>? AND emp_age<?) OR (emp_salary<? AND emp_age>?)
//1.創建Example對象
Example example = new Example(Employee.class);
//***********************
//i.設置排序信息
example.orderBy("empSalary").asc().orderBy("empAge").desc();
//ii.設置“去重”
example.setDistinct(true);
//iii.設置select字段
example.selectProperties("empName","empSalary");
//***********************
//2.通過Example對象創建Criteria對象
Example.Criteria criteria01 = example.createCriteria();
Example.Criteria criteria02 = example.createCriteria();
//3.在兩個Criteria對象中分別設置查詢條件
//property參數:實體類的屬性名
//value參數:實體類的屬性值
criteria01.andGreaterThan("empSalary", 3000)
.andLessThan("empAge", 25);
criteria02.andLessThan("empSalary", 5000)
.andGreaterThan("empAge", 30);
//4.使用OR關鍵詞組裝兩個Criteria對象
example.or(criteria02);
//5.執行查詢
List<Employee> empList = employeeService.getEmpListByExample(example);
for (Employee employee : empList) {
System.out.println(employee);
}
}