步骤一:配置web.xml
在web.xml中配置如下内容:
配置后,web.xml代码如下:
- <!-- Spring前端控制器-->
- <servlet>
- <servlet-name>SpringMVC</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>
- contextConfigLocation
- </param-name>
- <param-value>
- classpath:applicationContext.xml
- </param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>SpringMVC</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!-- 使用Filter解决中文乱码问题-->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>
- org.springframework.web.filter.CharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- 步骤二:配置applicationContext.xml
-
在applicationContext.xml中追加如下配置:
配置后,applicationContext.xml代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:jpa="http://www.springframework.org/schema/data/jpa"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
- <!-- 配置数据源 -->
- <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
- <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
- <property name="username" value="lhh" />
- <property name="password" value="123456" />
- </bean>
- <!-- 配置SqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="ds" />
- <property name="mapperLocations" value="classpath:com/test/entity/*.xml" />
- </bean>
- <!-- 配置MyBatis注解 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.test.dao" />
- <property name="annotationClass" value="com.test.annotation.MyBatisRepository" />
- </bean>
- <!-- 开启注解扫描 -->
- <context:component-scan base-package="com.test" />
- <!-- 开启RequestMapping注解 -->
- <mvc:annotation-driven />
- <!-- 处理请求转发 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </beans>
步骤三:创建业务控制器
创建业务控制器EmpController,并增加查询方法实现查询业务,代码如下
- import java.util.List;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.tarena.dao.EmpDao;
- import com.tarena.entity.Emp;
- @Controller
- @RequestMapping("/emp")
- public class EmpController {
- @Resource
- private EmpDao empDao;
- @RequestMapping("/findEmp.do")
- public String find(Model model) {
- List<Emp> list = empDao.findAll();
- model.addAttribute("emps", list);
- return "emp/emp_list";
- }
- }
步骤四:创建员工列表页面
创建员工列表页面emp_list.jsp,将查询到的员工数据显示在表格中,代码如下:
- <%@page pageEncoding="utf-8"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <html>
- <head>
- </head>
- <body>
- <table width="60%" border="1" cellpadding="2" cellspacing="0">
- <tr>
- <th>EMPNO</th>
- <th>ENAME</th>
- <th>JOB</th>
- <th>MGR</th>
- <th>HIREDATE</th>
- <th>SAL</th>
- <th>COMM</th>
- <th>DEPTNO</th>
- </tr>
- <c:forEach items="${emps }" var="emp">
- <tr>
- <td>${emp.empno }</td>
- <td>${emp.ename }</td>
- <td>${emp.job }</td>
- <td>${emp.mgr }</td>
- <td>${emp.hiredate }</td>
- <td>${emp.sal }</td>
- <td>${emp.comm }</td>
- <td>${emp.deptno }</td>
- </tr>
- </c:forEach>
- </table>
- </body>
- </html>
步骤五:测试
打开浏览器,访问员工列表,效果如下图: