1、导包
2、配置web.xml
<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:springmvc-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3、在src下创建springmvc-servlet.xml和springmvc-context.xml
配置springmvc-servlet.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:util="http://www.springframework.org/schema/util"
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/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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="com.lzcc" />
<!-- spring mvc3.1版本之前,使用以下配置映射器和适配器 -->
<!-- 注解映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
<!-- 注解适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
<!-- 3.1版本后采用注解扫描,代替上面的注解器和映射器 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
4、配置springmvc-context.xml
<!-- 数据源 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3307/train01"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- session工厂,SqlSessionFactoru:为整合应用提供sqlSession对象资源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定链接数据库的数据源 -->
<property name="dataSource" ref="ds"/>
<!-- 指定Mapper文件的存放位置 -->
<property name="mapperLocations" value="classpath:com/lzcc/dao/*.xml"/>
</bean>
<!-- 扫描指定包下的所有接口,MapperScannerConfigurer:根据指定包批量扫描Mapper接口并生成实例 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lzcc.dao"></property>
</bean>
5、Controller层
@Controller
public class EmpController {
//注解service层
@Resource
private EmpService empService;
//前台URI映射路径
@RequestMapping("/hello")
public ModelAndView hello(){
/**
* 通过ModelAndView向前台传递数据
* ModelAndView()括号中为跳转到的前台页面路径,也可以通过mav.setViewName(viewName)
* 这种方式定义跳转页面
*/
ModelAndView mav = new ModelAndView("succ");
List<Emp> list = empService.findAll();
if(list != null && list.size() > 0){
mav.addObject("list", list);
}
return mav;
}
注意:spring mvc 与 JSON交互
导入jackson jar包
》接收json数据:@RequestBody
》向前台传递json数据:@ResponseBody
具体参考:http://blog.csdn.net/eson_15/article/details/51742864
6、Service层
@Service
public class EmpService {
@Resource//注解dao层
private EmpMapper empMapper;
public List<Emp> findAll(){
List<Emp> list = empMapper.findAll();
return list;
}
}
7、DAO层(创建映射类和相应的映射xml)
映射类:
@Repository//DAO层,定义为接口
public interface EmpMapper {
List<Emp> findAll();
}
映射xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- namespace:DAO映射类 -->
<mapper namespace="com.lzcc.dao.EmpMapper">
<!-- id:用于标示,resultType:用于返回结果类型 ;另外给参数赋值用 #{参数名称}-->
<select id="findAll" resultType="com.lzcc.entity.Emp">
select * from tousu;
</select>
</mapper>
至此Spring mvc + Mybatis整合完成