欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SSM框架整合核心内容

程序员文章站 2022-03-18 16:37:32
所需要的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 fa ......

所需要的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

SSM框架整合核心内容

准备配置文件:

  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>