mybatis/mybatis-dynamic-sql

He can't work properly. How should he write

hermer opened this issue · 5 comments

hermer commented
        select(OBJECT_1.ID, OBJECT_1.NAME)
                        .from(OBJECT_1)
                        .leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID))
                        .where(OBJECT_1.ID, isEqualTo("id"))
                        .build());

The code won't compile, There is no build method after where,How should he write?
version: 1.5.2

That's normal, but I want a line. Any suggestions

        QueryExpressionDSL<SelectModel> select = select(OBJECT_1.ID, OBJECT_1.NAME).from(OBJECT_1);
        select.leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID));
        select.where(OBJECT_1.ID, isEqualTo("11"));
        dao.selectByCriteria(select.build());

Expect

        dao.selectByCriteria(select(OBJECT_1.ID, OBJECT_1.NAME)
                        .from(OBJECT_1)
                        .leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID))
                        .where(OBJECT_1.ID, isEqualTo("id"))
                        .build()));

please do not post screen shots of images, they are not searchable by others. Post the query directly as text. I don't have any other answer for you. Maybe someone else will but do make this so its searchable for others.

hermer commented

please do not post screen shots of images, they are not searchable by others. Post the query directly as text. I don't have any other answer for you. Maybe someone else will but do make this so its searchable for others.

Thank. It has been modified

@hermer none of this makes any sense to me. There is an obvious typo in the first code snippet - build()) - and selectByCriteria is not a method in this library. I looked at the original image you posted, and it is completely different from what you are now posting.

If you can create a minimal reproducible example, I'd be happy to take a look at it.

Here's a simple class I coded to try and reproduce your issue. I removed the extra closing parenthesis in the build call, but everything else seems to be ok. Maybe you could modify this class to show whatever issue you are having.

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;

import static org.mybatis.dynamic.sql.SqlBuilder.equalTo;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

public class GH847 {

    // class to demonstrate https://github.com/mybatis/mybatis-dynamic-sql/issues/847

    private static class DAO {
        private void selectByCriteria(SelectModel selectModel) {}
    }

    private static class Object1 extends SqlTable {
        public Object1() {
            super("Object1");
        }

        public SqlColumn<String> ID = column("id");
        public SqlColumn<Long> NAME = column("name");
    }

    private static class Object2 extends SqlTable {
        public Object2() {
            super("Object2");
        }

        public SqlColumn<String> MASTERID = column("id");
    }

    private final Object1 OBJECT_1 = new Object1();
    private final Object2 OBJECT_2 = new Object2();
    private final DAO dao = new DAO();

    private void gh847() {
        QueryExpressionDSL<SelectModel> select = select(OBJECT_1.ID, OBJECT_1.NAME).from(OBJECT_1);
        select.leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID));
        select.where(OBJECT_1.ID, isEqualTo("11"));
        dao.selectByCriteria(select.build());

        dao.selectByCriteria(select(OBJECT_1.ID, OBJECT_1.NAME)
                .from(OBJECT_1)
                .leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID))
                .where(OBJECT_1.ID, isEqualTo("id"))
                .build());
    }
}
hermer commented

Here's a simple class I coded to try and reproduce your issue. I removed the extra closing parenthesis in the build call, but everything else seems to be ok. Maybe you could modify this class to show whatever issue you are having.

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;

import static org.mybatis.dynamic.sql.SqlBuilder.equalTo;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

public class GH847 {

    // class to demonstrate https://github.com/mybatis/mybatis-dynamic-sql/issues/847

    private static class DAO {
        private void selectByCriteria(SelectModel selectModel) {}
    }

    private static class Object1 extends SqlTable {
        public Object1() {
            super("Object1");
        }

        public SqlColumn<String> ID = column("id");
        public SqlColumn<Long> NAME = column("name");
    }

    private static class Object2 extends SqlTable {
        public Object2() {
            super("Object2");
        }

        public SqlColumn<String> MASTERID = column("id");
    }

    private final Object1 OBJECT_1 = new Object1();
    private final Object2 OBJECT_2 = new Object2();
    private final DAO dao = new DAO();

    private void gh847() {
        QueryExpressionDSL<SelectModel> select = select(OBJECT_1.ID, OBJECT_1.NAME).from(OBJECT_1);
        select.leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID));
        select.where(OBJECT_1.ID, isEqualTo("11"));
        dao.selectByCriteria(select.build());

        dao.selectByCriteria(select(OBJECT_1.ID, OBJECT_1.NAME)
                .from(OBJECT_1)
                .leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID))
                .where(OBJECT_1.ID, isEqualTo("id"))
                .build());
    }
}

Thanks for reply!
selectByCriteria is a self-encapsulated method. The problem is building the SelectModel

         dao.selectByCriteria(select(OBJECT_1.ID, OBJECT_1.NAME)
                .from(OBJECT_1)
                .leftJoin(OBJECT_2).on(OBJECT_1.ID, equalTo(OBJECT_2.MASTERID))
                .where(OBJECT_1.ID, isEqualTo("id"))
                .build());  -- No build method. Compiler error

I'll write it this way for now, and it will compile normally

         dao.selectByCriteria((SelectModel) SelectDSL.select(OBJECT_1.ID, OBJECT_2.PRICE, OBJECT_2.MASTERID)
                .from(OBJECT_1)
                .leftJoin(OBJECT_2, new JoinCriterion.Builder().withConnector("on").withJoinColumn(OBJECT_1.ID).withJoinCondition(equalTo(OBJECT_2.MASTERID)).build())
                .where(OBJECT_1.ID, isEqualTo("id"))
                .build());