?
LikeSql是我写一个开源的小框架(http://code.google.com/p/likesql/),目标是提供一个更优雅地处理SQL的一个方案,希望能够替代PreparedStatement和直接拼接SQL。虽然现在还有没有正式的release版本,我还在为此努力。
?
设计宗旨:
?
一些代码示例:
?
public void testAtStyleExpression(){ LikeSql sql=DML.select("id,name") .from("user") .where(AtExp.at("(name =@name or id=@id) and gender=@gender") .set("id", 1) .set("name", "John") .set("gender", "M")); String eSql="SELECT id,name FROM user WHERE (name ='John' or id=1) and gender='M'"; String eTpl="SELECT id,name FROM user WHERE (name =? or id=?) and gender=?"; assertEquals(eSql,sql.toString()); assertEquals(eTpl,sql.toTemplate()); }
public void testInExpression(){ LikeSql sSql=DML.select("id,name") .from("user") .where(QuestExp.qt("id in (?)").set(1,ArrayExp.array(new int[]{1,3,6}))); String eSql="SELECT id,name FROM user WHERE id in (1,3,6)"; String eTpl="SELECT id,name FROM user WHERE id in (?,?,?)"; assertEquals(eSql,sSql.toString()); assertEquals(eTpl,sSql.toTemplate()); }
public void testSimpleSql(){ String eSql="INSERT INTO users (no,id,name,gender,height) VALUES(1,15666662656565,'johny',null,195.36)"; String eTpl="INSERT INTO users (no,id,name,gender,height) VALUES(?,?,?,?,?)"; LikeSql iSql=DML.insertInto("users") .value("no", 1) .value("id", new BigInteger("15666662656565")) .value("name","johny") .value("gender",null) .value("height", 195.36); assertEquals(eSql,iSql.toString()); assertEquals(eTpl,iSql.toTemplate()); }
?
LikeSql sql=DML.selectAll()
.from("user")
.where(QuestExp.qt("id=? AND gender=?")
.set(1, 1)
.set(2, 'M'));
PreparedStatement stmt=sql.toPreStatement(con);
System.out.println("Template:"+sql.toTemplate());
System.out.println("SQL:"+sql.toString());
ResultSet rs=stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("name"));
}
?