前提

  工作环境:JDK 1.8、Mysql 5.7.18、Intellij IDEA 2018.1、Tomcat 8.5、Maven

  框架版本:Spring 4.2.0.RELEASE、SpringMVC 4.2.0.RELEASE、MyBatis 3.3.0

一、项目目录结构

目录结构如下:

接口实现代码 cn.java

cn.java.controller 请求接口  cn.java.entity 实体对象 cn.java.service 业务接口层  cn.java.service.impl业务实现层  cn.java.mapper数据库相关  cn.java.controller.exception 捕获异常

cn.java.filters 过滤器(针对JSP)   cn.java.interceptors 拦截器(针对controller)   cn.java.tasks 任务

cn.java.utils 工具集

resource配置文件(db/springmvc/log4j等)

applicationContext.xml  database.properties  log4j.porperties  springmvc.xml

前端文件(WebContent)

WebContent/pages/front 存放前端页面(jsp文件)   /WebContent/resources/css 存放css文件

/WebContent/resources/images 存放图片         /WebContent/resources/js  存放js

/WebContent/resources/upload 上传             /WebContent/lib/  存放工程中lib包

/WebContent/WEB-INF/web.xml工程配置文件     /WebContent/index.jsp工程默认首页jsp

二、项目入口web.xml

JavaEE框架,入口基本都是在web.xml,这一点要记住      ;

那么Web.xml 文件的作用是什么呢?

web.xml文件是用来初始化配置信息:比如index页面、servlet、servlet-mapping、filter、listener、启动加载级别等。

当然web.xml 也不是必须要有的,当你的web工程没用到上述配置信息时,你可以不用web.xml文件来配置你的Application。

 web.xml 内容很多,SSM关键部分是

1.加载Spring配置文件applicationContext.xml

2.将SpringMVC核心调度器DispatcherServlet注册为servlet(配置文件为springmvc.xml)

web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!—定义了web应用的名称-->
<display-name>TestPlatform</display-name> <!—指定服务器在收到一个引用目录名而不是文件名的url时使用哪个文件-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置过滤器,解决post的乱码问题 -->
<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监听器,可以在容器启动时加载contextConfigLocation的context-param节点的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--=============配置SpringMVC核心调度器================ --> <servlet>
<servlet-name>dispatcherServlet</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>
<load-on-startup>1</load-on-startup>#代表启动顺序,设置为>0表示容器在应用启动时加载并初始化这个servlet
</servlet> <servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping> <!—指定错误404的处理页面-->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/pages/error/404.jsp</location>
</error-page>
</web-app>

  

三、Spring 框架配置(applicationContext.xml)

主要完成spring与mybatis的整合

<?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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<context:component-scan base-package="cn.java.service.impl" />#注:主容器中不扫描@Controller注解,因为@Controller将会在SpringMVC扫描
<!-- 读取database.properties文件 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 指定properties文件所在路径 -->
<property name="location" value="classpath:database.properties"></property>
</bean>
<!-- 配置数据源 -->
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 配置driverClassName、url、username、password -->
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="${maxActive}" />
<property name="minIdle" value="${minIdle}" />
</bean>
<!-- 配置扫描保存sql语句的局部xml文件 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="basicDataSource"></property>
<!-- 指定局部xml文件的位置 -->
<property name="mapperLocations" value="classpath*:cn/java/mapper/*.xml"></property>
</bean> <!-- 扫描mapper接口类,并且将接口类与xml文件关联 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定mapper接口类存放的位置 -->
<property name="basePackage" value="cn.java.mapper"></property>
</bean>
<!-- 配置事务 -->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="basicDataSource" />
</bean>
事务注解驱动
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
</beans>

请注意:

1、多文件配置(通常有以下两种做法)

 (1)在 web.xml配置中的contextConfigLocation节点配置多个值。

 (2)在一个application.xml中配置多个import标签引入其他文件。

2、mybatis相关配置,主要的就是自动扫描,自动注入,配置数据库。(引用)

