经测试,需注意以下几点:

1,controller的自动扫描不能放在applicationContext.xml中,要放在spring-mvc.xml中。同样是<context:component-scan base-package="com.xxx.controller"></context:component-scan>。(不知道为啥必须这样,若相知,请相教!)

2,所有的classpath必须得小写"p",小写"p",小写"p"。下面代码中全部写成classPath,启动时不报错,但是运行时会报错。

以下配置可直接使用,只需更改包名。

关于内部标签的解释及用法,都以注解形式在代码内部说明。个人原创,转载需注明出处。

1,web.xml。添加jar包后首先需要配置WEB-INF下的web.xml文件

 <?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CRM</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <!-- 以下配置在spring文档中可通过搜索web-app获得(不包含中文编码器) --> <!-- spring的配置文件,默认放在WEB-INF下。这里将其放在src下,故需要如下配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:appliationContext.xml</param-value>
</context-param> <!-- 启动web时加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 添加springMVC支持,单独springMVC时也需要在web.xml文件中配置 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:spring-mvc.xml</param-value>
</init-param>
<!-- 启动web时就加载springmvc的servlet,即启动时就加载springmvc的配置文件 -->
<load-on-startup>1</load-on-startup>
<!-- 可异步执行,最好都配上,项目会比较流畅 -->
<!-- 配置异步时若报错,因为是3.0新特征,可将2.5改为3.0即可 -->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 拦截所有以.do结尾的请求,后续的业务代码请求后缀都将以.do结尾(个人习惯) -->
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 中文编码器,防止出现中文乱码 -->
<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> </web-app>

web.xml

