JSqlParse(二)带where的查询语句build

JSqlParse(二)带where的查询语句build

资料较少,翻看源码,查看类的继承关系倒推出来的…费劲啊

单个where条件

		Table user = new Table("user");
		 Select select = SelectUtils.buildSelectFromTable(user);
		 EqualsTo equalsTo = new EqualsTo();
		 equalsTo.setLeftExpression(new Column("user_age"));
		 equalsTo.setRightExpression(new LongValue(22));
		
		 SelectBody selectBody = new PlainSelect();
		 ((PlainSelect) selectBody).setWhere(equalsTo);
		 select.setSelectBody(selectBody);
		 List selectItemList = new ArrayList<>();
		 AllTableColumns allTableColumns = new AllTableColumns(user);
		 selectItemList.add(allTableColumns);
		 ((PlainSelect) selectBody).setSelectItems(selectItemList);
		 // SELECT user.* WHERE user_age = 22
		 System.out.println(select);

and or 连接的where条件

		Table user = new Table("user");
		 Select select = SelectUtils.buildSelectFromTable(user);
		 EqualsTo equalsTo = new EqualsTo();
		 equalsTo.setLeftExpression(new Column("user_age"));
		 equalsTo.setRightExpression(new LongValue(22));
		
		 GreaterThan greaterThan = new GreaterThan();
		 greaterThan.setLeftExpression(new Column("user_id"));
		 greaterThan.setRightExpression(new LongValue(10000));
		
		 AndExpression andExpression = new AndExpression(equalsTo,greaterThan);
		
		 SelectBody selectBody = new PlainSelect();
		 ((PlainSelect) selectBody).setWhere(andExpression);
		 select.setSelectBody(selectBody);
		 List selectItemList = new ArrayList<>();
		 AllTableColumns allTableColumns = new AllTableColumns(user);
		 selectItemList.add(allTableColumns);
		 ((PlainSelect) selectBody).setSelectItems(selectItemList);
		 // SELECT user.* WHERE user_age = 22 AND user_id > 10000
		 System.out.println(select);

你可能感兴趣的:(JSqlParse,Java,DataBase)