【发布时间】:2012-01-17 22:40:32
【问题描述】:
我想构建一个可以标记更多元素的选择。所以multiple="true"。我使用mysql和spring的道技术。我成功地从数据库中获取了 select 的值。但是现在我在将选定的值插入我的数据库时遇到了问题。
重要的表格是:
表demo.instrumente 填充有guitar、piano 等数据和id。这些值(即guitar、piano)显示在多选中。
用户可以选择 2 或 3 种乐器。所以我需要给学生添加以下乐器。我对表schueler_instrumente 执行此操作。每个student 和instrument 都有一个id。所以我需要创建这样的数据:
student_id 1 and instrument_id 2
student_id 1 and instrument_id 5
这是我的仪器模型类代码:
public class Instrumente {
private Integer instrumentid;
private String instrumentname;
//...getters and setters
}
此代码是我的控制器的一部分:
@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model) {
model.put("instrumentListe", schuelerManager.getinstrumente());
return "usercenter";
}
这是我的 schuelerManager 的相关部分
public Map<String, String> getinstrumente() {
Map<String,String> result = new LinkedHashMap<String,String>();
for (Instrumente instrument : instrumentDao.getInstrumente()) {
result.put(instrument.getInstrumentid().toString(),
instrument.getInstrumentname());
}
return result;
}
以下是我从数据库中获取数据的方式:
public List<Instrumente> getInstrumente() {
return getJdbcTemplate().query("SELECT * FROM instrumente",
new RowMapper<Instrumente>() {
public Instrumente mapRow(ResultSet rs,
int rowNum)
throws SQLException {
Instrumente instrument = new Instrumente();
instrument.setInstrumentid
(rs.getInt("Instrument_ID"));
instrument.setInstrumentname
(rs.getString("Instrumentenbezeichnung"));
return instrument;
}
});
}
我现在知道我需要做什么才能从选择列表中获取选定的值。我要写什么给jsp中的path="?"。
我认为我可以取回一个值列表,但是如何将此列表插入我的表 schueler_instrument。我是否需要一段时间或重复并每次插入?
我在互联网上找不到任何好的例子。我希望有人可以用一些代码 sn-ps 告诉我如何做到这一点。
【问题讨论】:
标签: java mysql spring dao multiple-select