mybatisplus之使用@Select解读
作者:强风吹拂str
这篇文章主要介绍了mybatisplus之使用@Select解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
mybatisplus使用@Select
在自己的mapper文件里面 比如我这个是ckDao.java文件,写个方法,然后就可以用 @Autowired 注册这个mapper,就会去执行sql语句
@Select("SELECT DISTINCT name FROM `ck` where company = '顺网' and deleted = 1") List<CkEntity> listCompany(); @Select("SELECT count(*) FROM `ck` where company = '顺网' and deleted = 1" + " and name like '%${name}%'") String countCompanyData(@Param("name") String name);
实现方法
@Override public XXX listCompanyData() { List<CkEntity> ckEntities = ckDao.listCompany(); return XXX; }
mybatisplus @Select注解参数判空及SQL拼接
场景
在@Select注解使用中,自定义sql时,会存在一个或多个查询条件的情况,这样在注解形式sql里,插入参数需要进行判空
实例
@Select({"<script>"+ "SELECT DISTINCT "+ "a.monitor_id monitorId," + "a.monitor_name monitorName," + "a.target," + "a.priority,"+ "a.gmt_create gmtCreate " + "FROM "+ "alert a,"+ "( SELECT monitor_id, max( gmt_create ) gmt_update FROM alert GROUP BY monitor-id ) b " + "WHERE " + "a.monitor_id = b.monitor_id "+ "AND a.gmt_create = b.gmt_update " + "AND DATEDIFF(gmt_create,NOW())=0 "+ "<when test='times!=null'>" + "AND times = #{times}"+ "</when>"+ "<when test='monitorName!=null'" + "AND monitor_name like concat('%',#(monitorName},'%')" "</when>"+ "<when test='priority!=null'" + "AND priority = #{priority}"+ "</when>"+ "</script>"}) List<IndicateDetail> getIndicatorList(@Param("monitorName")String monitor_name,@Param("times")String times,@Param("priority")String priority);
用script标签包围,然后像xml语法写
条件匹配 =用 #{}包装传进来的参数,如果是模糊查询,需要使用函数concat拼接%字符串
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。