小程序三方平台开发: 解析小程序开发的未来趋势和机遇
1366
2023-03-06
在Mybatis @Select注解中实现拼写动态sql
现在随着mybatis plus的应用,越来越多的弱化了SQL语句,对于单表操作可以说几乎不需要进行自己编写SQL语句了,但对于多表查询操作目前mybatis plus还没有很好的支持,还需要自己编写SQL语句,如:
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.shield.base.model.domain.MenuDO;
import com.shield.base.model.param.MenuTreeParam;
/**
* 基础数据操作对象
*
* @author xxx
* @date 2018/5/18
*/
@Mapper
public interface MenuDAO extends BaseMapper
/**
* 根据菜单编码获得所有下级菜单列表(包括本级)
* @param menuId 菜单编码
* @return 该菜单下的所有菜单列表(包括本级)
*/
@Select("WITH menuTree"
+ " AS"
+ "("
+ " SELECT menu1.father_rowid as id,menu1.son_rowid as parentId,menu1.system_name as menuName,"
+ "menu1.system_full_rowid as menuTreeFlat,menu1.level_value as menuLevel,menu1.homepage_statuhttp://s as homeStatus,"
+ "menu1.menu_status as menuType,menu1.sort as sort,menu1.duty_name as createName,"
+ "menu1.duty_datetime as createDate,menu1.update_datetime as updateDate,menu1.stop_status as status"
+ " FROM system_menu_setup menu1 WHERE menu1.father_rowid = #{menuId}"
+ " UNION ALL"
+ " SELECT menu2.father_rowid as id,menu2.son_rowid as parentId,menu2.system_name as menuName,"
+ "menu2.system_full_rowid as menuTreeFlat,menu2.level_value as menuLevel,menu2.homepage_status as homeStatus,"
+ "menu2.menu_status as menuType,menu2.sort as sort,menu2.duty_name as createName,"
+ "menu2.duty_datetime as createDate,menu2.update_datetime as updateDate,menu2.stop_status as status"
+ " FROM system_menu_setup menu2"
+ " INNER JOIN menuTree T ON menu2.son_rowid = T.id"
+ ")"
+ " SELECT id,parentId,menuName,MenuTreeFlat,menuLevel,homeStatus,menuType,sort,createName,"
+ "createDate,updateDate,status FROM menuTree")
List
}
这样整个语句基本上都是写死的,没有办法通过参数动态拼接SQL语句,在对于 相同语句不同参数来拼接SQL语句是十分不便的,而如果使用xml来配置的话可以用
and menu.stop_status=#{stopStatus} and roleMenu.stop_status=#{stopStatus}
and menuSource.menu_source=#{menuSource}
and roleUser.operator_rowid=#{userId}
但是现在很多公司可能会采用@Select注解方式来编写SQL语句,而非通过xml 的SQL Mapper,那对于@Select这种该如何做呢?其实很简单,只是需要用")
List
}
至此我们就可以像在xml文件里面一样愉快的动态拼接你想要的SQL语句了!
注意:
在@Select注解中采用
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~