<!-- 1. 数据源配置 -->

    <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />

    <!-- Druid方式配置数据源 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 

        <!-- 基本属性 url、user、password -->

        <property name="driverClassName" value="${jdbc.driverClassName}" />

        <property name="url" value="${jdbc.url}" />

        <property name="username" value="${jdbc.username}" />

        <property name="password" value="${jdbc.password}" />

        <!-- 配置初始化大小、最小、最大 -->

        <property name="initialSize" value="10" /> 

        <property name="minIdle" value="20" /> 

        <property name="maxActive" value="100" /> 

        <!-- 配置获取连接等待超时的时间 -->

        <property name="maxWait" value="60000" /> 

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

        <property name="timeBetweenEvictionRunsMillis" value="6000" /> 

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

        <property name="minEvictableIdleTimeMillis" value="300000" /> 

            <!-- 验证是否回收 -->

        <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> 

        <property name="testWhileIdle" value="true" /> 

        <property name="testOnBorrow" value="false" /> 

        <property name="testOnReturn" value="false" />

            <!-- 开启Druid的监控统计功能 --> 

        <property name="filters" value="stat" /> 

    </bean>

        <!-- 2. 创建SqlSession的工厂 -->

    <!-- dataSource:引用数据源,统一加载配置-->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 

        <property name="dataSource" ref="dataSource" ></property>   

        <!-- 自动配置别名-作用类似mybatis-config.xml的别名 --> 

        <property name="typeAliasesPackage" value="com.demo.model" />

        <!-- 设置别名的类加上父类限定 --> 

        <property name="typeAliasesSuperType" value="com.demo.common.base.BaseEntity"/>

        <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径 -->  

        <!-- *是个通配符,代表所有的文件,**代表所有目录下 -->

        <property name="mapperLocations" value="classpath*:mappings/**/*.xml"/>

         <!-- 指定mybatis核心配置文件 -->

        <property name="configLocation" value="classpath:mybatis-config.xml"></property>

    </bean> 

    <!-- 3. 自动扫描加载Sql映射文件/接口 -->

    <bean id="mapperScannerConfigurer"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <!-- sqlSessionFactoryBeanName:代表延迟加载-->

        <!-- 这个配置的前提条件是:映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下(com.demo.mapper)-->

        <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

        <!-- basePackage:指定sql映射文件/接口所在的包(自动扫描)-->

        <property name="basePackage" value="com.demo.mapper"></property>

           <!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->

        <property name="annotationClass" value="com.demo.common.persistence.annotation.MyBatisDao"/>

    </bean>

        <!-- 4. 事务管理 -->

    <!-- dataSource:引用上面定义的数据源 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

    <!-- 5. 使用声明式事务 -->

    <!-- transaction-manager:引用上面定义的事务管理器 -->

    <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务  -->

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <!-- 定义JdbcTemplate的Bean -->

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"></bean>

  

四、SpringMVC 框架配置

这块配置主要是自动扫描控制器,视图模式,注解的启动

Springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- 配置包扫描 -->
<context:component-scan base-package="cn.java.controller.*" />
<!-- mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 定义Spring MVC的拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截所有请求 -->
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/go2Login.shtml" />
<mvc:exclude-mapping path="/login.shtml" />
<mvc:exclude-mapping path="/loginForm.shtml"/>
<!-- 自定义判断用户权限的拦截类 -->
<bean class="cn.java.interceptors.OneInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 配置文件上传的大小 -->
<property name="maxUploadSize" value="1048576"></property>
</bean>
<!-- 数据校验(hibernate-validator) -->
<!-- 定时器 -->
</beans>

  

 

springmvc+spring+mybatis 项目配置的更多相关文章

  1. SpringMVC+Spring+mybatis项目从零开始--分布式项目结构搭建

    转载出处: SpringMVC+Spring+mybatis+Redis项目从零开始--分布式项目结构搭建 /** 本文为博主原创文章,如转载请附链接. **/ SSM框架web项目从零开始--分布式 ...

  2. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  3. ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)

    Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...

  4. SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

    转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...

  5. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】

    本人做过一年的MATLAB编程和简单维护过VB和C++的项目.是跟着网上获得的黑马的Java双元视频课来自学入门Java知识和常用框架的使用. 淘淘商城(SpringMVC+Spring+Mybati ...

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十二天】(系统架构讲解、nginx)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  8. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十一天】(购物车+订单)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  9. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

随机推荐

  1. TCP的三次握手与四次挥手的理解

    本文经过借鉴书籍资料.他人博客总结出的知识点,欢迎提问    序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生:给字节编上 ...

  2. java语言实现对程序设计语言源文件统计字符数、单词数、行数及其他拓展功。

    本次作业Github项目地址:https://github.com/YiChenglong2018/WordCount 一.项目简介 本项目的需求可以概括为:对程序设计语言源文件统计字符数.单词数.行 ...

  3. python_生成器

    生成器: # 生成器函数(内部是否包含yield) def func(): print('F1') yield 1 print('F2') yield 2 print('F3') yield 100 ...

  4. Pycharm----设置默认脚本请求头

    每次新建py文件,均需要在文件头部加上编码声明,每次的手动添加比较麻烦,因此设置自动生成,也可添加作者.时间等等,详见如下: 设置后的样例显示: 操作方式: 操作完如上的截图步骤,再次新建一个py文件 ...

  5. 编译安装PTLib和H.323 Plus Core

    下载PTLib和H.323 Plus Core,官方网站很容易下载:http://www.h323plus.org/source/. 我下载这两个库后存放到目录/home/ynq/h323plus下, ...

  6. BZOJ 3931 / Luogu P3171 [CQOI2015]网络吞吐量 (最大流板题)

    题面 中文题目,不解释: BZOJ传送门 Luogu传送门 分析 这题建图是显然的,拆点后iii和i′i'i′连容量为吞吐量的边,根据题目要求,111和nnn的吞吐量看作∞\infty∞. 然后用di ...

  7. [ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array

    ES2019 introduces the Array.prototype.flatMap method. In this lesson, we'll investigate a common use ...

  8. [USACO10HOL]赶小猪

    嘟嘟嘟 这题和某一类概率题一样,大体思路都是高斯消元解方程. 不过关键还是状态得想明白.刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\fr ...

  9. 【概率论】1-4:事件的的并集(Union of Events and Statical Swindles)

    title: [概率论]1-4:事件的的并集(Union of Events and Statical Swindles) categories: Mathematic Probability key ...

  10. java集合类-List接口

    List接口包含List接口及List接口的所有实现类,List集合中的元素允许重复. List接口 List接口继承了Collection接口,包含Collection接口的所有方法,还定义了两个非 ...