我有 2 张 table Pekawai 表和 Absensi 表 我有问题..当我通过 nip 选择它时,无法显示 pekawai 表中的 id
这是我的 table 图片
我的查看代码:
<input type="text" name="nip" id="nip">
我的实现代码:
@Override
public ResultSet getByNip(String nip) {
ResultSet rs = (ResultSet) em.createQuery("select a.id from Pegawai a WHERE a.nip='"+nip+"'");
return rs;
}
我的 Controller 代码:
@RequestMapping(value = "ProsesAbsensi.htm", method = RequestMethod.POST)
public String prosesAbsensi(ModelMap modelMap, HttpServletResponse response,
@RequestMapping(value = "ProsesAbsensi.htm", method = RequestMethod.POST)
public String prosesAbsensi(ModelMap modelMap, HttpServletResponse response,
@ModelAttribute("Absensi") Absensi a,
@RequestParam(value = "nip", required = false) String nip
) throws SQLException, IOException {
PegawaiImplement pi = new PegawaiImplement();
ResultSet rs = pi.getByNip(nip);
AbsensiImplement ai = new AbsensiImplement();
ai.insert(a);
return "Depan";
}
如何将 id 从 pekawai 表获取到我的 Controller 中?
最佳答案
我强烈建议改进您的设计。据我所知,您正在使用 Spring 和 JPA。在这种情况下,我建议看一下 Spring Data JPA:http://projects.spring.io/spring-data-jpa/
这样,您最终会在服务层中得到以下代码
public Pegawai getByNip(String nip) {
return pegawaiRepository.findByNip(nip);
}
在你的 Controller 中你只需编写
service.getByNip(nip).getId()
为了获得ID。
如果您无法执行此操作(无论出于何种原因),您将使用实体管理器来创建查询(无结果集)。您可以在此处获取示例:http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html 。对于您的情况:
em.createQuery("select a.id from Pegawai a WHERE a.nip='"+nip+"'").getSingleResult();
关于java - java spring mvc 如何显示不按 id 的另一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42756117/