【前言】关于项目整合的资料已经很多了,至少可以在网上有两三种方法,而且都有自己的注解版和配置文件版,有些要把别人讲的变为自己能理解的 ,下面主要是根据一位 大神的项目进行理解进行项目的搭建
1 配置 web.xml文件,这里需要四个地方要更改
1) <listener>用来监听配置文件
2) 写一个参数长了<context-param> contextConfigLocation,这个参数在 监听器类里要用到
3) 核心的控制器<servlet>其中还配置了 springmvc 的配置文件 的路径,作为一个初始化的常量的值contextConfigLocation,可以看到,web启动需要加载两个配置文件
因为有两个contextConfigLocation
4) 添加 编码过滤器,这个是springmvc自带的(应该可以这么说吧)
看下源码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring_test</display-name>
<!-- 配置文件位置,默认为/WEB-INF/applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 字符集过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 上下文Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- servlet控制跳转 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context-dispatcher.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2 加载的
这个文件就是原来的 springmvc的主配置文件里面基本没变
<param-value>classpath:context-dispatcher.xml</param-value>
仍然分三个部分
1) 视图映射器
2)<context:component-scan> 扫描子包路径
3) <mvc:annotation-driven />
具体的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.geloin.spring" />
<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp"></property>
</bean>
<!-- <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8" /> -->
</beans>
3 实体类 Menu 不写了,容易扰乱主要内容
4 控制器类
@Controller 标注控制器
@RequestMapping(value = "background")标注在类上做命名空间,标注在方法上做路径,过滤为 .html【web.xml文件中定义】
@ @Resource(name = "menuService") 标注资源 【问:不需要set/.get方法?】
return new ModelAndView("background/menu", map);
具体配置如下
/**
*
* @author geloin
* @date 2012-5-5 上午9:31:52
*/
package com.geloin.spring.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.geloin.spring.entity.Menu;
import com.geloin.spring.service.MenuService;
/**
*
* @author geloin
* @date 2012-5-5 上午9:31:52
*/
@Controller
@RequestMapping(value = "background")
public class LoginController {
@Resource(name = "menuService")
private MenuService menuService;
/**
*
*
* @author geloin
* @date 2012-5-5 上午9:33:22
* @return
*/
@RequestMapping(value = "to_login")
public ModelAndView toLogin(HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
List<Menu> result = this.menuService.find();
map.put("result", result);
return new ModelAndView("background/menu", map);
}
}
5 框架整合最核心的部分就是 mybatis主配置文件和 spring 的文件整合,下面先看下 Mapper接口
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:34
*/
package com.geloin.spring.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.geloin.spring.entity.Menu;
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:34
*/
@Repository(value = "menuMapper")
public interface MenuMapper {
@Select(value = "${sql}")
@Results(value = { @Result(id = true, property = "id", column = "id"),
@Result(property = "parentId", column = "c_parent_id"),
@Result(property = "url", column = "c_url"),
@Result(property = "isShowLeft", column = "c_is_show_left"),
@Result(property = "name", column = "c_name") })
List<Menu> operateReturnBeans(@Param(value = "sql") String sql);
}
个人感觉 @Results(..)这部分用处不大,可以省略,这些是mybatis的注解方式,应该好理解,他的传参方式值得学习
6 下面就是主要的配置文件了,有以下几个部分
1)引入属性文件为了配置数据源,所以也要记得写属性文件,这个很简单
2)配置数据源
3)引入事务的注释 <tx:annotation-driven />
4) 事务要链接数据源
5) 创建数据源 sqlsessionFactory
主要的配置如下
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 可通过注解控制事务 -->
<tx:annotation-driven />
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--创建jdbc数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.geloin.spring.mapper" />
</bean>
</beans>
个人感觉 标红的 配置才是整合的关键所在