Springmvc+Spring+maven+c3p0+jsp查询数据库中所有数据并显示
上篇Springmvc+Spring+maven+c3p0不加入jsp的链接
(https://blog.youkuaiyun.com/duduyingya/article/details/114992131)
1 新的项目结构
2 向pom.xml加入新需要的jar包
<!--针对jsp页面 删除掉之前加入的servlet-api包-->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<!-- 去除依赖 -->
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 导入servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
2 加入一个自定义的RowMapper类
用于自定义jdbcTemplate的query方法返回的类型,制作成返回StudentBean的集合的类
package com.utilStu;
import com.db.StudentBean;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 自定义使用query时返回的实体类 类型
*/
public class StudentRowMapper implements RowMapper<StudentBean> {
@Override
public StudentBean mapRow(ResultSet resultSet, int i) throws SQLException {
StudentBean studentBean=new StudentBean();
studentBean.setId(resultSet.getInt("id"));
studentBean.setSname(resultSet.getString("sname"));
studentBean.setDiscri(resultSet.getString("discri"));
studentBean.setPrice(resultSet.getDouble("price"));
return studentBean;
}
}
3 修改控制类和接口
package com.controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface StudentCon {
ModelAndView cc(
HttpServletRequest request, HttpServletResponse response);
}
----------------------------------------------------------------------------------------------
package com.controller;
import com.db.StudentBean;
import com.server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/user")
public class StudentConImp implements StudentCon{
/*
注入业务层
*/
@Autowired
public StudentServer studentServerImp;
/**
* 采用get方式访问参数
* @return
* 参数 @RequestParam("userId") Integer stuId
*/
@RequestMapping(value = "/hello",method =RequestMethod.GET)
@Override
public ModelAndView cc(
HttpServletRequest request, HttpServletResponse response)
{
List<StudentBean> stus =
studentServerImp.selectAllSer();
ModelAndView mav=new ModelAndView();
mav.setViewName("success");
mav.addObject("stus",stus);
return mav;
}
}
4 将持久层 业务层的查找所有的方法
返回值统一修改成 实体类集合
package com.dao;
import com.db.StudentBean;
import java.util.List;
/**
* 数据持久层
*/
public interface StudentDao {
/*
查询所有
*/
List<StudentBean> selectAllDao();
}
---------------------------
package com.dao;
import com.db.StudentBean;
import com.utilStu.StudentRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class StudentDaoImp implements StudentDao {
//注入template模板 数据库操作语句模板
@Autowired
public JdbcTemplate jdbcTemplate;
//单纯jdbc
@Override
public List<StudentBean> selectAllDao() {
String sql="select * from stu";
//返回实体类 集合
List<StudentBean> stus =
jdbcTemplate.query(sql,new StudentRowMapper());
return stus;
}
}
-----------------------
//业务层
package com.server;
import com.db.StudentBean;
import java.util.List;
/**
*业务层接口类
*/
public interface StudentServer {
//查询所有
List<StudentBean> selectAllSer();
}
-----------------------
package com.server;
import com.dao.StudentDao;
import com.db.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 业务实现类
*/
@Service
public class StudentServerImp implements StudentServer{
/*
注入数据库层接口
*/
@Autowired
public StudentDao studentDaoImp;
/*
注入同学类
*/
@Autowired
public StudentBean stu;
/*
得到所有同学
*/
@Override
public List<StudentBean> selectAllSer() {
return studentDaoImp.selectAllDao();
}
}
5 修改jsp页面
1 index.jsp页面
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<html>
<body>
<h2>Hello World!</h2>
<!--对应con层的类映射路径和方法映射路径 前面不要加斜杠-->
<form action="${pageContext.request.contextPath}/user/hello" method="get">
<input type="submit" value="login">
</form>
</body>
</html>
2 success页面
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<!-- 引用jstl-->
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %><html>
<head>
<title>Title</title>
</head>
<body>
<tr>
<td>
id
</td>
<td>
sname
</td>
<td>
discri
</td>
<td>
price
</td>
</tr>
<tr>
<c:forEach items="${stus}" var="stulist">
<tr>
<td>
${stulist.id}
</td>
<td>
${stulist.sname}
</td>
<td>
${stulist.discri}
</td>
<td>
${stulist.price}
</td>
</tr>
</c:forEach>
</tr>
</body>
</html>
亲测通过的哈~
链接:
https://blog.youkuaiyun.com/ajax_yan/article/details/81452276
https://blog.youkuaiyun.com/zy103118/article/details/109492592