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

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. 每天一个linux命令:【转载】touch命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a    ...

  2. [BZOJ5329][SDOI2018]战略游戏

    bzoj luogu Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任 ...

  3. HDU4864 Task

    题意 Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, t ...

  4. spring--集合注入(常规方法)

    数据,list,set,map,Properties 集合注入 package Spring_collections; /** * Created by luozhitao on 2017/8/11. ...

  5. Nomad 了解

    Introduction to Nomad Welcome to the intro guide to Nomad! This guide is the best place to start wit ...

  6. FastAdmin 无刷新地址改变

    FastAdmin 无刷新地址改变 群里有人问 FastAdmin 是不是用了 pjax? 之前有看到 Karson 回复过,其实 FastAdmin 用的是 HTML5 的一个History API ...

  7. 关于RAS加密中pfx格式提取字符串私钥 (转)

    openssl 使用以下命令 如果如果执行出错,请查看是否安装环境或在命令前面加上openssl --提取出来的无法直接使用 pkcs12 -in zhaoshang2.pfx -nocerts -n ...

  8. zk中文乱码问题

    之前讲了怎么把数据导入到zookeeper(见zookeeper事件监听的importData方法),虽然本机win10的zookeeper展示没问题,但到了linux上就出现乱码了: << ...

  9. RK3288 usb 摄像头旋转

    系统:Android 5.1 下面实现了摄像头 180 度旋转,旋转角度只需修改 orientation. diff --git a/hardware/rockchip/camera/CameraHa ...

  10. 【Leetcode 371】Sum of Two Integers

    问题描述:不使用+是或-操作符进行整数的加法运算 int getSum(int a, int b); 我的思路:把整数化成二进制进行运算,注意类型是int,也就是要考虑负数.关于负数的二进制表示可见之 ...