1. 程式人生 > >SSM框架整合核心內容

SSM框架整合核心內容

所需要的jar包及其版本

  Spring 版本:4.3.18   tx、aop、beans、core、web、web-mvc、context、expression、jdbc 

  MyBatis:3.4.6 

  mybatis-spring:1.3.2 

  mysql-connector 連線資料庫:5.1.44 

  fastjson 生成json:1.2.13 

  druid 資料來源:1.1.9 

  jsr 303 資料校驗:hibernate-validate4.3.2、jboss-loggin3.1.0CR2、validation-api1.0GA 

  面向切面:aopaliance、aspectjweaver1.8.13 

  檔案上傳:commons-io、commons-fileupload、commons-loggin1.2 

  jsp、jstl:jstl.jar、standart.jar 

  log4j:1.2.17

準備配置檔案:

  database.properties  連線資料庫

  log4j.properties     輸出日誌

  mybatis-config.xml   mybatis配置檔案

  applicationContext-mybatis.xml   spring配置資料操作的配置檔案

  springmvc-servlet.xml  springmvc的配置檔案

  web.xml  專案配置檔案

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><settings><!--日誌實現--><setting name="logImpl" value="log4j"/><!--
非懶載入--><setting name="lazyLoadingEnabled" value="false"/></settings><!--實體類別名--><typeAliases><package name="cn.smbms.pojo"/></typeAliases> </configuration>

applicationContext-mybatis.xml 

<!--引入database.properties-->
 <bean  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     <property name="location">         <value>classpath:database.properties</value></property>
 </bean> 
<!--資料來源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">     <property name="driver" value="${jdbc.driver}"/>     <property name="url" value="${jdbc.url}"/>     <property name="username" value="${jdbc.user}"/>     <property name="password" value="${jdbc.pwd}"/>
 </bean> 
<!--sqlSessionFactoryBean--> 
<bean class="org.mybatis.spring.SqlSessionFactoryBean"  id="sqlSessionFactoryBean">     <property name="dataSource" ref="dataSource"/><!--mybatis-config.xml檔案-->     <property name="configLocation" value="classpath:mybatisconfig.xml"/>     <!--mapper檔案路徑--><property name="mapperLocations"><list><!--模糊匹配-->             <value>classpath:cn/smbms/dao/**/*.xml</value></list></property>
 </bean>

<!--批量注入對映器實現 MapperScannerConfigurer--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">     <property name="basePackage" value="cn.smbms.dao"/>
 </bean> 

<!--事務管理器--> 
<bean  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"       id="txManager">
    <property name="dataSource" ref="dataSource"/>
 </bean> 

<!--啟用宣告式事務註解--> 
<tx:annotation-driven transaction-manager="txManager"/>

<!--啟用aop的增強註解--> 
<aop:aspectj-autoproxy/>

springmvc-servlet.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:aop="http://www.springframework.org/schema/aop"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:mvc="http://www.springframework.org/schema/mvc"        xmlns:p="http://www.springframework.org/schema/p"        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.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">     <!--支援註解配置Bean-->     <context:component-scan base-package="cn.smbms.controller"/><!--檢視對應-->     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="prefix" value="/WEB-INF/jsp/"/>         <property name="suffix" value=".jsp"/>     </bean>
<!--靜態資源-->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!--spring mvc 註解支援 和訊息轉換器-->
<mvc:annotation-driven>
<mvc:message-converters>
<!--spring 訊息轉換器 防止中文亂碼-->
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
<!--fastJson 訊息轉換器 格式化日期-->
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
<property name="features">
<list>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--檔案上傳-->
<bean
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="2000000"/>
</bean>
<!--攔截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--攔截的路徑-->
<mvc:mapping path="/sys/**"/>
<bean class="cn.smbms.interceptor.SysInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--springmvc過濾器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--載入springmvc配置檔案-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--springmvc 請求編碼過濾器-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listenerclass>
</listener>
<!--上下文引數,指定spring配置檔案所在目錄-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
</web-app>