Spring+SpringMVC+Mybatis整合步骤

SpringMVC配置

web.xml配置文件

    1.配置前端控制器:`org.springframework.web.servlet.DispatcherServlet`
    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    2.配置上下文加载监听器(用来加载所有的配置文件):`org.springframework.web.context.ContextLoaderListener`
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:application*.xml</param-value>
    </context-param>
    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    3.设置字符编码过滤器:`org.springframework.web.filter.CharacterEncodingFilter`
    <!-- 字符编码过滤 -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

前端控制器的配置文件(web.xml中的<servlet-name>中的名称+-servlet.xml)主要命名空间(mvc)

  • 配置控制器层要扫描的包(用来自动创建Controller层对象)
  • 加入注解驱动(使Controller对象能够对客户端请求进行映射)
  • 创建视图解析器:org.springframework.web.servlet.view.InternalResourceViewResolver
  • 根据需要创建文件上传解析器:org.springframework.web.multipart.commons.CommonsMultipartResolver
    -处理静态资源
<mvc:resources location="/WEB-INF/jsp/" mapping="/**"></mvc:resources>

Spring配置

applicationContext.xml(resources文件目录下)

  • 设置数据源(数据库连接池)(c3p0,durid等)
  • 设置事务管理器org.springframework.jdbc.datasource.DataSourceTransactionManager
  • 添加事务注解驱动,使事务注解生效<tx:annotation-driven />
  • 设置要扫描的包(Dao层与service层,使用@Repository和@Service注解标识)
  • 设置SqlSessionFactoryBeanorg.mybatis.spring.SqlSessionFactoryBean,同时配置映射文件的位置:
        <!-- 映射文件位置 -->
        <property name="mapperLocations" value="classpath*:com/ding/mapper/*.xml"></property>

用来自动创建具体的Dao接口与Mapper文件组合的实现类,还可以配置分页插件:

        <!-- 配置分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=mysql
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
  • 设置MyBatis要用到接口所在包
    <mybatis-spring:scan base-package="" />

配置Mybatis

   主要编写与dao接口对应的映射文件,放在resources下与配置中映射文件路径相同 

具体业务层开发

  • Dao接口:@Repository
  • Service层:@Service
  • Controller层:@Controller
  • 在层与层之间需要调用时,使用@Autowired注解通过IOC容器自动装配
  • Controller层用来获取参数及映射的注解:

@RequestMapping:前端控制器拦截请求,转发到控制层,执行SpringMVC控制层对应请求的方法执行不同的业务逻辑
@ResponseBody:将返回值对象转换成json对象返回给客户端
@SessionAttribute:此注解用在类上,将数据存储到session中

  • 映射请求参数的获取:

@RequestHeader:获取请求头中的数据参数
@CookieValue:获取cookie中的数据
@RequestParam(“name”):获取请求参数中指定名称的数据
直接使用pojo,SpringMVC会自动将请求中的参数赋值给pojo对象
支持原生的servletAPI:可以使用HttpServletRequest,HttpServletResponse,HttpSession等原生的API

猜你喜欢

转载自blog.csdn.net/dingse/article/details/80021985