关闭

SSM整合(SpringMVC+Spring+Mybatis)

标签: mybatisspringspringMVC
46人阅读 评论(0) 收藏 举报
分类:

1.加入jar

aopalliance-1.0.jar

aspectjweaver.jar

c3p0-0.9.1.2.jar

commons-fileupload-1.3.1.jar

commons-io-2.4.jar

commons-logging-1.2.jar

jackson-annotations-2.2.1.jar

jackson-core-2.2.1.jar

jackson-databind-2.2.1.jar

log4j-1.2.17.jar

mybatis-3.2.6.jar

mybatis-spring-1.2.2.jar

mysql-connector-java-5.1.30.jar

spring-aop-4.2.2.RELEASE.jar

spring-aspects-4.2.2.RELEASE.jar

spring-beans-4.2.2.RELEASE.jar

spring-context-4.2.2.RELEASE.jar

spring-context-support-4.2.2.RELEASE.jar

spring-core-4.2.2.RELEASE.jar

spring-expression-4.2.2.RELEASE.jar

spring-instrument-4.2.2.RELEASE.jar

spring-instrument-tomcat-4.2.2.RELEASE.jar

spring-jdbc-4.2.2.RELEASE.jar

spring-jms-4.2.2.RELEASE.jar

spring-messaging-4.2.2.RELEASE.jar

spring-orm-4.2.2.RELEASE.jar

spring-oxm-4.2.2.RELEASE.jar

spring-test-4.2.2.RELEASE.jar

spring-tx-4.2.2.RELEASE.jar

spring-web-4.2.2.RELEASE.jar

spring-webmvc-4.2.2.RELEASE.jar

spring-webmvc-portlet-4.2.2.RELEASE.jar

spring-websocket-4.2.2.RELEASE.jar

 

2.配置web.xml

这里主要配置了: 编码过滤器,spring配置监听器,springMVCdispatcherServlet的配置,log4j

log4j.properties

 

 ### set log levels ### 

log4j.rootLogger = debug,stdout,D,E 

 

log4j.appender.stdout = org.apache.log4j.ConsoleAppender 

log4j.appender.stdout.Target = System.out 

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout 

log4j.appender.stdout.layout.ConversionPattern = [%-5p]%d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n 

 

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender 

log4j.appender.D.File = H://logs/log.log 

log4j.appender.D.Append = true 

log4j.appender.D.Threshold = DEBUG  

log4j.appender.D.layout = org.apache.log4j.PatternLayout 

log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-ddHH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n 

 

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender 

log4j.appender.E.File =H://logs/error.log  

log4j.appender.E.Append = true 

log4j.appender.E.Threshold = ERROR  

log4j.appender.E.layout = org.apache.log4j.PatternLayout 

log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-ddHH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

 

 

--------------------------------------------------------------

<!-- filter -->

 <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>

  </filter>

  <filter-mapping>

         <filter-name>CharacterEncodingFilter</filter-name>

         <url-pattern>/*</url-pattern>

  </filter-mapping>

 

 <!-- spring -->

  <!-- needed forContextLoaderListener -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

 

<!-- Bootstraps the root web application context before servletinitialization -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

  <!-- springMVC -->

  <!-- The frontcontroller of this Spring Web application, responsible for handling allapplication requests -->

<servlet>

<servlet-name>springDispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:mvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

 

<!-- Map all requests to the DispatcherServlet for handling-->

<servlet-mapping>

<servlet-name>springDispatcherServlet</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

<!-- 配置log4j -->

<!-- 设置根目录 --> 

<context-param>   

      <param-name>webAppRootKey</param-name>   

      <param-value>webapp.root</param-value>   

 </context-param>   

 

<context-param> 

<param-name>log4jConfigLocation</param-name> 

<param-value>classpath:log4j.properties</param-value> 

</context-param> 

<!-- 3000表示开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 --> 

<context-param>   

       <param-name>log4jRefreshInterval</param-name>   

       <param-value>3000</param-value>   

</context-param>  

<listener> 

   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 

</listener>

 

 

3.配置spring

思路:1.先配数据源datasource

             2.配置工厂 sqlSessionFactory

 3.配置事务管理器 dataSourceTransactionManager

 4.配置事务属性通知 txAdvice

 5.配置事务切入点 pointcut

 6.配置注解支持

 

例:

db.properties

 

driver=com.mysql.jdbc.Driver

url=jdbc:mysql:///test

name=root

password=wxniit

 

----------------------------------------------------------------------------------------------------

<!-- 读取属性文件 -->

<!--<context:property-placeholderlocation="classpath:db.properties"/>  -->

<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<propertyname="location">

          <value>classpath:db.properties</value>

</property>

</bean>

 

<!-- 配置datasource -->

<beanid="datasource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<propertyname="driverClassName"value="${driver}"></property>

<propertyname="url" value="${url}"></property>

<propertyname="username" value="${name}"></property>

<propertyname="password" value="${password}"></property>

</bean>

 

<!-- 配置工厂 -->

<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<propertyname="dataSource" ref="datasource"></property>

<propertyname="configLocation"value="classpath:mybatis-cfg.xml"></property>

</bean>

 

<!-- 配置声明式事务  -->

<!-- 事务管理器 -->

<beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<propertyname="dataSource" ref="datasource"></property>

</bean>

<!-- 事务声明通知 -->

<tx:adviceid="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:methodname="save" propagation="REQUIRED"/>

<tx:methodname="get" read-only="true"/>

<tx:methodname="*" propagation="REQUIRED"/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut expression="execution(* com.chenj.app.serivce.impl.*.*(..))"id="pointcut"/>

<aop:advisoradvice-ref="txAdvice" pointcut-ref="pointcut"/>

</aop:config>

 

<!-- 扫描controller -->

<context:component-scanbase-package="com.chenj.app"></context:component-scan>

 

4.配置springMVC

这里主要配置了 json ,文件上传,视图解析,注解支持,拦截器(需要实现org.springframework.web.servlet.HandlerInterceptor

例:

<!-- 启动Springmvc注解驱动 -->

<mvc:annotation-driven/>

<!-- 返回json 方法一需要导入 fastjson.jar-->

<!--<mvc:annotation-driven> <mvc:message-convertersregister-defaults="false">

避免IE执行AJAX,返回JSON出现下载文件 <bean id="fastJsonHttpMessageConverter"class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">

<propertyname="supportedMediaTypes"> <list> 这里顺序不能反,一定先写text/html,不然ie下出现下载提示

<value>text/html;charset=UTF-8</value><value>application/json;charset=UTF-8</value>

</list></property> </bean> </mvc:message-converters></mvc:annotation-driven> -->

 

 

<!-- 返回json 方法二需要导入jackson-annotations.jar,jackson-core.jar,jackson-databind.jar -->

<bean

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<bean

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

<propertyname="messageConverters">

<list>

<bean

class="org.springframework.http.converter.StringHttpMessageConverter">

<propertyname="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

<value>application/json;charset=UTF-8</value>

</list>

</property>

</bean>

<bean

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

<propertyname="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

<value>application/json;charset=UTF-8</value>

</list>

</property>

</bean>

</list>

</property>

</bean>

 

<!-- 拦截器配置 -->

<!--<mvc:interceptors> <mvc:interceptor> <mvc:mappingpath="/**"/> <bean

class="com.chenj.app.interceptor.LoginInterceptor"><property name="allowedPass">

<list><value>login.do</value> </list> </property></bean> </mvc:interceptor>

</mvc:interceptors>-->

 

<!-- 文件上传配置 -->

<beanid="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<propertyname="defaultEncoding" value="utf-8"></property>

<propertyname="maxUploadSize"value="10485760000"></property>

<propertyname="maxInMemorySize" value="40960"></property>

</bean>

 

<!-- 配置视图解析器 -->

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--<property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property>-->

<propertyname="prefix" value="/WEB-INF/pages/" />

<propertyname="suffix" value=".jsp" />

</bean>

 

<!-- 配置注解支持 -->

<context:component-scanbase-package="com.chenj.app"></context:component-scan>

 

5.配置mybatis

注:需要在spring配置工厂时加上<property name="configLocation"value="classpath:mybatis-cfg.xml"></property>

 

例:

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEconfiguration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<typeAliases>

   <packagename="com.chenj.app.entity"/>

</typeAliases>

<!-- 配置mapper的位置-->

<mappers>

<mapperresource="com/chenj/app/entity/userMapper.xml"/>

</mappers>

</configuration>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:492次
    • 积分:81
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档