用java代码实现一个下拉选择框,对下拉选择框里面内容进行赋值的接口,包含cotroller,service,以及xml的SQL语句的编写
时间: 2024-05-03 22:22:53 浏览: 59
Controller代码:
```
@RestController
@RequestMapping("/select")
public class SelectController {
@Autowired
private SelectService selectService;
@GetMapping("/options")
public List<Option> getOptions() {
return selectService.getOptions();
}
}
```
Service代码:
```
@Service
public class SelectServiceImpl implements SelectService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Option> getOptions() {
List<Option> options = new ArrayList<>();
String sql = "SELECT id, name FROM options";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : list) {
Option option = new Option();
option.setId((Integer) map.get("id"));
option.setName((String) map.get("name"));
options.add(option);
}
return options;
}
}
```
XML SQL语句:
```
<select id="getOptions" resultType="map">
SELECT id, name FROM options
</select>
```
阅读全文