springMVC +MyBatis

来源:互联网 发布:淘宝网络客服招聘 编辑:程序博客网 时间:2024/05/02 03:06

步骤一:配置web.xml

在web.xml中配置如下内容:

配置后,web.xml代码如下:    

  1. <!-- Spring前端控制器-->
  2.     <servlet>
  3.         <servlet-name>SpringMVC</servlet-name>
  4.         <servlet-class>
  5.             org.springframework.web.servlet.DispatcherServlet
  6.         </servlet-class>
  7.         <init-param>
  8.             <param-name>
  9.                 contextConfigLocation
  10.             </param-name>
  11.             <param-value>
  12.                 classpath:applicationContext.xml
  13.             </param-value>
  14.         </init-param>
  15.     </servlet>
  16.     <servlet-mapping>
  17.         <servlet-name>SpringMVC</servlet-name>
  18.         <url-pattern>*.do</url-pattern>
  19.     </servlet-mapping>
  20.     <!-- 使用Filter解决中文乱码问题-->
  21.     <filter>
  22.      <filter-name>encodingFilter</filter-name>
  23.      <filter-class>
  24.          org.springframework.web.filter.CharacterEncodingFilter
  25.      </filter-class>
  26.      <init-param>
  27.      <param-name>encoding</param-name>
  28.      <param-value>UTF-8</param-value>
  29.      </init-param>
  30.     </filter>
  31.     <filter-mapping>
  32.      <filter-name>encodingFilter</filter-name>
  33.      <url-pattern>*.do</url-pattern>
  34.     </filter-mapping>
  35. 步骤二:配置applicationContext.xml
  36. 在applicationContext.xml中追加如下配置:

    配置后,applicationContext.xml代码如下:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns:context="http://www.springframework.org/schema/context"
    5.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    6.     xmlns:jee="http://www.springframework.org/schema/jee"
    7.     xmlns:tx="http://www.springframework.org/schema/tx"
    8.     xmlns:aop="http://www.springframework.org/schema/aop"
    9.     xmlns:mvc="http://www.springframework.org/schema/mvc"
    10.     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    11.     xsi:schemaLocation="
    12.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    14.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    15.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    16.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    17.         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    18.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    19.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    20.     <!-- 配置数据源 -->
    21.     <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
    22.         <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    23.         <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    24.         <property name="username" value="lhh" />
    25.         <property name="password" value="123456" />
    26.     </bean>
    27.     
    28.     <!-- 配置SqlSessionFactory -->
    29.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    30.         <property name="dataSource" ref="ds" />
    31.         <property name="mapperLocations" value="classpath:com/test/entity/*.xml" />
    32.     </bean>
    33.     
    34.     <!-- 配置MyBatis注解 -->
    35.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    36.         <property name="basePackage" value="com.test.dao" />
    37.         <property name="annotationClass" value="com.test.annotation.MyBatisRepository" />
    38.     </bean>
    39.     
    40.     <!-- 开启注解扫描 -->
    41.     <context:component-scan base-package="com.test" />
    42.     
    43.     <!-- 开启RequestMapping注解 -->
    44.     <mvc:annotation-driven />
    45.     
    46.     <!-- 处理请求转发 -->
    47.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    48.         <property name="prefix" value="/WEB-INF/"/>
    49.         <property name="suffix" value=".jsp"/>
    50.     </bean>    
    51. </beans>

    步骤三:创建业务控制器

    创建业务控制器EmpController,并增加查询方法实现查询业务,代码如下

    1. import java.util.List;
    2. import javax.annotation.Resource;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.ui.Model;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import com.tarena.dao.EmpDao;
    7. import com.tarena.entity.Emp;
    8. @Controller
    9. @RequestMapping("/emp")
    10. public class EmpController {
    11.     @Resource
    12.     private EmpDao empDao;
    13.     @RequestMapping("/findEmp.do")
    14.     public String find(Model model) {
    15.         List<Emp> list = empDao.findAll();
    16.         model.addAttribute("emps", list);
    17.         return "emp/emp_list";
    18.     }
    19. }

    步骤四:创建员工列表页面

    创建员工列表页面emp_list.jsp,将查询到的员工数据显示在表格中,代码如下:

    1. <%@page pageEncoding="utf-8"%>
    2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    3. <html>
    4. <head>
    5. </head>
    6. <body>
    7.     <table width="60%" border="1" cellpadding="2" cellspacing="0">
    8.         <tr>
    9.             <th>EMPNO</th>
    10.             <th>ENAME</th>
    11.             <th>JOB</th>
    12.             <th>MGR</th>
    13.             <th>HIREDATE</th>
    14.             <th>SAL</th>
    15.             <th>COMM</th>
    16.             <th>DEPTNO</th>
    17.         </tr>
    18.         <c:forEach items="${emps }" var="emp">
    19.             <tr>
    20.                 <td>${emp.empno }</td>
    21.                 <td>${emp.ename }</td>
    22.                 <td>${emp.job }</td>
    23.                 <td>${emp.mgr }</td>
    24.                 <td>${emp.hiredate }</td>
    25.                 <td>${emp.sal }</td>
    26.                 <td>${emp.comm }</td>
    27.                 <td>${emp.deptno }</td>
    28.             </tr>
    29.         </c:forEach>
    30.     </table>
    31. </body>
    32. </html>

    步骤五:测试

    打开浏览器,访问员工列表,效果如下图:



0 0