Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何在自定义的Mapper对应的Provider中,获取Condition(Example)条件和entityClass等参数? #21

Closed
mataoxf opened this issue May 6, 2016 · 1 comment

Comments

@mataoxf
Copy link

mataoxf commented May 6, 2016

如何在自定义的Mapper对应的Provider中,获取Condition(Example)条件和entityClass等参数?

public class MyMybatisProvider extends MapperTemplate{

    private ExampleProvider exampleProvider;

    public MyMybatisProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
        exampleProvider = new ExampleProvider(mapperClass, mapperHelper);
    }

    /**
     * 
     * @desc 根据Condition条件,查询未(逻辑)删除的数据
     * @param ms
     * @return
     */
    public String selectNoDeleteByCondition(MappedStatement ms) {

        // 我想在这里增加Condition的条件,
        //  如果Condition为NULL时还需要获取entityClass来构造Condition
        return exampleProvider.selectByExample(ms);
    }

}
@mataoxf
Copy link
Author

mataoxf commented May 6, 2016

在作者的指导下搞定了。

    public String selectNoDeleteByCondition(MappedStatement ms) {
         Class<?> entityClass = getEntityClass(ms);
            //将返回值修改为实体类型
            setResultType(ms, entityClass);
            StringBuilder sql = new StringBuilder("SELECT ");
            sql.append("<if test=\"distinct\">distinct</if>");
            //支持查询指定列
            sql.append(SqlHelper.exampleSelectColumns(entityClass));
            sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
            sql.append("<where>\n")
            .append(" (del_flag is null or del_flag!=1) \n")
            .append("<trim prefix=\" and (\" suffix=\") \">\n");
            //去掉SqlHelper.exampleWhereClause方法中的<where></where>标签
            String whereClause = SqlHelper.exampleWhereClause();
            whereClause = StringUtils.removeAll(whereClause, "<where>","</where>");
            sql.append(whereClause)
            .append("</trim>\n")
            .append("</where>")
            .append(SqlHelper.orderByDefault(entityClass))
            .append(SqlHelper.exampleOrderBy(entityClass));
            return sql.toString();
    }

@abel533 abel533 closed this as completed Aug 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants