MapHandler&MapListHandler&ScalarHandler结果集处理
上一篇文章:Javaweb JDBC数据库连接 第六讲 :Apache DBUtils & BeanHandler和BeanListHandler结果集处理器
写法:
MapHandler(将数据放入Map中以Key value形式存放)
public Map<String,Object> findByIdWithMap(int id){
String sql = "select * from user where id=?";
Map<String,Object> map = null;
try {
map = queryRunner.query(sql,new MapHandler(),id);
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
MapListHandler(将数据放入Map中以Key value形式存放,再将Map 以list的形式存放)
public List<Map<String,Object>> listWithMap(){
String sql = "select * from user";
List<Map<String,Object>> list = null;
try {
list = queryRunner.query(sql,new MapListHandler());
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
ScalarHandler(单个数据的存放)
public String countUser(int id){
String sql="select username from user where id = ?";
String count=null;
try {
count=(String) queryRunner.query(sql,new ScalarHandler<>(),id);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
项目————实际操作————分别使用两种方式输出数据库数据
- UserDao:
public class UserDao {
private QueryRunner queryRunner=new QueryRunner(DataSourceUtil.getDataSource());
public Map<String,Object> findByIdWithMap(int id){
String sql="select * from user where id = ?";
Map<String,Object> map=null;
try {
map=queryRunner.query(sql,new MapHandler(),id);
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
public List<Map<String,Object>> listWithMap(){
String sql="select * from user ";
List<Map<String,Object>> list=null;
try {
list=queryRunner.query(sql,new MapListHandler());
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public String countUser(int id){
String sql="select username from user where id = ?";
String count=null;
try {
count=(String) queryRunner.query(sql,new ScalarHandler<>(),id);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
}
- UserService
public interface UserService {
Map<String,Object> findByIdWithMap(int id);
List<Map<String,Object>> listWithMap();
String countUser(int id);
}
- UserServiceimpl
public class UserServiceimpl implements UserService {
private UserDao userDao=new UserDao();
@Override
public Map<String, Object> findByIdWithMap(int id) {
return userDao.findByIdWithMap(id);
}
@Override
public List<Map<String, Object>> listWithMap() {
return userDao.listWithMap();
}
@Override
public String countUser(int id) {
return userDao.countUser(id);
}
}
UserServlet
@WebServlet(name= "userServlet",urlPatterns = "/user")
public class UserServlet extends HttpServlet {
private UserService userService=new UserServiceimpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if (method.equals("findByIdWithMap")){
String idStr=req.getParameter("id");
int id=Integer.parseInt(idStr);
Map<String,Object> map=userService.findByIdWithMap(id);
System.out.println(map.toString());
}
if (method.equals("listWithMap")){
List<Map<String,Object>> list=userService.listWithMap();
System.out.println(list.toString());
}
if (method.equals("countUser")){
String idStr=req.getParameter("id");
int id=Integer.parseInt(idStr);
System.out.println(userService.countUser(id));
}
}
}
结果截图:
MapHandler
数据的结构形式:
{id=3, phone=2323432, pwd=xdclass-lw, sex=1, img=wwwww, create_time=2020-05-20, role=1, username=二当家小D, wechat=xdclass1},
MapListHandler
数据结构形式:
[{id=1, phone=123, pwd=666, sex=1, img=xdclass.net, create_time=2021-09-09, role=1, username=jack, wechat=xdclass6},
{id=3, phone=2323432, pwd=xdclass-lw, sex=1, img=wwwww, create_time=2020-05-20, role=1, username=二当家小D, wechat=xdclass1},
{id=4, phone=2323432, pwd=3232323, sex=1, img=wwwww, create_time=2020-05-20, role=1, username=老王, wechat=xdclass-lw},
{id=5, phone=null, pwd=123456, sex=1, img=null, create_time=2022-02-16, role=null, username=爱卿呀, wechat=null},
{id=6, phone=null, pwd=123456, sex=null, img=null, create_time=null, role=null, username=哼哼, wechat=null},
{id=7, phone=null, pwd=123456, sex=null, img=null, create_time=null, role=null, username=哈哈, wechat=null}]
ScalarHandler
数据结构形式:
爱卿呀
数据库: