我有以下界面:
public interface Mapper {
public SomeType methodOne(HashMap<String, Object>);
public OtherType methodTwo(HashMap<String, Object>);
...
}
那么是否可以在另一个类中调用其中一个methodOne
或作为参数?methodTwo
public class Other {
public void doSomething(HashMap<String, Object> params, ????) {
Mapper mapper = new ConcreteMapper();
mapper.methodOne(params);
}
}
HashMap<String, Object>
我尝试了 java 反射,但在编译时无法获得类型。我想知道有没有办法解决这个问题?
动力来自我在工作场所遇到的一个实际问题。例如,每个REST
方法调用的唯一不同之处是getAddressPhone
and AddressPhoneType
。像打开 SQL 会话、实例化这些嘈杂的步骤Mapper
本质上是相同的。
public Response getAddressPhone(@PathParam("acc_nbr") String accNbr, @HeaderParam("AuthToken") String authToken) {
SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
Logger log = LoggerFactory.getLoggerFactory();
AddressPhoneType addressPhone = null;
try {
MyAccountMapper mapper = session.getMapper(MyAccountMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("acc_nbr", accNbr);
addressPhone = mapper.getAddressPhone(map);
if (addressPhone == null) {
return Response.ok(null).status(Status.NOT_FOUND).build();
}
} catch (Exception e) {
log.debug("getAddressPhone(map):" + e.getMessage());
return Response.ok().status(Status.BAD_REQUEST).build();
} finally {
session.close();
}
return Response.ok(gson.toJson(addressPhone)).header("AuthToken", authToken).status(Status.OK).build();
}
编辑
我正在使用MyBatis映射器来执行 MySql 查询,所以我有以下名为MyAccountMapper
. 由于这些方法中的每一个都与 SQL 查询中的 id 进行映射,因此它们没有定义:
public interface MyAccountMapper {
AddressPhoneType getAddressPhone(HashMap<String, Object> map);
AdminUserInfoType getAdminUserInfo(HashMap<String, Object> map);
DueDateInfoType getDueDateInfo(HashMap<String, Object> map);
....
}
现在,在我的所有 Web 服务调用中(我正在使用 JAX-RS),我需要调用这些方法来对我的数据库做一些事情,正如你从上面的例子中看到的那样,大部分的东西,比如设置映射器、创建记录器是相同的。唯一不同的是方法调用和返回类型。
@GET
@Path("/admin/user/info/{acc_nbr}")
@Produces("application/json")
public Response getAdminUserInfo(@PathParam("acc_nbr") String accNbr) {
SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
AdminUserInfoType adminUserInfoType = null;
Logger log = LoggerFactory.getLoggerFactory();
try {
MyAccountMapper mapper = session.getMapper(MyAccountMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("acc_nbr", accNbr);
adminUserInfoType = mapper.getAdminUserInfo(map);
if (adminUserInfoType == null) {
return Response.ok(gson.toJson(null)).status(Status.NOT_FOUND).build();
}
} catch (Exception e) {
log.debug("getAdminUserInfo(map):" + e.getMessage());
return Response.ok("getAdminUserInfo(map):"+ MyBatisErrorMessage.SELECT_ERROR).status(Status.BAD_REQUEST).build();
} finally {
session.close();
}
return Response.ok(gson.toJson(adminUserInfoType)).status(Status.OK).build();
}
@GET
@Path(value = "/address/phone/{acc_nbr}")
@Produces(MediaType.APPLICATION_JSON)
public Response getAddressPhone(@PathParam("acc_nbr") String accNbr,
@HeaderParam("AuthToken") String authToken) {
SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
Logger log = LoggerFactory.getLoggerFactory();
AddressPhoneType addressPhone = null;
try {
MyAccountMapper mapper = session.getMapper(MyAccountMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("acc_nbr", accNbr);
addressPhone = mapper.getAddressPhone(map);
if (addressPhone == null) {
return Response.ok(null).status(Status.NOT_FOUND).build();
}
} catch (Exception e) {
log.debug("getAddressPhone(map):" + e.getMessage());
return Response.ok("getAddressPhone(map):"+ MyBatisErrorMessage.SELECT_ERROR).status(Status.BAD_REQUEST).build();
} finally {
session.close();
}
return Response.ok(gson.toJson(addressPhone)).header("AuthToken", authToken).status(Status.OK).build();
}