Top

JAVA SPRINGMYBATIS02UNIT01

  1. 使用Spring整合MyBatis,查询全部员工数据
  2. 使用Spring MVC,完成员工查询功能

1 使用Spring整合MyBatis,查询全部员工数据

1.1 问题

使用Spring整合MyBatis,实现对数据库的访问。

1.2 方案

采用MapperScannerConfigurer方式整合MyBatis。

1.3 步骤

步骤一:建表

本案例中采用的员工表,建表脚本如下:

--员工表
create table t_emp(
	empno number(4) primary key,
	ename varchar(20),
	job varchar(10),
	mgr number(4),
	hiredate date,
	sal number(9,2),
	comm number(9,2),
	deptno number(4)
);

--员工表sequence
create sequence emp_seq start with 100;

--预置的员工表数据
ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy mm dd hh24:mi:ss';
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (1,'SMITH','CLERK',3,'1980-5-12',800,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (2,'ALLEN','SALESMAN',3,'1981-6-3',1600,300,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (3,'WARD','SALESMAN',4,'1990-3-15',1250,500,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (4,'JONES','MANAGER',5,'1985-4-8',2975,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (5,'MARTIN','SALESMAN',7,'1986-3-8',1250,1400,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (6,'BLAKE','MANAGER',9,'1989-6-1',2850,null,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7,'CLARK','MANAGER',9,'1995-10-1',2450,null,10);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (8,'SCOTT','ANALYST',9,'1993-5-1',3000,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (9,'KING','PRESIDENT',null,'1988-8-8',5000,null,10);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (10,'TURNER','SALESMAN',5,'1983-2-1',1500,0,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (11,'ADAMS','CLERK',5,'1992-7-3',1100,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (12,'JAMES','CLERK',1,'1996-9-10',950,null,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (13,'FORD','ANALYST',1,'1993-1-1',3000,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (14,'MILLER','CLERK',3,'1983-10-9',1300,null,10);
commit;

步骤二:导包

创建WEB项目SpringUnit01,并导入如下开发包:

导入之后,项目的包结构如下图:

图-1

步骤三: 配置applicationContext.xml

在src下创建applicationContext.xml,并配置如下内容:

1)配置数据源

2)配置SqlSessionFactory

3)配置MyBatis注解(该注解需要自定义)

完成上述配置后,applicationContext.xml中代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /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/tarena/entity/*.xml" />
	</bean>
	
	<!-- 配置MyBatis注解 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.tarena.dao" />
		<property name="annotationClass" value="com.tarena.annotation.MyBatisRepository" />
	</bean>
	
</beans>

上述配置中使用的MyBatis注解com.tarena.annotation.MyBatisRepository需要自定义,该注解的代码如下:

package com.tarena.annotation;

/**
 * MyBatis使用该注解标识DAO
 */
public @interface MyBatisRepository {

}

步骤四:创建实体类

创建员工实体类,代码如下:

package com.tarena.entity;

import java.sql.Date;

/**
 * 员工表的实体类
 */
public class Emp {

	private Integer empno;
	private String ename;
	private String job;
	private Integer mgr;
	private Date hiredate;
	private Double sal;
	private Double comm;
	private Integer deptno;

	public Integer getEmpno() {
		return empno;
	}

	public void setEmpno(Integer empno) {
		this.empno = empno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public Integer getMgr() {
		return mgr;
	}

	public void setMgr(Integer mgr) {
		this.mgr = mgr;
	}

	public Date getHiredate() {
		return hiredate;
	}

	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}

	public Double getSal() {
		return sal;
	}

	public void setSal(Double sal) {
		this.sal = sal;
	}

	public Double getComm() {
		return comm;
	}

	public void setComm(Double comm) {
		this.comm = comm;
	}

	public Integer getDeptno() {
		return deptno;
	}

	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}

}

步骤五:创建DAO接口

创建员工DAO接口EmpDao,并在接口中增加查询全部员工的方法,代码如下:

package com.tarena.dao;

import java.util.List;
import com.tarena.annotation.MyBatisRepository;
import com.tarena.entity.Emp;

/**
 * 员工表的DAO组件
 */
@MyBatisRepository
public interface EmpDao {

	List<Emp> findAll();

}

步骤六:创建MyBatis映射文件

创建员工表的MyBatis映射文件EmpMapper.xml,并在该文件中实现查询全部员工数据所需的配置,代码如下:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "/ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="com.tarena.dao.EmpDao">

	<!-- 查询全部的员工 -->
	<select id="findAll" 
		resultType="com.tarena.entity.Emp">
		select * from t_emp
	</select>
	
</mapper>

步骤七:创建Junit测试类

创建Junit测试类,测试EmpDao中的findAll方法,代码如下:

package com.tarena.test;

import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.dao.EmpDao;
import com.tarena.entity.Emp;

/**
 *	EmpDao测试类
 */
public class TestEmpDao {
	
	/**
	 * 测试查询全部员工
	 */
	@Test
	public void testFindAll() {
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(
					"applicationContext.xml");
		EmpDao dao = ctx.getBean(EmpDao.class);
		List<Emp> list = dao.findAll();
		for(Emp e : list) {
			System.out.println(
				e.getEmpno() + " " +
				e.getEname() + " " +
				e.getJob()
			);
		}
	}
	
}

1.4 完整代码

员工表的建表脚本完整代码如下:

--员工表
create table t_emp(
	empno number(4) primary key,
	ename varchar(20),
	job varchar(10),
	mgr number(4),
	hiredate date,
	sal number(9,2),
	comm number(9,2),
	deptno number(4)
);

--员工表sequence
create sequence emp_seq start with 100;

--预置的员工表数据
ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy mm dd hh24:mi:ss';
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (1,'SMITH','CLERK',3,'1980-5-12',800,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (2,'ALLEN','SALESMAN',3,'1981-6-3',1600,300,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (3,'WARD','SALESMAN',4,'1990-3-15',1250,500,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (4,'JONES','MANAGER',5,'1985-4-8',2975,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (5,'MARTIN','SALESMAN',7,'1986-3-8',1250,1400,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (6,'BLAKE','MANAGER',9,'1989-6-1',2850,null,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7,'CLARK','MANAGER',9,'1995-10-1',2450,null,10);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (8,'SCOTT','ANALYST',9,'1993-5-1',3000,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (9,'KING','PRESIDENT',null,'1988-8-8',5000,null,10);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (10,'TURNER','SALESMAN',5,'1983-2-1',1500,0,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (11,'ADAMS','CLERK',5,'1992-7-3',1100,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (12,'JAMES','CLERK',1,'1996-9-10',950,null,30);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (13,'FORD','ANALYST',1,'1993-1-1',3000,null,20);
insert into t_emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (14,'MILLER','CLERK',3,'1983-10-9',1300,null,10);
commit;

MyBatis注解完整代码如下:

package com.tarena.annotation;

/**
 * MyBatis使用该注解标识DAO
 */
public @interface MyBatisRepository {

}

员工实体类Emp完整代码如下:

package com.tarena.entity;

import java.sql.Date;

/**
 * 员工表的实体类
 */
public class Emp {

	private Integer empno;
	private String ename;
	private String job;
	private Integer mgr;
	private Date hiredate;
	private Double sal;
	private Double comm;
	private Integer deptno;

	public Integer getEmpno() {
		return empno;
	}

	public void setEmpno(Integer empno) {
		this.empno = empno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public Integer getMgr() {
		return mgr;
	}

	public void setMgr(Integer mgr) {
		this.mgr = mgr;
	}

	public Date getHiredate() {
		return hiredate;
	}

	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}

	public Double getSal() {
		return sal;
	}

	public void setSal(Double sal) {
		this.sal = sal;
	}

	public Double getComm() {
		return comm;
	}

	public void setComm(Double comm) {
		this.comm = comm;
	}

	public Integer getDeptno() {
		return deptno;
	}

	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}

}

DAO接口完整代码如下:

package com.tarena.dao;

import java.util.List;
import com.tarena.annotation.MyBatisRepository;
import com.tarena.entity.Emp;

/**
 * 员工表的DAO组件
 */
@MyBatisRepository
public interface EmpDao {

	List<Emp> findAll();

}

MyBatis映射文件EmpMapper.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "/ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="com.tarena.dao.EmpDao">

	<!-- 查询全部的员工 -->
	<select id="findAll" 
		resultType="com.tarena.entity.Emp">
		select * from t_emp
	</select>
	
</mapper>

Junit测试类完整代码如下:

package com.tarena.test;

import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.dao.EmpDao;
import com.tarena.entity.Emp;

/**
 *	EmpDao测试类
 */
public class TestEmpDao {
	
	/**
	 * 测试查询全部员工
	 */
	@Test
	public void testFindAll() {
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(
					"applicationContext.xml");
		EmpDao dao = ctx.getBean(EmpDao.class);
		List<Emp> list = dao.findAll();
		for(Emp e : list) {
			System.out.println(
				e.getEmpno() + " " +
				e.getEname() + " " +
				e.getJob()
			);
		}
	}
	
}

2 使用Spring MVC,完成员工查询功能

2.1 问题

使用Spring MVC,完成员工查询功能。

2.2 方案

应用Spring MVC,处理页面请求。

2.3 步骤

步骤一:配置web.xml

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

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="/java.sun.com/xml/ns/javaee" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="/java.sun.com/xml/ns/javaee 
	/java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  	
 <!-- 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>  
  
</web-app>

步骤二:配置applicationContext.xml

在applicationContext.xml中追加如下配置:

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /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/tarena/entity/*.xml" />
	</bean>
	
	<!-- 配置MyBatis注解 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.tarena.dao" />
		<property name="annotationClass" value="com.tarena.annotation.MyBatisRepository" />
	</bean>
	
#cold_bold	<!-- 开启注解扫描 -->
#cold_bold	<context:component-scan base-package="com.tarena" />
#cold_bold	
#cold_bold	<!-- 开启RequestMapping注解 -->
#cold_bold	<mvc:annotation-driven />
#cold_bold	
#cold_bold	<!-- 处理请求转发 -->
#cold_bold	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
#cold_bold		<property name="prefix" value="/WEB-INF/"/>
#cold_bold		<property name="suffix" value=".jsp"/>
#cold_bold	</bean>	

</beans>

步骤三:创建业务控制器

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

package com.tarena.controller;

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="/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>

步骤五:测试

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

图-2

2.4 完整代码

web.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="/java.sun.com/xml/ns/javaee" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="/java.sun.com/xml/ns/javaee 
	/java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  	
  	<!-- 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>  
  
</web-app>

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /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/tarena/entity/*.xml" />
	</bean>
	
	<!-- 配置MyBatis注解 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.tarena.dao" />
		<property name="annotationClass" value="com.tarena.annotation.MyBatisRepository" />
	</bean>
	
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.tarena" />
	
	<!-- 开启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完整代码如下:

package com.tarena.controller;

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="/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>