通过springMVC+spring+jdbcTemplate来搭建一个web项目,完成员工列表
    web.xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <!--配置前端总调度器-->
    7. <servlet>
    8. <servlet-name>dispatcherServlet</servlet-name>
    9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    10. <!--绑定spring核心配置文件-->
    11. <init-param>
    12. <param-name>contextConfigLocation</param-name>
    13. <!--配置文件的路径-->
    14. <param-value>classpath:xml/springmvc.xml</param-value>
    15. </init-param>
    16. <!--设置启动级别,需要在Tomcat服务器启动的时候就加载核心容器对象-->
    17. <load-on-startup>1</load-on-startup>
    18. </servlet>
    19. <servlet-mapping>
    20. <servlet-name>dispatcherServlet</servlet-name>
    21. <!--拦截所有客户端请求-->
    22. <url-pattern>/</url-pattern>
    23. </servlet-mapping>
    24. </web-app>

    springmvc.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:mvc="http://www.springframework.org/schema/mvc"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xsi:schemaLocation="
    7. http://www.springframework.org/schema/beans
    8. http://www.springframework.org/schema/beans/spring-beans.xsd
    9. http://www.springframework.org/schema/mvc
    10. http://www.springframework.org/schema/mvc/spring-mvc.xsd
    11. http://www.springframework.org/schema/context
    12. http://www.springframework.org/schema/context/spring-context.xsd">
    13. <!--开启扫描包-->
    14. <context:component-scan base-package="com.jy"></context:component-scan>
    15. <!--过滤静态资源-->
    16. <mvc:default-servlet-handler></mvc:default-servlet-handler>
    17. <!--支持mvc注解驱动-->
    18. <mvc:annotation-driven></mvc:annotation-driven>
    19. <!--配置视图解析器-->
    20. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    21. <!--设置前缀-->
    22. <property name="prefix" value="/WEB-INF/jsp/"></property>
    23. <!--设置后缀-->
    24. <property name="suffix" value=".jsp"></property>
    25. </bean>
    26. <!--配置持久层实例-->
    27. <bean id="dao" class="com.jy.dao.impl.EmpDapImpl">
    28. <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    29. </bean>
    30. <!--注册jdbcTemplate-->
    31. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    32. <!--注入数据源实例-->
    33. <property name="dataSource" ref="dataSource"></property>
    34. </bean>
    35. <!--注册数据源实例-->
    36. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    37. <!--注入源数据-->
    38. <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=GMT&amp;characterEncoding=utf-8"></property>
    39. <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    40. <property name="username" value="root"></property>
    41. <property name="password" value="root"></property>
    42. </bean>
    43. </beans>

    持久层(略去接口,仅展示实现代码):

    1. /**
    2. * @author shizi 2022/2/8
    3. */
    4. public class EmpDapImpl implements EmpDao {
    5. @Autowired
    6. private JdbcTemplate jdbcTemplate;
    7. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    8. this.jdbcTemplate = jdbcTemplate;
    9. }
    10. @Override
    11. public List<Emp> getEmpList() {
    12. return jdbcTemplate.query("select * from tb_emp", new RowMapper<Emp>() {
    13. @Override
    14. public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
    15. Emp emp = new Emp();
    16. emp.setEmpName(resultSet.getString("emp_name"));
    17. emp.setAge(resultSet.getInt("age"));
    18. emp.setEid(resultSet.getInt("eid"));
    19. emp.setSex(resultSet.getInt("sex"));
    20. emp.setSalary(resultSet.getDouble("salary"));
    21. return emp;
    22. }
    23. });
    24. }
    25. }

    业务层:

    1. /**
    2. * @author shizi 2022/2/8
    3. */
    4. @Service
    5. public class EmpServiceImpl implements EmpService {
    6. @Autowired
    7. private EmpDao empDao;
    8. @Override
    9. public List<Emp> getEmpList() {
    10. return empDao.getEmpList();
    11. }
    12. }

    表现层:

    1. /**
    2. * @author shizi 2022/2/8
    3. */
    4. @Controller
    5. @RequestMapping("/emp")
    6. public class EmpController {
    7. @Autowired
    8. private EmpService empService;
    9. /**
    10. * 方式一
    11. * @param model
    12. * @return
    13. */
    14. /*@RequestMapping("list")
    15. public String getEmpList(Model model){
    16. //访问持久层查询数据
    17. List<Emp> empList = empService.getEmpList();
    18. //将数据存到Model中
    19. model.addAttribute("empList",empList);
    20. return "list";
    21. }*/
    22. /**
    23. * 方式二
    24. * @param model
    25. * @return
    26. */
    27. @RequestMapping("list")
    28. public ModelAndView getEmpList(Model model){
    29. ModelAndView modelAndView = new ModelAndView();
    30. //访问持久层查询数据
    31. List<Emp> empList = empService.getEmpList();
    32. //将数据存到Model中
    33. modelAndView.addObject(empList);
    34. //设置视图名称
    35. modelAndView.setViewName("list");
    36. return modelAndView;
    37. }
    38. }

    前端jsp:

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--引入jstl标签库--%>
    3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    4. <html>
    5. <head>
    6. <title>员工列表</title>
    7. </head>
    8. <body>
    9. <h1 align="center" style="font-size: 30px">员工列表</h1>
    10. <table align="center" border="1px">
    11. <tr>
    12. <th>员工姓名</th>
    13. <th>年龄</th>
    14. <th>性别</th>
    15. <th>薪水</th>
    16. </tr>
    17. <c:forEach items="${empList}" var="emp">
    18. <tr>
    19. <td>${emp.empName}</td>
    20. <td>${emp.age}</td>
    21. <td>${emp.sex == 1 ? '男' : '女'}</td>
    22. <td>${emp.salary}</td>
    23. </tr>
    24. </c:forEach>
    25. </table>
    26. </body>
    27. </html>