记录与baas的首次接触
今天在写后台接口代码的时候遇到了一些问题:
- 进行连表查询的时候,不知道查询语句写在哪里(公司封装的 BaseDao)功能太齐全了,以至于我的 Dao 层只继承了 BaseDao 接口 ,每个 Dao 都是空空如也。(还询问了下:这么多个表,可以只用一个公共的 Dao 吗?)
- 用 inner join 的话,他返回的并不是一个对象,而是你 select 出来的对象的集合,不知道该怎么下手了。
公司的代码:
对了,很多东西都封装过的,getSession 什么就别看了。
- DaoImpl 代码如下:
@Override
public Table getAuthorityGroupInfo(String groupName) {
Session session = null;
try {
String queryString = "SELECT t1.authorityId ,t1.firstName,t1.firstCode,t1.secondName,t1.secondCode,t1.thirdName,t1.thirdCode FROM ym_shop_sys_authority t1"
+ "LEFT JOIN ym_shop_sys_authority_group t2 ON (t2.authorityId = t1.authorityId AND t2.groupName ='manager')";
List<Object> sqlParams = new ArrayList<>();
sqlParams.add(groupName);
session = getSession();
java.sql.Connection conn = session.connection();
Table t = DataUtils.queryData(conn, queryString, sqlParams, null, null, null);
session.close();
// Transform.tableToJson(l);
return t;
} catch (RuntimeException re) {
re.printStackTrace();
return null;
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
}
- Service 层代码:
@Override
public Map<String, Object> getUserAuthorityInfo(String userId, String groupName) {
if (StringEmptyUtils.isEmpty(userId) || StringEmptyUtils.isEmpty(groupName)) {
return ReturnInfoUtils.errorInfo("请求参数不能为空!");
}
Table table = authorityDao.getAuthorityGroupInfo(userId, groupName);
if (table == null) {
return ReturnInfoUtils.errorInfo("查询失败,服务器繁忙!");
} else if (!table.getRows().isEmpty()) {
List<Row> lr = table.getRows();
return baleUserAuthorityInfo(lr);
} else {
//
Table table2 = authorityDao.getAuthorityGroupInfo(groupName);
if (table2 != null && !table2.getRows().isEmpty()) {
List<Row> lr = table2.getRows();
return baleUserAuthorityInfo(lr);
} else {
return ReturnInfoUtils.errorInfo("查询失败,服务器繁忙!");
}
}
}
下面三个类:
com.justep.baas.data.Table
com.justep.baas.data.DataUtils
com.justep.baas.data.Row
都是来自于 baas。
回答:
- 公司果然封装了一个公共 Dao 。。。。
- 用 baas 就行了,可以 根据行和列来查询查找到的表对象。但是其实自己写一个查询两次的查询比连表查询更好一点,这个返回的值类似于一个数组,还需要手动解析(列的组合更方便),但是两次查询反而可以返回其中一个对象。需要看实际需求了。