jsqlParser例子(二)join

一、获取连接的表名及字段

public void testParseJoin(){
        String sql = "select *from A as a left join B on a.bid = B.id left join C on A.cid = C.id left join D on B.did = D.id";
        try {
            Select select = (Select)CCJSqlParserUtil.parse(sql);
            SelectBody selectBody = select.getSelectBody();
            PlainSelect plainSelect = (PlainSelect)selectBody;
            List joins = plainSelect.getJoins();
            for(Join join : joins){
                EqualsTo equalsTo = (EqualsTo)join.getOnExpression();
                Column leftExpression = (Column)equalsTo.getLeftExpression();
                Column rightExpression = (Column)equalsTo.getRightExpression();

                System.out.println("left table name:"+leftExpression.getTable());
                System.out.println("left table field:"+leftExpression.getColumnName());
                System.out.println("right table name:"+rightExpression.getTable());
                System.out.println("right table field:"+rightExpression.getColumnName());
                System.out.println("---------------------");
            }

        } catch (JSQLParserException e) {
            e.printStackTrace();
        }

    }

输出结果为:

left table name:a
left table field:bid
right table name:B
right table field:id
---------------------
left table name:A
left table field:cid
right table name:C
right table field:id
---------------------
left table name:B
left table field:did
right table name:D
right table field:id
---------------------

你可能感兴趣的:(jsqlParser例子(二)join)