mybatis provider list 参数的使用
来源:互联网 发布:python数学库代码 编辑:程序博客网 时间:2024/05/17 07:59
使用mybatis provider 时因为传入的参数只能是map类型,所以如果查询条件中有list就需要放入map中再传入
记录一下传入list参数后如何获取list参数中的值
首先取得传入的list参数
final List<Integer> userIds = params.get("userIds")==null?null: (List<Integer>) params.get("userIds");然后在拼接sql语句的时候使用userIds[0]的方式拼接,生成sql语句之后就能自动按照list的顺序将值赋给sql语句
if (userIds != null && userIds.size()>0) { String str = ""; for (int i = 0; i < userIds.size(); i++) { if (Objects.equals(str, "")) { str = "(#{userIds["+i+"]}"; } else { str += ", #{userIds["+i+"]}"; } } str += ")"; WHERE("u.userid in "+str); }
顺便,写一下子查询
new SQL(){{ SELECT("*"); FROM("user u"); JOIN("("+new SQL(){{ SELECT("userid,finishyear,ROUND(AVG(score)) as avgyear"); FROM("work"); WHERE("finishtime is NOT NULL"); if (finishyear != null) { WHERE("finishyear=#{finishyear}"); } GROUP_BY("userid,finishyear"); }}.toString()+") y on u.userid = y.userid");
}}.toString();顺便,再写一下mybatis generator生成provider 时自定义分页的添加
在自定义分页类中重写providerSelectByExampleWithBLOBsMethodGenerated和providerSelectByExampleWithoutBLOBsMethodGenerated方法即可
一下是分页全部代码,BootPage为自定义类有offset和limit两个属性即可
/** * * MyBatise针对MySQL的分页插件 * * @author Helios * @date 2014年11月20日 下午2:10:55 * */public class MySQLPaginationPlugin extends PluginAdapter{/** * Page类所在的类路径:其中Page类中一定要有begin和end属性(即:开始记录位置和结束记录位置) */public static final String pageClassPath = "com.partwork.common.plugin.page.BootPage";/** * 给自动生成Example对象添加page对象的set和get方法 * @param topLevelClass * @param introspectedTable * @return */@Overridepublic boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable){// add field, getter, setter for limit clauseaddPage(topLevelClass, introspectedTable, "page");return super.modelExampleClassGenerated(topLevelClass, introspectedTable);}/** * 生成xml文件SelectByExample方法自动添加分页节点 * @param element * @param introspectedTable * @return */@Overridepublic boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable){XmlElement page = new XmlElement("if");page.addAttribute(new Attribute("test", "page != null"));page.addElement(new TextElement("limit #{page.offset} , #{page.limit}"));element.addElement(page);return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);}@Overridepublic boolean providerSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {addLimitLines(method, topLevelClass, introspectedTable);return super.providerSelectByExampleWithBLOBsMethodGenerated(method, topLevelClass, introspectedTable);}@Overridepublic boolean providerSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {addLimitLines(method, topLevelClass, introspectedTable);return super.providerSelectByExampleWithoutBLOBsMethodGenerated(method, topLevelClass, introspectedTable);}private void addLimitLines(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {List<String> limits = Arrays.asList("if (example != null && example.getPage() != null) {","return sql.toString()+\" limit \"+example.getPage().getOffset()+\",\"+example.getPage().getLimit();","}");method.addBodyLines(method.getBodyLines().size()-1,limits);}/** * 对Example对象添加page属性 * @param topLevelClass * @param introspectedTable * @param name */private void addPage(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name){topLevelClass.addImportedType(new FullyQualifiedJavaType(pageClassPath));CommentGenerator commentGenerator = context.getCommentGenerator();Field field = new Field();field.setVisibility(JavaVisibility.PROTECTED);field.setType(new FullyQualifiedJavaType(pageClassPath));field.setName(name);commentGenerator.addFieldComment(field, introspectedTable);topLevelClass.addField(field);char c = name.charAt(0);String camel = Character.toUpperCase(c) + name.substring(1);Method method = new Method();method.setVisibility(JavaVisibility.PUBLIC);method.setName("set" + camel);method.addParameter(new Parameter(new FullyQualifiedJavaType(pageClassPath), name));method.addBodyLine("this." + name + "=" + name + ";");commentGenerator.addGeneralMethodComment(method, introspectedTable);topLevelClass.addMethod(method);method = new Method();method.setVisibility(JavaVisibility.PUBLIC);method.setReturnType(new FullyQualifiedJavaType(pageClassPath));method.setName("get" + camel);method.addBodyLine("return " + name + ";");commentGenerator.addGeneralMethodComment(method, introspectedTable);topLevelClass.addMethod(method);}/** * This plugin is always valid - no properties are required */public boolean validate(List<String> warnings){return true;}}生成xml的插件内容使用,请参考:
http://blog.csdn.net/xzknet/article/details/44158009
阅读全文
0 0
- mybatis provider list 参数的使用
- Mybatis的参数使用
- 在mybatis中传多个参数,并且遍历其中的list和数组,使用map集合
- mybatis复合参数 map中包含list的参数
- 在mybatis查询时使用map以及list参数
- mybatis 参数为list时,校验list是否为空, mybatis ${}与#{}的区别
- mybatis使用Map传多个参数的使用
- mybatis传值List类型的参数如何遍历
- mybatis 查询参数为list、数组、map的情况
- content provider 的使用
- content provider的使用!
- Java使用MyBatis传入参数的问题
- Mybatis的使用mapper传多个参数
- MyBatis-sqlMapper传入List类型参数,返回List类型参数。
- mybatis使用字符串参数
- mybatis xml 参数 使用
- 总结Content Provider的使用
- 总结Content Provider的使用
- siki学院A计划
- c++面向对象三大特征封装、继承和多态知识总结
- 抓包工具PowerSniff-0.1
- Bootstrap(四十五)
- method.invoke(owner, args)
- mybatis provider list 参数的使用
- pclint vc6/linux 工程,测试正常
- Bootstrap(四十六)
- EOJ 3303 1的个数最多的整数
- libtcc使用问题一二
- 可以管理时间的二叉堆
- trace工具,c++/c#/python
- java实际项目中interface和abstract interface 区别
- Bootstrap(四十七)