整合说明
- springmvc是spring的子框架,不用进行整合
- 配置文件为 springmvc-servlet.xml
spring和mybatis是需要整合的,最主要的!
- 配置文件,将原本的application.xml拆分为三部分好方便管理。
- applicationContext-dao.xml
- aapplicationContext-service.xml
- applicationContext-transaction.xml
- 你会发现mybatis的配置文件sqlMapConfig.xml里面的配置基本都转移了。
- 配置文件,将原本的application.xml拆分为三部分好方便管理。
版本
- springmvc 4.3.5
- spring 4.3.5
- mybatis 3.3.0
相关jar包下载地址
导入相关Jar包
- 相关jar包,简单说一些
- springmvc和spring类
- spring-aop
- spring-beans
- spring-core Spring核心
- spring-webmvc-4.3.5 SpringMCV核心
- ……
- mybatis类
- mybatis-3.3.0 Mybatis核心
- mybatis-spring-1.3.1 用于整合Spring和Mybatis
- commons类
- commom-dbcp2-2.1.1 common数据源第二版本
- common-pool2-2.4.2
- common-logging-1.2
- ……
- mysql类
- mysql-connector-java-5.1.7 连接MySQL
- log4j类
- log4j-1.2.17 日志输出
- log4j-api-2.2
- log4j-core-2.2
- springmvc和spring类
项目目录结构

- controller 为控制器
- mapper 里面写mapper.xml和mapper.java
- pojo 为数据模型层
- service 为业务接口层
- serviceImpl 为业务实现层
- test 测试
框架整合配置文件详细说明
整合Web项目和Spring,需要在web.xml中配置。通过监听器,实现框架的整合(在监听器中,实现了Spring框架的初始化设置)
1234567891011<content-param><description>设置Spring配置文件的位置</description><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></content-param><!-- 这里classpath:后面你需要填写的Spring配置文件的位置,这里这样写是因为Spring的配置文件直接放在src根目录下 --><listener><description>加载Spring配置文件,并执行初始化操作</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
整合Web项目和SpringMVC,需要在web.xml中配置。通过一个servlet实现框架的整合。
123456789101112<servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- override default name {servlet-name}-servlet.xml --><param-name>contextConfigLocation</param-name><param-value>classpath:/WEB-INF/springmvc-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet>Web项目和Spring、SpringMVC整合完毕了。那么,现在来看看Spring和SpringMVC的整合。
前面说过,SpringMVC是Spring的子框架,那么整合的事情看起来是如此和谐,需要的配置文件就是上面的
<param-value>classpath:/WEB-INF/springmvc-servlet.xml</param-value>
中所包含的springmvc-servlet.xml。123456789101112<!--配置自动扫描,之前需要将各种bean利用id和ref属性手动来关联起来,这是十分繁杂的。现在,我们只需要利用Spring提供的自动扫描,即可轻松解决。base-package是指明需要扫描的包,Spring将扫描该文件夹,并找出Bean(注解为@Component)并注册到Spring容器中。在Spring中,有4中种类型的组件自动扫描注解类型1.@Component:自动扫描组件2.@Repository:持久层DAO组件3.@Service:业务层组件4.@Controller:表示层控制器组件--><context:component-scan base-package="" />web.xml
|
|
- springmvc-servlet.xml
|
|
- applicationContext-dao.xml
|
|
- applicationContext-service.xml
|
|
- applicationContext-transaction.xml
|
|
- db.properties(数据库连接配置)
|
|
- log4j.properties(日志配置)
|
|
总结
- SSM框架目前是比较流行的,有轻量,简洁,快速等特点。
- 在接口的实现类上,一般要用加上@Service注解,这样才能使用@Autowired来注入接口。