ssm框架整合之Spring4+SpringMVC+Mybaties3之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/29
经测试,需注意以下几点:
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的更多相关文章
- Spring+SpringMVC+Mybaties整合之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/27
以下配置可直接使用,只需更改包名. 关于内部标签的解释及用法,都以注解形式在代码内部说明.个人原创,转载需注明出处. 1,web.xml.添加jar包后首先需要配置WEB-INF下的web.xml文件 ...
- SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)
1.开发环境搭建以及创建Maven Web项目 参看之前的博文[确保maven web项目不报错]:http://www.cnblogs.com/cainiaomahua/p/6306476.html ...
- SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(Spring+SpringMVC+MyBatis) ]
SSM_BookSystem SSM框架基础 SSM_BookSystem ---> Hello CRUD 说明:本项目目前包含基础的CRUD 日期:2017-05-01 22:25:37 作者 ...
- SSM框架整合(Spring + SpringMVC + MyBatis)
搭建环境 使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层) Spring框架 创建数据库表 CREATE DATABASE ssm; USE ssm; C ...
- SSM框架整合(Spring、SpringMVC、Mybatis)
#毫无疑问我们肯定是使用Spring去整合SpringMVC和Mybatis,在整合过程中我们首先要让各自的模块实现,然后再去使用Spring整合:比如我先实现Mybatis框架的配置,然后再通过测试 ...
- SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql
准备工作: 下载整合所需的jar包 点击此处下载 使用MyBatis Generator生成dao接口.映射文件和实体类 如何生成 搭建过程: 先来看一下项目的 目录结构 1.配置dispatcher ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- springmvc(二) ssm框架整合的各种配置
ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
随机推荐
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- HDU2896 病毒侵袭 【AC自动机】
HDU2896 病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一 ...
- vs2013 快捷键
//////////////// 编辑: ctrl+-(shift+ctrl+-):移动光标到上次位置或相反,比如定位一个函数,转到函数定义后想回到函数使用处,则用ctrl+-,若又想回到函数 ...
- openssl 查看证书细节
打印证书的过期时间 openssl x509 -in signed.crt -noout -dates 打印出证书的内容: openssl x509 -in cert.pem -noout -text ...
- Elixir's keyword lists as option parameters
备注: 文章转自:https://www.djm.org.uk/posts/writing-extensible-elixir-with-behaviours-adapters-pluggable-b ...
- lapis 集成openresty最新版本cjson 问题的解决
备注: 为了解决安装了lapis.同时又希望使用新版nginx 以及openresty 的特性(stream ...) 1. 解决方法 参考: https://github.com/leaf ...
- HDFS(三)
DataNode 下面的数据文件有两种类型,一种是数据块,一种是数据块的描述文件(元数据文件),后者文件后面带有.meta后缀: Version文件字段内容其实和NameNode里面涵义是一致的: 安 ...
- winform下实现pictureBox全屏播放
最近开发一个项目,需要通过双击pictureBox实现全屏的功能,网上查找资料,加上一点摸索,最终实现了.做一下记录,以备以后需要. 主要功能都在下面这个类里面 using System; using ...
- Erlang ets -- something about cache
都说用ets 写一个cache 太简单, 那就简单的搞一个吧, 具体代码就不贴了, 就说说简要的需求和怎么做(说设计有点虚的慌). 需求场景 >> 查询系统,对于主存储而言,一次写入多次查 ...
- Jmeter录制App 请求是HTTPS的
1.jmeter开启代理后,在bin目录下找到 证书 2.把这个证书通过QQ发送到手机上面,使用QQ浏览器打开 安装证书,信任证书 3.jmeter里点击SSl管理器选择上面的证书(这部貌似为了抓浏览 ...