mybatis plus 是中國程序員對 mybatis 的做的改變,往 hibernate 的方向靠攏了一下,宗旨是只做增強,不做改變,確實蠻好用的,下面介紹使用方法
?
1、導入依賴
<!--mybatis plus的依賴--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatisplus.version}</version><exclusions><exclusion><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId></exclusion></exclusions></dependency>
?
2、application.yml 配置,大家按照自己的環境配置,不配置也可以,有默認配置的
# 容器服務 server:port: 8002servlet:context-path: /xiongdi-api# spring 配置 spring:profiles:active: dev # 使用 application-dev.yml配置servlet:multipart: # 文件上傳和下載enabled: truemax-file-size: 100MBmax-request-size: 100MB# 自定義配置 xiongdi:redis:open: false # 是否開啟緩存 true 開啟 false 關閉# mybatis plus的配置 mybatis-plus:# mybatis 的原生配置configuration:cache-enabled: false # 是否開啟緩存map-underscore-to-camel-case: true # 是否開啟自動駝峰命名規則(camel case)映射call-setters-on-nulls: true # MyBatis 在使用 resultMap 來映射查詢結果中的列,如果查詢結果中包含空值的列jdbc-type-for-null: 'null' # 如果type為空# MyBatis-Plus 全局策略配置global-config:# MyBatis-Plus 全局策略中的 DB 策略配置db-config:id-type: auto # 全局默認主鍵類型,AUTO:"數據庫ID自增", INPUT:"用戶輸入ID", ID_WORKER:"全局唯一ID (數字類型唯一ID)", UUID:"全局唯一ID UUID";field-strategy: not_null # 字段策略 IGNORED:"忽略判斷",NOT_NULL:"非 NULL 判斷"),NOT_EMPTY:"非空判斷"logic-delete-value: -1 # 邏輯已刪除值,(邏輯刪除下有)logic-not-delete-value: 0 # 邏輯未刪除值,(邏輯刪除下有效)banner: false # 是否顯示mybatis-plus的圖標
jpa和mybatisplus,?
3、定義 Mapper 接口 繼承 BaseMapper,繼承該接口后無需編寫 mapper.xml 文件,即可獲得CRUD功能,泛型 T 代表實體,也就是我們從數據庫查出來的數據用哪個類封裝
package io.xiongdi.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import io.xiongdi.entity.TokenEntity; import org.apache.ibatis.annotations.Mapper;/*** TokenEntity 的操作DAO層【繼承Base Mapper就具有了對TokenEntity的基本操作方法】* @author wujiaxing* @date 2019-06-30*/ @Mapper public interface TokenDao extends BaseMapper<TokenEntity> { }
?
4、如果有需要自行編寫的方法,也可以繼續定義 mapper.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="io.xiongdi.dao.TokenDao"></mapper>
?
5、編寫 Service 接口,繼承 IService,這個接口定義了很多默認抽象方法,如果方法沒有自己想要的,可以自定義,泛型 T 也是實體
package io.xiongdi.service;import com.baomidou.mybatisplus.extension.service.IService; import io.xiongdi.entity.TokenEntity;/*** token* @author wujiaxing* @date 2019-06-30*/ public interface TokenService extends IService<TokenEntity> {/*** <p>* 根據請求token查詢token信息* </p>* @param token* @return*/TokenEntity queryByToken(String token);/*** 創建token* @param userId 用戶ID* @return 返回token信息*/TokenEntity createToken(long userId);/*** 設置token過期* @param userId 用戶ID*/void expireToken(long userId); }
mybatis plus3。?
6、編寫 service 的實現類,除了實現我們定義的接口,還要繼承 ServiceImpl 類,該類泛型 M 表示你要告訴 mybatis plus 你的 Mapper 接口是哪個,一定要繼承 BaseMapper 接口,泛型 T 是實體
package io.xiongdi.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.xiongdi.dao.TokenDao; import io.xiongdi.entity.TokenEntity; import io.xiongdi.service.TokenService; import org.springframework.stereotype.Service;import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.util.Date; import java.util.UUID;/*** @author wujiaxing* @date 2019-07-08*/ @Service("tokenService") public class TokenServiceImpl extends ServiceImpl<TokenDao, TokenEntity> implements TokenService {/*** 12 小時過期 單位:毫秒*/private final static int EXPIRE = 3600 * 12 * 1000;/*** 根據請求頭的token查詢數據庫對應的token信息* @param token* @return*/@Overridepublic TokenEntity queryByToken(String token) {return this.getOne(new QueryWrapper<TokenEntity>().eq("token", token));}@Overridepublic TokenEntity createToken(long userId) {// 得到當前時間LocalDateTime now = LocalDateTime.now();// 根據過期時間加上當前時間,得到token的有效期long indate = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() + EXPIRE;LocalDateTime tokenExpireDateTime = LocalDateTime.ofInstant(new Date(indate).toInstant(), ZoneId.systemDefault());// 生成tokenString token = generateToken();// 創建實體對象TokenEntity tokenEntity = TokenEntity.builder().expireTime(tokenExpireDateTime).userId(userId).token(token).updateTime(now).build();// 放入數據庫保存this.saveOrUpdate(tokenEntity);return tokenEntity;}/*** 生成token* @return*/private String generateToken() {return UUID.randomUUID().toString().replace("-", "");}@Overridepublic void expireToken(long userId) {// 獲取當前時間LocalDateTime now = LocalDateTime.now();TokenEntity tokenEntity = TokenEntity.builder().userId(userId).expireTime(now).updateTime(now).build();this.saveOrUpdate(tokenEntity);} }
?
QueryWrapper 類定義了很多條件方法,例如:like、lt、gt、eq 等等
?