欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus @select動(dòng)態(tài)查詢方式

 更新時(shí)間:2024年05月10日 09:57:17   作者:仔仔程  
這篇文章主要介紹了mybatis-plus @select動(dòng)態(tài)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mybatis-plus @select 動(dòng)態(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動(dòng)態(tài)查詢支持的通用方法

這幾天研究使用了一下 springBoot + MyBatis動(dòng)態(tài)注解.

看了好多人說myBatis不支持動(dòng)態(tài),其實(shí)不然, 我個(gè)人不喜歡太多配置, 所以一慣喜歡使用注解模式, 但spring體系當(dāng)中注解實(shí)在太多了, 其實(shí)常用的也就那么幾個(gè).

呵呵又跑題了.回來

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);
	
}

我個(gè)常用的也就是CRUD這4個(gè)方法, 其他的Mapper方法你可以在繼承中再繼續(xù)寫吧, 那些就是大家常用的可以寫在繼承接口當(dāng)中.

這里我寫了一個(gè)通用的SQLProvider類SQLGen.java

package cn.miw.rpc.batis.comm;
 
import java.lang.reflect.Field;
 
import org.apache.ibatis.jdbc.SQL;
/**
 * 常規(guī)CRUD四個(gè)方法
 * @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方法時(shí)傳入相應(yīng)的實(shí)體, 如果字段類型為String且包含%, 將使用like 進(jìn)行查詢, 該操作僅對(duì)select和delete操作有效. insert,update則不受此限制, '%'百分號(hào)將作為內(nèi)容被保存進(jìn)數(shù)據(jù)庫

在對(duì)應(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);
}

各位看包名, 其實(shí)我這個(gè)假期是在研究一些rpc的東西, 順帶折騰了一下mybatis.

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?AOP實(shí)現(xiàn)聲明式事務(wù)機(jī)制源碼解析

    Spring?AOP實(shí)現(xiàn)聲明式事務(wù)機(jī)制源碼解析

    這篇文章主要為大家介紹了Spring?AOP實(shí)現(xiàn)聲明式事務(wù)機(jī)制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 解決DataInputStream?read不等于-1,socket文件傳輸只能傳輸一個(gè)文件無法傳輸多個(gè)問題

    解決DataInputStream?read不等于-1,socket文件傳輸只能傳輸一個(gè)文件無法傳輸多個(gè)問題

    這篇文章主要介紹了解決DataInputStream?read不等于-1,socket文件傳輸只能傳輸一個(gè)文件無法傳輸多個(gè)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決

    springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決

    這篇文章主要介紹了springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例

    Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例

    這篇文章主要介紹了Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能,結(jié)合實(shí)例形式分析了Java二維數(shù)組的遍歷、運(yùn)算、賦值等實(shí)現(xiàn)轉(zhuǎn)置的相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • springboot bean掃描路徑的實(shí)現(xiàn)

    springboot bean掃描路徑的實(shí)現(xiàn)

    這篇文章主要介紹了springboot bean掃描路徑的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java中的System.getenv()和System.getProperty()使用詳解

    Java中的System.getenv()和System.getProperty()使用詳解

    文章介紹了Java中用于讀取環(huán)境配置信息的兩種方法:System.getenv()和System.getProperty(),前者讀取系統(tǒng)環(huán)境變量,返回一個(gè)不可修改的Map;后者獲取JVM環(huán)境變量值,可以通過-D參數(shù)設(shè)置,文章還提到,通過這兩種方法可以簡化配置,不需要修改代碼
    2024-11-11
  • Java8的stream().map()用法詳解

    Java8的stream().map()用法詳解

    這篇文章主要介紹了Java8的stream().map()用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java實(shí)現(xiàn)List去重的方法詳解

    Java實(shí)現(xiàn)List去重的方法詳解

    本文用示例介紹Java的List(ArrayList、LinkedList等)的去重的方法。List去重的常用方法一般是:JDK8的stream的distinct、轉(zhuǎn)為HashSet、轉(zhuǎn)為TreeSet等,感興趣的可以了解一下
    2022-05-05
  • Java多線程提交按照時(shí)間順序獲取線程結(jié)果詳解流程

    Java多線程提交按照時(shí)間順序獲取線程結(jié)果詳解流程

    在工作中是否存在這樣的場景,多個(gè)線程提交執(zhí)行,你不想全部線程執(zhí)行結(jié)束了獲取結(jié)果,而是有線程完成返回結(jié)果就獲取消費(fèi)。本文提供該場景的工具類,可以直接用哦
    2021-11-11
  • 關(guān)于Java中的CAS如何使用

    關(guān)于Java中的CAS如何使用

    這篇文章主要介紹了關(guān)于Java中的CAS如何使用,CAS是Compare And Swap(比較并交換)的縮寫,是一種非阻塞式并發(fā)控制技術(shù),用于保證多個(gè)線程在修改同一個(gè)共享資源時(shí)不會(huì)出現(xiàn)競爭條件,從而避免了傳統(tǒng)鎖機(jī)制的各種問題,需要的朋友可以參考下
    2023-09-09

最新評(píng)論