框架的版本: Spring4.2.1 + SpringMVC4.2.1 + MyBatis3.3.1
整合 SSM 主要分为以下几个步骤:
注:Spring 和 MyBatis 都依赖 commons-logging,因为 Spring 已经导入了,MyBatis 就不需要重复导入了。
注:如果是 junit 4.7 版本的话就不需要 hamcrest。
ps:jstl 实在忘记了是哪两个就全部加进来把…
<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"
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">
<context:component-scan base-package="com.qjl.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}">property>
<property name="jdbcUrl" value="${jdbc.url}">property>
<property name="user" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="txManager" />
<aop:aspectj-autoproxy />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="classpath:mybatis-config.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qjl.ssm.*.mapper">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
beans>
<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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.qjl.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true">setting>
<setting name="aggressiveLazyLoading" value="false">setting>
settings>
<typeAliases>
<package name="com.qjl.ssm.sysmanage.entity"/>
typeAliases>
<mappers>
<package name="com.qjl.ssm.sysmanage.mapper"/>
mappers>
configuration>
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123
log4j.rootLogger=DEBUG ,INFO, CONSOLE, FILE
## console 配置文件输出的目的地 (控制台)
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy/MM/dd/HH:mm:ss} %-5p [%t] %10l - %m%n
## file 配置文件输出的目的地 (写入日志文件)
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=D:/logs/log4j.log
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.Append = true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy/MM/dd/HH:mm:ss} %-5p [%t] %10l - %m%n
##第一个参数代表日志的级别 日志级别有五个 DEBUG INFO WARN ERROR FATAL
##常用的日志基本有4个 DEBUG INFO WARN ERROR
##DEBUG 我们为程序设定的一些调试信息
##INFO 为一般 要显示的信息 ,比如登陆,参数的值
##WARN 一般为警告信息 ,比如说session丢失,文件路径不存在
##ERROR 一般为异常信息 用于异常打印
##
##第二个和第三个参数代表日志信息的输出地点 输出地点分五个类型
##1.org.apache.log4j.ConsoleAppender(控制台)
##2.org.apache.log4j.FileAppender(文件)
##3.org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
##4.org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
##5.org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方--邮箱)
## layout表示日志信息的输出格式风格
## 1.org.apache.log4j.HTMLLayout(以HTML表格形式布局),
## 2.org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
## 3.org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
## 4.org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
##%d: 输出日志时间点的日期或时间,比如:%d{yyy MMM dd HH:mm:ss},输出类似:2011年10月18日 22:10:28
##%p: 输出日志信息优先级,即DEBUG,INFO,WARN,ERROR,FATAL,
##%t: 输出产生该日志事件的线程名
##%c: 输出日志信息所属的类目,通常就是所在类的全名
##%l: 输出代码中的行号
##%m: 输出代码中指定的消息,产生的日志具体信息
##%n: 输出一个回车换行符
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>interview_ssmdisplay-name>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
记录一些问题:
问题 1:为什么 Spring 的配置文件要和 SpringMVC 的配置文件分开配置?
Spring 的配置文件一般包括:数据源的配置,事务控制,跟其他框架的整合,注解驱动(service类和dao类的注册以及依赖关系)
SpringMVC 的配置文件一般是包括 Controller 层的注册以及涉及到 SpringMVC 的一些相关配置(映射器,适配器,类型转换器,异常的配置,国际化…)
特别注意:在进行注解扫描时,要在Spring配置文件中要剔除Controller的扫描,在SpringMVC的配置文件中只能包含Controller的扫描。
特别特别注意:SpringMVC 自容器可以依赖父容器,例:Controller 依赖 Service,反之不可以。
开发一个功能时,首先明确:表结构设计好没有,需不需要我来设计?
如果是前后端混合开发,建议从dao层开发,提供通用的dao层接口
service层的接口主要是面向控制层的调用,在service层里面进行业务逻辑处理,调用dao层接口。
controller层主要是面向页面的url请求。
关注我的微信公众号,观看更多精彩内容: