日期:2014-05-16 浏览次数:20516 次
PersonTest.Table t = new PersonTest.Table(); ISelectCause sel1 = dao.newCause(); sel1.select(t.ALL_).from(t).where().eq(t.id,"a").or().gt(t.age, 5).orderby().asc(t.age);?
以上java语句将产生以下的sql语句。
?
SELECT t1.* FROM t_person AS t1 WHERE t1.Id = ? OR t1.Age > ? ORDER BY t1.Age ASC?
完整的service代码如下。
?
public class ITestImpl implements ITest { private IDAO dao; public void setDao(IDAO dao) { this.dao = dao; } @Override public List<Map> findLs() { PersonTest.Table t = new PersonTest.Table(); ISelectCause sel1 = dao.newCause(); sel1.select(t.ALL_).from(t).where().eq(t.id,"a").or().gt(t.age, 5).orderby().asc(t.age); return dao.findList(sel1); } @Override public List<PersonTest> findLsPersonTest(String pId,int pAge) { ICriteria criteria = dao.newCriteria(); criteria.eq(t.id,pId).or().gt(t.age, pAge).orderby().asc(t.age); return dao.findList(PersonTest.class,criteria); } }
?
PersonTest的代码。有点复杂,具体规则后续会讲。总之,这不是一个简单的java bean。
?
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.haishen.test.bean; import com.haishen.db.api.IPreparedStatementCallBack; import com.haishen.db.bean.AbstractBean; import com.haishen.db.bean.TableMetadata; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * * @author hicen */ public class PersonTest extends AbstractBean { private final static PersonTest.Table tableTmp = new PersonTest.Table(); public static class Table extends PersonTestTable { } public PersonTest() { super(tableTmp); } public void setId(final String id) { this.id = id; IPreparedStatementCallBack pscb = new IPreparedStatementCallBack() { @Override public void setValue(PreparedStatement psmt, int pintIndex) throws SQLException { psmt.setString(pintIndex, id); } }; this.put(tableTmp.id.getName(), pscb); } public void setName(final String name) { this.name = name; IPreparedStatementCallBack pscb = new IPreparedStatementCallBack() { @Override public void setValue(PreparedStatement psmt, int pintIndex) throws SQLException { psmt.setString(pintIndex, name); } }; this.put(tableTmp.name.getName(), pscb); } public void setAge(final int age) { this.age = age; IPreparedStatementCallBack pscb = new IPreparedStatementCallBack() { @Override public void setValue(PreparedStatement psmt, int pintIndex) throws SQLException { psmt.setInt(pintIndex, age); } }; this.put(tableTmp.age.getName(), pscb); } @Override public void iniFromDB(ResultSet rs) throws SQLException { this.id = rs.getString(tableTmp.id.getName()); this.name = rs.getString(tableTmp.name.getName()); this.age = rs.getInt(tableTmp.age.getName()); } @Override public String getIdName() { return tableTmp.id.getFullName(); } @Override public String getIdValue() { return this.id; } public String getId() { return id; } public String getName() { return name; } public int getAge() { return age; } private String id; private String name; private int age; } class PersonTestTable extends TableMetadata { public PersonTestTable() { super("t_person"); } public StringCol id = new StringCol("Id"); public StringCol name = new StringCol("Name"); public IntegerCol age = new Inte