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

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

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

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

    经测试,需注意以下几点: 1,controller的自动扫描不能放在applicationContext.xml中,要放在spring-mvc.xml中.同样是<context:componen ...

  2. spring+springmvc+mybaties整合实例

    spring+springmvc+mybaties即SSM框架整合在ecpliseee中开发:很么多西都是只有只有自己上手做,才会懂.昨晚熬了很久,才弄出来.也希望对新手有帮助!下面整理一下思路:关键 ...

  3. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  4. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  5. 框架篇:Spring+SpringMVC+hibernate整合开发

    前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...

  6. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

  7. spring+springmvc+hibernate整合遇到的问题

    spring+springmvc+hibernate整合遇到的问题2016年10月20日 23:24:03 守望dfdfdf 阅读数:702 标签: ssh学习经历的异常exception异常框架更多 ...

  8. ssm之spring+springmvc+mybatis整合初探

    1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1. ...

  9. springmvc框架(Spring SpringMVC, Hibernate整合)

    直接干货 model 考虑给用户展示什么.关注支撑业务的信息构成.构建成模型. control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献: view怎样对数据进行布局,以一种优美的方式展示 ...

随机推荐

  1. 获取手机 id 与 ip

    //id #import <AdSupport/AdSupport.h> //ip #include <ifaddrs.h> #include <arpa/inet.h& ...

  2. MySQL查询语句的45道练习

              一.设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四) ...

  3. JavaSE之认识java

    本来很早之前就应该总结自己在JavaSE中系统学到的知识了,马上就要出去工作了,想想自己还是非常菜的菜鸟,自己就夜不能寐呀.现在从zero基础开始带大家一起回顾学习的基础知识. 现在已经是凌晨了,但是 ...

  4. CSS限制字数,超出部份显示点点点...

    最近项目中需要用CSS实现限制字数,超出部份显示点点点...,只需要一下代码即可: width:400px;/*要显示文字的宽度*/ text-overflow :ellipsis; /*让截断的文字 ...

  5. 解决MVC模型验证在IE 6 7下不起作用或者报错

    文件版本列出: Jquery版本1.7.1 jQuery Validation 版本1.9.0 (VS2012创建MVC项目自动生成的版本) 最好VS2012创建MVC项目自动生成的版本,而不是VS2 ...

  6. iOS全角符与半角符之间的转换

    iOS全角符与半角符之间的转换 相关资料: 函数『CFString​Transform』中文 详情: 问题 1.17-03-15,「有人在群里边问怎么把『半角』符字符串转换成『全角』字符串?」,百度的 ...

  7. 对于自定义标签类中JspBody类的invoke方法的理解

    下面是javaeeAPI中对于invoke()方法的介绍: 其中的参数out是一个Writer类的对象,如果写null,就是将标签体内容写到了与此jsp相关联的JspWriter对象,也就是下面的w: ...

  8. python学习之路-书籍推荐

    学python有一段时间了,总结走来的路,发现还是看书靠谱,当然也要多实践. 一.入门篇 1.简明 Python 教程(A Byte of python) http://www.kuqin.com/a ...

  9. Python 一些有趣的技巧哦!

    #Python 技巧命令 python 如一股清流,可以说屌到飞起,下面咱就来看看一些屌的东西 ### python2 最简单的web服务 ` python -m SimpleHTTPServer 8 ...

  10. post和get请求的区别

    post和get请求的区别: 1.post发送的数据在请求体中,用户看不到 get发送的数据在地址栏中 2.post请求中有content-type,作用是告诉服务器,发送给服务器的数据格式,是和ur ...