上一節咱們學習基於 mybatis-plus
的條件構造器——QueryWrapper,這一節咱們來學習一下mybatis-plus
的ActiveRecord(活動記錄)
。java
在開始以前,咱們先來了解一下什麼是ActiveRecord(活動記錄)?mysql
Active Record(活動記錄),是一種領域模型模式,特色是一個模型類對應關係型數據庫中的一個表,而模型類的一個實例對應表中的一行記錄。git
ActiveRecord 一直廣受動態語言( PHP 、 Ruby 等)的喜好,而 Java 做爲準靜態語言,對於 ActiveRecord 每每只能感嘆其優雅,因此 MP 也在 AR 道路上進行了必定的探索github
那麼咱們如何使用AR模式呢?spring
僅僅須要讓實體類繼承 Model 類且實現主鍵指定方法,便可開啓 AR 之旅 。sql
下面咱們來介紹詳細內容,首先按照快速開始——Spring集成Mybatis-Plus
一節的操做,新建一個mp04
的 Module
,能夠將mp02
中的內容所有複製過來,而後進行下面的修改:數據庫
修改 Employee
實體類:apache
/**
* mybatis-plus 默認會使用實體類的小寫類名做爲表名
*
* 繼承 Model 類,開啓AR模式
*/
@Data
@ToString
public class Employee extends Model<Employee> {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Integer age;
// 當前字段是否在數據庫中存在,若是不存在則忽略該字段插入到數據庫中
@TableField(exist = false)
private Double salary;
// 注意:咱們這裏須要重寫pkVal()方法,return當前類的主鍵。 在實際的實踐中,發現若是未重寫pkVal()方法,並不會影響AR的使用
@Override
protected Serializable pkVal() {
return this.id;
}
}
複製代碼
注意
:mybatis-plus 默認會使用實體類的小寫類名做爲表名,由於咱們在 applicationContext.xml
中已經配置了表的前綴生成策略mybatis
<bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
<!-- 全局表主鍵生成策略 -->
<property name="idType" value="AUTO"></property>
<!-- 全局的表前綴策略配置 -->
<property name="tablePrefix" value="tbl_"></property>
</bean>
複製代碼
因此,不會出現數據庫表和實體類不匹配的情況,若是想要使用單獨的表名,能夠在實體類上使用@TableName("表名")
註解。app
@TableName("tbl_employee")
public class Employee extends Model<Employee>{
// .. fields
// .. getter and setter
@Override
protected Serializable pkVal() {
return this.id;
}
}
複製代碼
注意
:mapper不用作修改,可是也不能少,雖然AR模式用不到該接口,但必定要定義,不然使用AR時會報空指針異常。
public interface EmployeeMapper extends BaseMapper<Employee> {
}
複製代碼
修改mp04的pom.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mybatis-plus-in-action</artifactId>
<groupId>com.demo.mybatis-plus</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mp04</artifactId>
<dependencies>
<!-- mp 依賴
mybatis-plus 會自動維護mybatis 以及 mybatis-spring相關的依賴
Mybatis 及 Mybatis-Spring 依賴請勿加入項目配置,以避免引發版本衝突!!!Mybatis-Plus 會自動幫你維護!
-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
複製代碼
刪除TestMp.java
中的內容,方便咱們來進行測試。
上面的準備工做作完了,下面咱們能夠來使用AR進行操做了:
/**
* AR 插入操做
*/
@Test
public void testARInsert() {
Employee employee = new Employee();
employee.setGender(1);
employee.setLastName("ar_insert");
boolean b = employee.insert();
System.out.println(b);
}
複製代碼
注意
:AR操做是經過對象自己調用相關方法,AR加強了實體類的功能,好比要insert一個Employee,那就用這個Employee調用insert方法便可。能夠看到在上面的代碼中並不須要注入mapper接口,不過正如以前所說,不使用但仍是要定義,不然會報錯。
/**
* AR 修改操做
*/
@Test
public void testARUpdate() {
Employee employee = new Employee();
employee.setId(27);
employee.setLastName("李老師");
employee.setGender(2);
employee.setAge(23);
boolean r = employee.updateById();
System.out.println(r);
}
複製代碼
/**
* AR 查詢操做
*/
@Test
public void testARSelect() {
Employee employee = new Employee();
// 一、查詢操做,直接根據id查詢 selectById(id)
Employee result = employee.selectById(14);
System.out.println(result);
// 二、查詢操做
employee.setId(23);
Employee employee1 = employee.selectById();
System.out.println(employee1);
//三、查詢操做 selectAll
List<Employee> employees = employee.selectAll();
System.out.println(employees);
//四、查詢操做 selectList
List<Employee> employeeList = employee.selectList(
new QueryWrapper<Employee>().like("last_name", "M"));
System.out.println(employeeList);
//五、統計查詢
Integer count = employee.selectCount(
new QueryWrapper<Employee>().like("last_name", "M"));
System.out.println(count);
}
複製代碼
/**
* AR 刪除操做
*/
@Test
public void testARDelete() {
// 一、根據id刪除
Employee employee = new Employee();
boolean deleteById = employee.deleteById(25);
System.out.println(deleteById);
//二、根據id刪除
employee.setId(26);
boolean delete = employee.deleteById();
System.out.println(delete);
//三、wrapper 刪除
boolean delete1 = employee.delete(
new QueryWrapper<Employee>().like("last_name", "M"));
System.out.println(delete1);
}
複製代碼
/**
* AR 複雜分頁操做
*/
@Test
public void testARPage() {
Employee employee = new Employee();
IPage<Employee> page = employee.selectPage(
new Page<>(1, 2),
new QueryWrapper<Employee>().like("email", ".com"));
System.out.println(page.getRecords());
}
複製代碼
public class TestMp {
private ApplicationContext ioc = new
ClassPathXmlApplicationContext("applicationContext.xml");
private EmployeeMapper employeeMapper = ioc.getBean("employeeMapper", EmployeeMapper.class);
/**
* AR 複雜分頁操做
*/
@Test
public void testARPage() {
Employee employee = new Employee();
IPage<Employee> page = employee.selectPage(
new Page<>(1, 2),
new QueryWrapper<Employee>().like("email", ".com"));
System.out.println(page.getRecords());
}
/**
* AR 刪除操做
*/
@Test
public void testARDelete() {
// 一、根據id刪除
Employee employee = new Employee();
boolean deleteById = employee.deleteById(25);
System.out.println(deleteById);
//二、根據id刪除
employee.setId(26);
boolean delete = employee.deleteById();
System.out.println(delete);
//三、wrapper 刪除
boolean delete1 = employee.delete(
new QueryWrapper<Employee>().like("last_name", "M"));
System.out.println(delete1);
}
/**
* AR 查詢操做
*/
@Test
public void testARSelect() {
Employee employee = new Employee();
// 一、查詢操做,直接根據id查詢 selectById(id)
Employee result = employee.selectById(14);
System.out.println(result);
// 二、查詢操做
employee.setId(23);
Employee employee1 = employee.selectById();
System.out.println(employee1);
//三、查詢操做 selectAll
List<Employee> employees = employee.selectAll();
System.out.println(employees);
//四、查詢操做 selectList
List<Employee> employeeList = employee.selectList(
new QueryWrapper<Employee>().like("last_name", "M"));
System.out.println(employeeList);
//五、統計查詢
Integer count = employee.selectCount(
new QueryWrapper<Employee>().like("last_name", "M"));
System.out.println(count);
}
/**
* AR 修改操做
*/
@Test
public void testARUpdate() {
Employee employee = new Employee();
employee.setId(27);
employee.setLastName("李老師");
employee.setGender(2);
employee.setAge(23);
boolean r = employee.updateById();
System.out.println(r);
}
/**
* AR 插入操做
*/
@Test
public void testARInsert() {
Employee employee = new Employee();
employee.setGender(1);
employee.setLastName("ar_insert");
boolean b = employee.insert();
System.out.println(b);
}
}
複製代碼
完成上面的操做後,mp04的代碼結構以下所示:
至此,基於 mybatis-plus
的ActiveRecord(活動記錄)
演示就完成了,下面咱們就能夠進入到下一節代碼生成器
的學習了。
相關示例完整代碼:mybatis-plus-in-action