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),其實(shí)不然, 我個人不喜歡太多配置, 所以一慣喜歡使用注解模式, 但spring體系當(dāng)中注解實(shí)在太多了, 其實(shí)常用的也就那么幾個.
呵呵又跑題了.回來
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)的實(shí)體, 如果字段類型為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);
}各位看包名, 其實(shí)我這個假期是在研究一些rpc的東西, 順帶折騰了一下mybatis.
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- mybatis plus動態(tài)數(shù)據(jù)源切換及查詢過程淺析
- MybatisPlus實(shí)現(xiàn)分頁查詢和動態(tài)SQL查詢的示例代碼
- MyBatis-Plus多表聯(lián)查的實(shí)現(xiàn)方法(動態(tài)查詢和靜態(tài)查詢)
- Mybatis-plus動態(tài)條件查詢QueryWrapper的使用案例
- MyBatis-Plus多表聯(lián)查(動態(tài)查詢)的項(xiàng)目實(shí)踐
- MybatisPlus使用Mybatis的XML的動態(tài)SQL的功能實(shí)現(xiàn)多表查詢
- mybatis-plus?實(shí)現(xiàn)查詢表名動態(tài)修改的示例代碼
相關(guān)文章
Java SpringBoot安全框架整合Spring Security詳解
這篇文章主要介紹了Spring Boot整合Spring Security的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09
java注釋轉(zhuǎn)json插件開發(fā)實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了java注釋轉(zhuǎn)json插件開發(fā)實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
SpringBoot如何使用RequestBodyAdvice進(jìn)行統(tǒng)一參數(shù)處理
這篇文章主要介紹了SpringBoot使用RequestBodyAdvice進(jìn)行統(tǒng)一參數(shù)處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringMVC的REST風(fēng)格的四種請求方式總結(jié)
下面小編就為大家?guī)硪黄猄pringMVC的REST風(fēng)格的四種請求方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
spring boot+vue 的前后端分離與合并方案實(shí)例詳解
這篇文章主要介紹了spring boot+vue 的前后端分離與合并方案實(shí)例詳解,需要的朋友可以參考下2017-11-11
詳解Java數(shù)組的一維和二維講解和內(nèi)存顯示圖
這篇文章主要介紹了Java數(shù)組的一維和二維講解和內(nèi)存顯示圖,數(shù)組就相當(dāng)于一個容器,存放相同類型數(shù)據(jù)的容器。而數(shù)組的本質(zhì)上就是讓我們能 "批量" 創(chuàng)建相同類型的變量,需要的朋友可以參考下2023-05-05

