第三节,数据库查询

## 步骤 #### 1. 创建类 DataBaseController 创建 com.xnx3.wangmarket.learn.controller.DataBaseController 类,其内代码为: ```` package com.xnx3.wangmarket.learn.controller; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.xnx3.j2ee.entity.User; import com.xnx3.j2ee.service.SqlService; import com.xnx3.wangmarket.admin.controller.BaseController; /** * 网市场云建站系统 二次开发入门之第三节 数据库查询 * @author 管雷鸣 */ @Controller @RequestMapping("/") public class DataBaseController extends BaseController { /** * 数据库相关操作 */ @Resource private SqlService sqlService; /** * 查询user用户表的信息 */ @RequestMapping("/dbSelect${url.suffix}") @ResponseBody public String dbSelect(HttpServletRequest request){ List userList = sqlService.findBySqlQuery("SELECT * FROM user", User.class); for (int i = 0; i < userList.size(); i++) { User user = userList.get(i); System.out.println(user.getId() + "-" + user.getUsername()); } return "success"; } } ```` ##### 注意: 1. 新增加了 SqlService ,可以用此来直接操作数据库 2. User 对象(com.xnx3.j2ee.entity.User)为已针对数据表 user 创建好的实体类。 #### 2. 访问测试 访问 http://localhost:8080/dbSelect.do ,然后回到 Eclipse 控制台 Console,可以看到打印出的信息: ![](http://cdn.weiunity.com/site/473/news/faa4d9927fed449a9a10a1f60b3cb440.png)
## SqlService 更多文档 更多数据库相关方法说明,请参考:[http://www.xnx3.com/doc/wangmarket/com/xnx3/j2ee/service/SqlService.html](http://www.xnx3.com/doc/wangmarket/com/xnx3/j2ee/service/SqlService.html)