本文简述了 SSM 框架技术基本的搭建与整合
何谓SSM?
Spring+SpringMVC+MyBatis
使用环境My Eclipse 2015、MySQL数据库
创建一个Java web project
Name : CSDN-SSMBuild
创建一个Source Folder
Name:cfg
用来存一些所需要的配置文件 例如database.properties、mybatis.xml 等
在src目录下创建一些基础使用的包
com.xxx.entity
com.xxx.controllers
com.xxx.services
com.xxx.dao
补建一个包 gen(generator) 存入逆向生成工程时的XML配置文件
搭建Spring环境
右键工程-->MyEclipse-->Projecs facets-->Install Spring facets
NEXT-->配置三个对勾且路径选择为刚刚特意为了安置配置文件所创建的cfg资源文件夹-->NEXT-->END
导入jar包
基本jar包共29个
spring : core/beans/context/expression
aop : aop/aspects/aopalliance/aspectweaver
springMVC : web/webmvc
spring事务处理 : tx/jdbc/orm
spring-mybatis : spring-mybatis(for spring and mybatis)
日志相关 : cglib/log4j/commons-logging
逆向生成 : generator-core
mybatis主包 : mybatis
c3p0 : c3p0/mchange-commons-java
mybatis : ant/ant-launcher/asm/javassist/ognl/slf4j-api/slf4j-log4j
mysql : mysql-connector-java
准备配置文件 放入cfg目录下
applicationContext.xml 这个是在搭建spring环境时自动生成的
db.properties 数据库的四大参数
gen.xml 逆向工程
log4j.xml 日志打印
mybatis.xml mybatis工程搭建
springmvc.xml springmvc下的搭建
首先配置web.xml
解决三大问题:
1 DispatcherServlet
2 解决全站乱码问题(有些特殊情况特殊对待)
3 解决支持CRUD的四种操作
DispatcherServlet的作用(摘自网络)
DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:
1、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
3、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
4、通过ViewResolver解析逻辑视图名到具体视图实现;
5、本地化解析;
6、渲染具体的视图等;
7、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。
从以上我们可以看出DispatcherServlet主要负责流程的控制
摘自Spring Reference
参数 | 描述 |
contextClass | 实现WebApplicationContext接口的类,当前的servlet用它来创建上下文。如果这个参数没有指定, 默认使用XmlWebApplicationContext。 |
contextConfigLocation | 传给上下文实例(由contextClass指定)的字符串,用来指定上下文的位置。这个字符串可以被分成多个字符串(使用逗号作为分隔符) 来支持多个上下文(在多上下文的情况下,如果同一个bean被定义两次,后面一个优先)。 |
namespace | WebApplicationContext命名空间。默认值是[server-name]-servlet。 |
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CSDN-SSMBuild</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 服务器启动后被application自动创建 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置springmvc.xml的路径 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 表示启动该容器时 初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
<!-- 此处dispatcher标签不设置Default value为REQUEST -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 支持CRUD操作的get/post/put/delete四种请求方式(本文使用四种还有更多) -->
<!-- 其中PUT和DELETE是POST派生出来的两种请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>
配置db.properties
jdbc.user=root
jdbc.password=123
jdbc.jdbcUrl=jdbc\:mysql\:///csdn
jdbc.driverClass=com.mysql.jdbc.Driver
配置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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫描IOC -->
<context:component-scan base-package="com.csdn">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 开启Spring注解的支持 -->
<context:annotation-config></context:annotation-config>
<!-- 开启Spring事务处理注解的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 导入标准配置文件的值到spring中 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置C3P0数据源
在导入了标准配置文件中的值到spring中后
使用${jdbc.xxx} 可以在修改数据库配置数据的时候不修改主配置文件
-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
</bean>
<!-- 配置spring事务管理器 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置增强 -->
<tx:advice id="advice1" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切点和切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.csdn.service.*.*(..))" id="cut1"/>
<aop:advisor advice-ref="advice1" pointcut-ref="cut1"/>
</aop:config>
<!-- IOC SQLsessionfactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 关联Mybatis.xml 主配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"></property>
<!-- 实现mapper实现类的接口 -->
<property name="mapperLocations" value="classpath*:com/csdn/dao/*.xml"></property>
<!-- 相对应的实现了mapper接口 对应的实现类自动的注入饿到接口层 -->
<mybatis-spring:scan base-package="com.csdn.dao"/>
</bean>
</beans>
配置gen.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 4个参数 con -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///class2" userId="root" password="123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定 POJO 所在包名 和路径 -->
<javaModelGenerator targetPackage="com.csdn.entity"
targetProject="./src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定 映射文件 所在的 包名 和路径 -->
<sqlMapGenerator targetPackage="com.csdn.dao"
targetProject="./cfg">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定 Mapper接口 包名 和路径 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.csdn.dao" targetProject="./src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 表名 与 POJO 映射 -->
<table tableName="download" domainObjectName="Download" />
</context>
</generatorConfiguration>
配置log4j.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
mybatis.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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>