2,applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- spring自动扫描base-package下及其子包下的所有Java文件,当扫描到含有@service或@controller注解时,自动将该类注册成bean -->
<!-- 不用再配置<context:annotation-config/>,因为已包含 -->
<context:component-scan base-package="com.xxx.service"></context:component-scan>
<context:component-scan base-package="com.xxx.controller"></context:component-scan> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_Name"></property>
<property name="userName" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classPath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classPath:com/xxx/mappers/*.xml"></property>
<property name="typeAliasesPackage" value="classPath:com.xxx.entity"></property>
</bean>
<!-- sqlSessionFactory的属性 -->
<!-- 1,dataSource:必须属性 -->
<!-- 2,configLocation:当mybatis-config.xml放在src下(个人习惯),配置该属性。 目前别名已经配置在SqlSessionFactoryBean里,可否省略mybatis-config.xml文件我也不清楚。若相知,请相告!谢谢! -->
<!-- 3,mapperLocations:自动扫描mapper.xml文件。 -->
<!-- 4,typeAliasesPackage:自动配置别名 --> <!-- 转换器MapperScannerConfig它可以将接口转换为Spring容器中的Bean,不需要注解(@service) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 配置事务通知 -->
<!-- 没有采用<tx:annotation-driven/>,即注解事务管理。是因为注解事务需要在很多public方法前加上@Transactional,很麻烦,用以下方法可以一劳永逸 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 拦截以'insert','add'...'find'等开头的方法名方法。最后一句代表拦截所有方法 --> <!-- 配置事务切面,将事务通过切面的方式嵌套入代码逻辑,从而不侵入代码业务 -->
<!-- 若需单独用切面,在定义<aop:config>时需要加入切面类,即<aop:aspect> -->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.xxx.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
<!-- 切点方法表达式格式 -->
<!-- execution(<scope> <return-type> <fully-qualified-class-name>.<method-name>.(parameters))
例: execution(* com.spring.service.*.*(..)):匹配service包及其子包(service.impl)所有类的所有方法;
execution(public List com.spring.service.impl.UserServiceImpl.getUserList(int,String)):
匹配UserServiceImpl类中返回List且方法名为getUserList且参数为int和String类型的所有公共方法 --> </beans>

applicationContext.xml

3,spring-mvc.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀,WEB-INF下的jsp文件夹 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀,以.jsp结尾 -->
<property name="suffix" value=".jsp" />
</bean> </beans>

spring-mvc.xml

4,mybatis-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 只需要一个空的<configuration>(必须,不写会报错),数据源、别名、mapper的扫描都已经在applicationContext.xml中定义 -->
<configuration></configuration>

mybatis-config.xml

ssm框架整合之Spring4+SpringMVC+Mybaties3之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/29的更多相关文章

  1. Spring+SpringMVC+Mybaties整合之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/27

    以下配置可直接使用,只需更改包名. 关于内部标签的解释及用法,都以注解形式在代码内部说明.个人原创,转载需注明出处. 1,web.xml.添加jar包后首先需要配置WEB-INF下的web.xml文件 ...

  2. SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)

    1.开发环境搭建以及创建Maven Web项目 参看之前的博文[确保maven web项目不报错]:http://www.cnblogs.com/cainiaomahua/p/6306476.html ...

  3. SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(Spring+SpringMVC+MyBatis) ]

    SSM_BookSystem SSM框架基础 SSM_BookSystem ---> Hello CRUD 说明:本项目目前包含基础的CRUD 日期:2017-05-01 22:25:37 作者 ...

  4. SSM框架整合(Spring + SpringMVC + MyBatis)

    搭建环境 使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层) Spring框架 创建数据库表 CREATE DATABASE ssm; USE ssm; C ...

  5. SSM框架整合(Spring、SpringMVC、Mybatis)

    #毫无疑问我们肯定是使用Spring去整合SpringMVC和Mybatis,在整合过程中我们首先要让各自的模块实现,然后再去使用Spring整合:比如我先实现Mybatis框架的配置,然后再通过测试 ...

  6. SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql

    准备工作: 下载整合所需的jar包 点击此处下载 使用MyBatis Generator生成dao接口.映射文件和实体类 如何生成 搭建过程: 先来看一下项目的 目录结构 1.配置dispatcher ...

  7. SSM框架整合环境构建——基于Spring4和Mybatis3

    目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...

  8. springmvc(二) ssm框架整合的各种配置

    ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...

  9. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

随机推荐

  1. WebLogic11g-创建域(Domain)及基本配置

    转:http://www.codeweblog.com/weblogic11g-%e5%88%9b%e5%bb%ba%e5%9f%9f-domain-%e5%8f%8a%e5%9f%ba%e6%9c% ...

  2. Core Animation1-简介

    一.Core Animation简介 * Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代 ...

  3. js 正则表达式符号含义

    \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个单词的边界. -或- 对正则表达式功 ...

  4. GNU Radio: 自定义 block 实例

    综述 本文通过在GNU Radio 中编写一个block的例子,系统介绍创建一个block的过程.该 block 的功能是可以在GRC中通过滑块(WX GUI Slider)来实时改变信号源(Sign ...

  5. mysql大数据量之limit优化

    背景:当数据库里面的数据达到几百万条上千万条的时候,如果要分页的时候(不过一般分页不会有这么多),如果业务要求这么做那我们需要如何解决呢?我用的本地一个自己生产的一张表有五百多万的表,来进行测试,表名 ...

  6. Cousera 无法播放视频 解决办法 widows 和 linux

    查资料得知,Cousera无法播放课程视频原因在于DNS污染. 尽管通过FQ软件把视频看完了,在最后一课找到了这个解决办法,现在拿出来分享给大家: Windows: 请参照以下链接: http://j ...

  7. FPGA设计者必须精通的5项基本功

    FPGA设计者的5项基本功:仿真.综合.时序分析.调试.验证. 对于FPGA设计者来说,练好这5项基本功,与用好相应的EDA工具是同一过程,对应关系如下: 1. 仿真:Modelsim, Quartu ...

  8. scanf在竞赛中的技巧总结ing

    前言 当输入流是一个字符串,我们需要在其中提取我们所需要的数值时,我们可以在读入阶段就完成数据的筛选工作. 使用方法 scanf("%ns", str); 表示读取长度为n的字符串 ...

  9. linux tcp server demo

    #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <ne ...

  10. docker 远程rest api 访问配置

    Docker RestApi 的配置及使用 Centos Docker1.12 远程Rest api访问的配置方法 http restapiv1.24 docker sdk for python