注入数据库对象和操作数据库代码:
~~~
package server;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
@Controller
public class Test {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@RequestMapping(value="/sql")
@ResponseBody
public String sql(){
String sql = "insert into temp1(title) values('测试')";
jdbcTemplate.update(sql);
return "ok";
}
@RequestMapping(value="/find")
@ResponseBody
public String findAll(){
String sql = "select * from temp1;";
java.util.List<Map<String, Object>> r = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : r) {
System.out.println(map.get("title"));
}
return "ok";
}
public String find(){
return "ok";
}
}
~~~