use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestInsertSql method testInsertByCondition.
/**
* 带Where条件子句,将若干个POJO所有属性值插入数据库 <code>
* class Pet{
* private Integer id;
* private String name;
* private int age;
* //此处省略setter和getter方法
* }
* Pet pet = new Pet();
* pet.setName("小白");
* pet.setAge(4);
* insertByCondition("xxx", pet);
* </code> 会执行sql:INSERT INTO $table values('小黑','3') WHERE xxx ;
*
* POJO的类型
*
* @param condition
* Where条件
* @param ts
* 带有数据的POJO,可多个不同类型或同类型
* @return 如果插入成功,返回true,否则返回false
*/
@Test
public void testInsertByCondition() {
pet.setName("小黑");
pet.setAge(3);
pet.setType("dog");
pet.setMaster(null);
Sql sql = insert.create("xxx = 'ooo'")[0];
Assert.assertEquals("INSERT INTO t_pet(name,age,cate) VALUES(?,?,?) WHERE xxx = 'ooo' ;", sql.sql);
Assert.assertEquals("小黑", sql.args.get(0));
Assert.assertEquals(3, sql.args.get(1));
Assert.assertEquals("dog", sql.args.get(2));
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestUpdateSql method testUpdateByField.
/**
* 修改给定字段
*
* @param t
* 给定的对象
* @param fields
* 给定的字段
* @return 返回布尔
*/
@Test
public <T> void testUpdateByField() {
pet.setName("xiaohuang");
pet.setAge(3);
pet.setPetId(12L);
Master master = new Master();
master.setId(5L);
pet.setMaster(master);
String[] fields = { "name", "age", "master" };
Sql sql = update.update(fields)[0];
Assert.assertEquals("UPDATE t_pet SET name = ? , age = ? , master_id = ? WHERE id = ? ;", sql.sql);
Assert.assertEquals("xiaohuang", sql.args.get(0));
Assert.assertEquals(3, sql.args.get(1));
Assert.assertEquals(5l, sql.args.get(2));
Assert.assertEquals(12l, sql.args.get(3));
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class DeleteDAOImpl method batchDelete.
public <T> Number[] batchDelete(T... ts) throws DAOException {
Number[] ids = null;
Connection con = null;
if (ts == null || ts.length == 0)
return ids;
ids = new Number[ts.length];
try {
con = ds.getConnection();
Sql[] sqls = SqlFactory.getDeleteSql(ts).delete();
List<Object[]> argList = new ArrayList<Object[]>(ts.length);
for (Sql sql : sqls) {
argList.add(sql.args.toArray());
}
Object[][] args = new Object[argList.size()][];
for (int i = 0; i < argList.size(); i++) {
args[i] = argList.get(i);
}
ids = JdbcUtil.batchUpdateWithArgs(con, sqls[0].sql, args);
} catch (Exception e) {
throw new DAOException("", e);
}
return ids;
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class DeleteDAOImpl method deleteByFieldIsValue.
public <T> Number deleteByFieldIsValue(Class<T> clazz, String[] fields, String[] values) throws DAOException {
Number id = 0;
if (clazz != null && fields != null && values != null && fields.length == values.length) {
Connection con = null;
try {
con = ds.getConnection();
Sql[] sqls = SqlFactory.getDeleteSql(new Object[] { clazz.newInstance() }).delete(fields, values);
id = JdbcUtil.updateWithArgs(con, sqls[0].sql, sqls[0].args.toArray());
} catch (Exception e) {
throw new DAOException("", e);
}
}
return id;
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class UpdateDAOImpl method updateByFieldIsValue.
public <T> Number[] updateByFieldIsValue(T[] ts, String[] fields, String[] values) throws DAOException {
Number[] ids = null;
if (ts != null && ts.length > 0 && fields != null && fields.length > 0) {
Connection con = null;
ids = new Number[ts.length];
try {
Sql[] sqls = SqlFactory.getUpdateSql(ts).update(fields, values);
for (int i = 0; i < ts.length; i++) {
con = ds.getConnection();
ids[i] = JdbcUtil.updateWithArgs(con, sqls[i].sql, sqls[i].args.toArray());
// 更新缓存
}
} catch (Exception e) {
throw new DAOException("updateByFieldIsValue exception ", e);
}
}
return ids;
}
Aggregations