HaishenDBの最初の例
4739 ワード
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
完全なサービスコードは以下の通りです.
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 IntegerCol("Age");
}