mybatis-plus @select Dynamic query
@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=' != null and !=\"\"'>" + " and risk.risk_name like concat('%',#{},'%') " + "</if>" + "<if test=' != null and !=\"\"'>" + " and pro.pro_name like concat('%',#{},'%') " + "</if>" + "<if test=' != null and !=\"\"'>" + " and pro.pro_code =#{} " + "</if>" + "<if test=' != null '>" + " and cor.correct_status=#{} " + "</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);
Common methods supported by SpringBoot+MyBatis dynamic query
The research has used springBoot + MyBatis dynamic annotation in the past few days.
I have read many people saying that myBatis does not support dynamics. In fact, it is not the case. I personally don’t like too many configurations, so I always like to use annotation mode. However, there are too many annotations in the spring system, and in fact there are only a few commonly used ones.
Haha, I'm off topic again. Come back
package ; import ; import ; import ; import ; import ; import ; /** * General Mapper basic interface, use the paradigm, and other Mappers can be inherited * @author mrzhou * * @param <T> */ public interface GeneralMapper<T> { @InsertProvider(method="insert",type=) @Options(useGeneratedKeys=true,keyProperty="id") int save(T t); @DeleteProvider(method="del",type=) int del(T t); @UpdateProvider(method="update",type=) int update(T t); @SelectProvider(method="select",type=) List<T> list(T t); }
My commonly used four methods are CRUD. You can continue to write other Mapper methods in inheritance. Those are the ones that everyone uses that can be written in the inheritance interface.
Here I wrote a general SQLProvider class
package ; import ; import ; /** * Four methods of regular CRUD * @author mrzhou * * @param <T> */ public class SQLGen<T> { public String select(T object) { return new SQL() { { SELECT("*"); FROM(().getSimpleName()); try { Field[] fields = ().getDeclaredFields(); for (Field field : fields) { (true); Object v = (object); if (v != null) { String fieldName = (); 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(().getSimpleName()); try { Field[] fields = ().getDeclaredFields(); for (Field field : fields) { (true); Object v = (object); if (v != null) { String fieldName = (); SET(fieldName + "=#{" + fieldName + "}"); } } } catch (Exception e) { } WHERE("id=#{id}"); } }.toString(); } public String insert(T object) { return new SQL() { { INSERT_INTO(().getSimpleName()); try { Field[] fields = ().getDeclaredFields(); for (Field field : fields) { (true); Object v = (object); if (v != null) { String fieldName = (); VALUES(fieldName,"#{"+fieldName+"}"); } } } catch (Exception e) { } } }.toString(); } public String del(T object) { return new SQL() { { DELETE_FROM(().getSimpleName()); try { Field[] fields = ().getDeclaredFields(); for (Field field : fields) { (true); Object v = (object); if (v != null) { String fieldName = (); if (v instanceof String && ((String)v).contains("%")) { WHERE(fieldName + " like '"+v+"'" ); } else { WHERE(fieldName + "=#{" + fieldName + "}"); } } } } catch (Exception e) { } } }.toString(); } }
When calling the Mapper method, the corresponding entity is passed in. If the field type is String and contains %, it will be queried using like. This operation is only valid for select and delete operations. insert, update is not subject to this restriction, and the '%' percentage sign will be saved into the database as content
In the corresponding service we only need to use this
User user = new User(); ("open%");// Or ("%Zhao%");List<User> list = (user);
Isn't it very convenient?
Of course, your other methods can be continued to be described in the corresponding mapper.
package ; import ; import ; import ; import ; import ; import ; import ; import ; /** * User Mapper, define other common convenient methods * @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); }
Everyone, look at the name of the package. Actually, I am studying some RPC things this holiday, and I have tried mybatis.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.