mybatis-plus @select動態(tài)查詢方式
mybatis-plus @select 動態(tài)查詢
@Select({"<script> select cor.risk_id,cor.create_by as leader,cor.create_time as put_forward_time," +
"cor.correct_user_name as handle,cor.update_time as handle_time," +
"cor.correct_end_time,cor.correct_status,risk.risk_name,rt.risk_name as risk_type,pro.pro_code," +
"pro.pro_name,pro.pro_leader_name as pro_leader,pt.type_name as pro_type from data_risk_correct cor " +
"left join data_project_risk risk on cor.risk_id=risk.risk_id " +
"left join data_risk_type rt on risk.risk_type_id=rt.risk_id " +
"left join data_project pro on risk.pro_id=pro.pro_id " +
"left join data_project_type pt on pro.pro_type_id=pt.type_id " +
"<where>"+
"<if test='riskCenterVo.riskName != null and riskCenterVo.riskName !=\"\"'>" +
" and risk.risk_name like concat('%',#{riskCenterVo.riskName},'%') " +
"</if>" +
"<if test='riskCenterVo.proName != null and riskCenterVo.proName !=\"\"'>" +
" and pro.pro_name like concat('%',#{riskCenterVo.proName},'%') " +
"</if>" +
"<if test='riskCenterVo.proCode != null and riskCenterVo.proCode !=\"\"'>" +
" and pro.pro_code =#{riskCenterVo.proCode} " +
"</if>" +
"<if test='riskCenterVo.correctStatus != null '>" +
" and cor.correct_status=#{riskCenterVo.correctStatus} " +
"</if>" +
"</where>" +
"and cor.is_delete=0 and cor.correct_user_id=#{userId}" +
"</script>"})
List<RiskCenterVo> selectRiskCenterList(@Param("riskCenterVo") RiskCenterVo riskCenterVo,@Param("userId") String userId);SpringBoot+MyBatis動態(tài)查詢支持的通用方法
這幾天研究使用了一下 springBoot + MyBatis動態(tài)注解.
看了好多人說myBatis不支持動態(tài),其實不然, 我個人不喜歡太多配置, 所以一慣喜歡使用注解模式, 但spring體系當(dāng)中注解實在太多了, 其實常用的也就那么幾個.
呵呵又跑題了.回來
package cn.miw.rpc.batis.comm;
import java.util.List;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
/**
* 通用Mapper基礎(chǔ)接口,使用范型,其他Mapper繼承即可
* @author mrzhou
*
* @param <T>
*/
public interface GeneralMapper<T> {
@InsertProvider(method="insert",type=SQLGen.class)
@Options(useGeneratedKeys=true,keyProperty="id")
int save(T t);
@DeleteProvider(method="del",type=SQLGen.class)
int del(T t);
@UpdateProvider(method="update",type=SQLGen.class)
int update(T t);
@SelectProvider(method="select",type=SQLGen.class)
List<T> list(T t);
}我個常用的也就是CRUD這4個方法, 其他的Mapper方法你可以在繼承中再繼續(xù)寫吧, 那些就是大家常用的可以寫在繼承接口當(dāng)中.
這里我寫了一個通用的SQLProvider類SQLGen.java
package cn.miw.rpc.batis.comm;
import java.lang.reflect.Field;
import org.apache.ibatis.jdbc.SQL;
/**
* 常規(guī)CRUD四個方法
* @author mrzhou
*
* @param <T>
*/
public class SQLGen<T> {
public String select(T object) {
return new SQL() {
{
SELECT("*");
FROM(object.getClass().getSimpleName());
try {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object v = field.get(object);
if (v != null) {
String fieldName = field.getName();
if (v instanceof String && ((String)v).contains("%")) {
WHERE(fieldName + " like '"+v+"'" );
} else {
WHERE(fieldName + "=#{" + fieldName + "}");
}
}
}
} catch (Exception e) {
}
}
}.toString();
}
public String update(T object) {
return new SQL() {
{
UPDATE(object.getClass().getSimpleName());
try {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object v = field.get(object);
if (v != null) {
String fieldName = field.getName();
SET(fieldName + "=#{" + fieldName + "}");
}
}
} catch (Exception e) {
}
WHERE("id=#{id}");
}
}.toString();
}
public String insert(T object) {
return new SQL() {
{
INSERT_INTO(object.getClass().getSimpleName());
try {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object v = field.get(object);
if (v != null) {
String fieldName = field.getName();
VALUES(fieldName,"#{"+fieldName+"}");
}
}
} catch (Exception e) {
}
}
}.toString();
}
public String del(T object) {
return new SQL() {
{
DELETE_FROM(object.getClass().getSimpleName());
try {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object v = field.get(object);
if (v != null) {
String fieldName = field.getName();
if (v instanceof String && ((String)v).contains("%")) {
WHERE(fieldName + " like '"+v+"'" );
} else {
WHERE(fieldName + "=#{" + fieldName + "}");
}
}
}
} catch (Exception e) {
}
}
}.toString();
}
}在調(diào)用Mapper方法時傳入相應(yīng)的實體, 如果字段類型為String且包含%, 將使用like 進(jìn)行查詢, 該操作僅對select和delete操作有效. insert,update則不受此限制, '%'百分號將作為內(nèi)容被保存進(jìn)數(shù)據(jù)庫
在對應(yīng)的Service中我們只需要這樣使用
User user = new User();
user.setName("張%");// 或者user.setName("%趙%");
List<User> list = userMapper.list(user);是不是很方便呢?
當(dāng)然你的其他方法可以繼續(xù)在相應(yīng)的Mapper中繼續(xù)描述
package cn.miw.rpc.batis.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.miw.rpc.batis.comm.GeneralMapper;
import cn.miw.rpc.model.User;
/**
* 用戶Mapper,定義其他常規(guī)的方便方法
* @author mrzhou
*
*/
@Mapper
public interface UserMapper extends GeneralMapper<User> {
@Insert("insert into User(name,age) values(#{name},#{age})")
int addUser(@Param("name") String name, @Param("age") int age);
@Select("select * from User where id =#{id}")
User findById(@Param("id") int id);
@Update("update User set name=#{name} where id=#{id}")
void updataById(@Param("id") int id, @Param("name") String name);
@Delete("delete from User where id=#{id}")
void deleteById(@Param("id") int id);
}各位看包名, 其實我這個假期是在研究一些rpc的東西, 順帶折騰了一下mybatis.
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- mybatis plus動態(tài)數(shù)據(jù)源切換及查詢過程淺析
- MybatisPlus實現(xiàn)分頁查詢和動態(tài)SQL查詢的示例代碼
- MyBatis-Plus多表聯(lián)查的實現(xiàn)方法(動態(tài)查詢和靜態(tài)查詢)
- Mybatis-plus動態(tài)條件查詢QueryWrapper的使用案例
- MyBatis-Plus多表聯(lián)查(動態(tài)查詢)的項目實踐
- MybatisPlus使用Mybatis的XML的動態(tài)SQL的功能實現(xiàn)多表查詢
- mybatis-plus?實現(xiàn)查詢表名動態(tài)修改的示例代碼
相關(guān)文章
Spring?AOP實現(xiàn)聲明式事務(wù)機(jī)制源碼解析
這篇文章主要為大家介紹了Spring?AOP實現(xiàn)聲明式事務(wù)機(jī)制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
解決DataInputStream?read不等于-1,socket文件傳輸只能傳輸一個文件無法傳輸多個問題
這篇文章主要介紹了解決DataInputStream?read不等于-1,socket文件傳輸只能傳輸一個文件無法傳輸多個問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決
這篇文章主要介紹了springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java編程實現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例
這篇文章主要介紹了Java編程實現(xiàn)的二維數(shù)組轉(zhuǎn)置功能,結(jié)合實例形式分析了Java二維數(shù)組的遍歷、運(yùn)算、賦值等實現(xiàn)轉(zhuǎn)置的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Java中的System.getenv()和System.getProperty()使用詳解
文章介紹了Java中用于讀取環(huán)境配置信息的兩種方法:System.getenv()和System.getProperty(),前者讀取系統(tǒng)環(huán)境變量,返回一個不可修改的Map;后者獲取JVM環(huán)境變量值,可以通過-D參數(shù)設(shè)置,文章還提到,通過這兩種方法可以簡化配置,不需要修改代碼2024-11-11
Java多線程提交按照時間順序獲取線程結(jié)果詳解流程
在工作中是否存在這樣的場景,多個線程提交執(zhí)行,你不想全部線程執(zhí)行結(jié)束了獲取結(jié)果,而是有線程完成返回結(jié)果就獲取消費(fèi)。本文提供該場景的工具類,可以直接用哦2021-11-11

