MyBatis Plus 入門(mén)使用詳細(xì)教程
一、MyBatis Plus 介紹
MyBatis Plus 是國(guó)內(nèi)人員開(kāi)發(fā)的 MyBatis 增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。
MyBatis Plus 的核心功能有:支持通用的 CRUD、代碼生成器與條件構(gòu)造器。
通用 CRUD:定義好 Mapper 接口后,只需要繼承 BaseMapper<T>
接口即可獲得通用的增刪改查功能,無(wú)需編寫(xiě)任何接口方法與配置文件條件構(gòu)造器:通過(guò) EntityWrapper<T>
(實(shí)體包裝類),可以用于拼接 SQL 語(yǔ)句,并且支持排序、分組查詢等復(fù)雜的 SQL代碼生成器:支持一系列的策略配置與全局配置,比 MyBatis 的代碼生成更好用
BaseMapper<T>
接口中通用的 CRUD 方法:
二、MyBatis Plus 集成 Spring
數(shù)據(jù)表結(jié)構(gòu)
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
文件
<dependencies> <!-- MP --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.3</version> </dependency> <!-- 測(cè)試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 數(shù)據(jù)源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- Spring 相關(guān) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.9.RELEASE</version> </dependency> </dependencies>
MyBatis 全局配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 不作任何配置 --> <configuration />
數(shù)據(jù)源 db.properties
jdbc.url=jdbc:mysql://localhost:3306/mp jdbc.username=root jdbc.password=1234
Spring 配置文件 applicationContext.xml
<!-- 數(shù)據(jù)源 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- MP 提供的 MybatisSqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"> <!-- 數(shù)據(jù)源 --> <property name="dataSource" ref="dataSource"></property> <!-- mybatis 全局配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 別名處理 --> <property name="typeAliasesPackage" value="com.jas.bean"></property> <!-- 注入全局MP策略配置 --> <property name="globalConfig" ref="globalConfiguration"></property> <!-- 插件注冊(cè) --> <property name="plugins"> <list> <!-- 注冊(cè)分頁(yè)插件 --> <bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" /> <!-- 注入 SQL 性能分析插件,建議在開(kāi)發(fā)環(huán)境中使用,可以在控制臺(tái)查看 SQL 執(zhí)行日志 --> <bean class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"> <property name="maxTime" value="1000" /> <!--SQL 是否格式化 默認(rèn)false--> <property name="format" value="true" /> </bean> </list> </property> </bean> <!-- 定義 MybatisPlus 的全局策略配置--> <bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration"> <!-- 在 2.3 版本以后,dbColumnUnderline 默認(rèn)值是 true --> <property name="dbColumnUnderline" value="true"></property> <!-- 全局的主鍵策略 --> <property name="idType" value="0"></property> <!-- 全局的表前綴策略配置 --> <property name="tablePrefix" value="tbl_"></property> </bean> <!-- 配置mybatis 掃描mapper接口的路徑 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.jas.mapper"></property> </bean>
三、快速體驗(yàn) MyBatis Plus
實(shí)體類 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<Employee> {}
在測(cè)試類中生成測(cè)試的 mapper 對(duì)象
private ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); private EmployeeMapper employeeMapper = context.getBean("employeeMapper", EmployeeMapper.class);
簡(jiǎn)單查詢測(cè)試
@Test public void getEmpByIdTest() { Employee employee = employeeMapper.selectById(1); System.out.println(employee); }
分頁(yè)查詢測(cè)試
@Test public void getEmpByPage() { Page<?> page = new Page<>(1, 5); List<Employee> list = employeeMapper.selectPage(page, null); System.out.println("總記錄數(shù):" + page.getTotal()); System.out.println("總頁(yè)數(shù)" + page.getPages()); System.out.println(list); }
條件構(gòu)造器測(cè)試
@Test public void getEmpByName() { EntityWrapper<Employee> wrapper = new EntityWrapper<>(); // 'last_name' 與 'age' 對(duì)應(yīng)數(shù)據(jù)庫(kù)中的字段 wrapper.like("last_name", "張"); wrapper.eq("age", 20); List<Employee> list = employeeMapper.selectList(wrapper); System.out.println(list); }
控制臺(tái)輸出的 SQL 分析日志
上面幾個(gè)例子中,并沒(méi)有在 EmployeeMapper
接口中定義任何方法,也沒(méi)有在配置文件中編寫(xiě) SQL 語(yǔ)句,而是通過(guò)繼承 BaseMapper<T>
接口獲得通用的的增刪改查方法,復(fù)雜的 SQL 也可以使用條件構(gòu)造器拼接。
通過(guò)這兩種方式已經(jīng)能夠滿足很多的開(kāi)發(fā)需求了,不過(guò)復(fù)雜的業(yè)務(wù)需求還是要編寫(xiě) SQL 語(yǔ)句的,流程和 MyBatis 一樣。
PS:
完整的代碼(mybatis-plus-demo
目錄)傳到了 GitHub,包括 SQL 表結(jié)構(gòu)與數(shù)據(jù)文件,點(diǎn)我前往~
總結(jié)
相關(guān)文章
springboot+springsecurity+mybatis+JWT+Redis?實(shí)現(xiàn)前后端離實(shí)戰(zhàn)教程
這篇文章主要介紹了springboot+springsecurity+mybatis+JWT+Redis?實(shí)現(xiàn)前后端離實(shí)戰(zhàn)教程,需要的朋友可以參考下2024-01-01java利用注解實(shí)現(xiàn)簡(jiǎn)單的excel數(shù)據(jù)讀取
這篇文章主要為大家詳細(xì)介紹了java利用注解實(shí)現(xiàn)簡(jiǎn)單的excel數(shù)據(jù)讀取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Java爬蟲(chóng)實(shí)戰(zhàn)抓取一個(gè)網(wǎng)站上的全部鏈接
這篇文章主要介紹了JAVA使用爬蟲(chóng)抓取網(wǎng)站網(wǎng)頁(yè)內(nèi)容的方法,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-10-10Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解
這篇文章主要介紹了Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05利用Java工具類Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能
這篇文章主要介紹了利用Java工具類Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能,利用Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn),校驗(yàn)的Servlet與今天的第一篇是一樣的,唯一就是驗(yàn)證碼的生成是不一樣的,利用Hutool生成驗(yàn)證碼更快捷.需要的朋友可以參考下2022-10-10Spring Cloud 系列之服務(wù)調(diào)用 OpenFeign的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 系列之服務(wù)調(diào)用 OpenFeign的實(shí)現(xiàn),需要的朋友可以參考下2020-11-11Java數(shù)據(jù)結(jié)構(gòu)之有向圖設(shè)計(jì)與實(shí)現(xiàn)詳解
有向圖是具有方向性的圖,由一組頂點(diǎn)和一組有方向的邊組成,每條方向的邊都連著一對(duì)有序的頂點(diǎn)。本文為大家介紹的是有向圖的設(shè)計(jì)與實(shí)現(xiàn),需要的可以參考一下2022-11-11Java線程池submit阻塞獲取結(jié)果的實(shí)現(xiàn)原理詳解
Java線程池中提交任務(wù)運(yùn)行,通常使用execute()方法就足夠了。那如果想要實(shí)現(xiàn)在主線程中阻塞獲取線程池任務(wù)運(yùn)行的結(jié)果,該怎么辦呢?本文就來(lái)和大家一起討論討論2022-10-10