通过springMVC+spring+jdbcTemplate来搭建一个web项目,完成员工列表
web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--配置前端总调度器--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--绑定spring核心配置文件--><init-param><param-name>contextConfigLocation</param-name><!--配置文件的路径--><param-value>classpath:xml/springmvc.xml</param-value></init-param><!--设置启动级别,需要在Tomcat服务器启动的时候就加载核心容器对象--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><!--拦截所有客户端请求--><url-pattern>/</url-pattern></servlet-mapping></web-app>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--开启扫描包--><context:component-scan base-package="com.jy"></context:component-scan><!--过滤静态资源--><mvc:default-servlet-handler></mvc:default-servlet-handler><!--支持mvc注解驱动--><mvc:annotation-driven></mvc:annotation-driven><!--配置视图解析器--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--设置前缀--><property name="prefix" value="/WEB-INF/jsp/"></property><!--设置后缀--><property name="suffix" value=".jsp"></property></bean><!--配置持久层实例--><bean id="dao" class="com.jy.dao.impl.EmpDapImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><!--注册jdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><!--注入数据源实例--><property name="dataSource" ref="dataSource"></property></bean><!--注册数据源实例--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!--注入源数据--><property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=GMT&characterEncoding=utf-8"></property><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean></beans>
持久层(略去接口,仅展示实现代码):
/*** @author shizi 2022/2/8*/public class EmpDapImpl implements EmpDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic List<Emp> getEmpList() {return jdbcTemplate.query("select * from tb_emp", new RowMapper<Emp>() {@Overridepublic Emp mapRow(ResultSet resultSet, int i) throws SQLException {Emp emp = new Emp();emp.setEmpName(resultSet.getString("emp_name"));emp.setAge(resultSet.getInt("age"));emp.setEid(resultSet.getInt("eid"));emp.setSex(resultSet.getInt("sex"));emp.setSalary(resultSet.getDouble("salary"));return emp;}});}}
业务层:
/*** @author shizi 2022/2/8*/@Servicepublic class EmpServiceImpl implements EmpService {@Autowiredprivate EmpDao empDao;@Overridepublic List<Emp> getEmpList() {return empDao.getEmpList();}}
表现层:
/*** @author shizi 2022/2/8*/@Controller@RequestMapping("/emp")public class EmpController {@Autowiredprivate EmpService empService;/*** 方式一* @param model* @return*//*@RequestMapping("list")public String getEmpList(Model model){//访问持久层查询数据List<Emp> empList = empService.getEmpList();//将数据存到Model中model.addAttribute("empList",empList);return "list";}*//*** 方式二* @param model* @return*/@RequestMapping("list")public ModelAndView getEmpList(Model model){ModelAndView modelAndView = new ModelAndView();//访问持久层查询数据List<Emp> empList = empService.getEmpList();//将数据存到Model中modelAndView.addObject(empList);//设置视图名称modelAndView.setViewName("list");return modelAndView;}}
前端jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%--引入jstl标签库--%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head><title>员工列表</title></head><body><h1 align="center" style="font-size: 30px">员工列表</h1><table align="center" border="1px"><tr><th>员工姓名</th><th>年龄</th><th>性别</th><th>薪水</th></tr><c:forEach items="${empList}" var="emp"><tr><td>${emp.empName}</td><td>${emp.age}</td><td>${emp.sex == 1 ? '男' : '女'}</td><td>${emp.salary}</td></tr></c:forEach></table></body></html>
