经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
SSM框架整合篇
来源:cnblogs  作者:Miya。  时间:2018/10/16 9:39:57  对本文有异议

目录

SSM整合

Author:SimpleWu

github(已上传SSMrest风格简单增删该查实例):https://gitlab.com/450255266/code/

目前Spring+SpringMVC+Mybatis是一套非常流行的配套开发框架。

  1. spring核心ioc、aop技术,ioc解耦,使得代码复用,可维护性大幅度提升,aop提供切面编程,同样的增强了生产力。提供了对其他优秀开源框架的集成支持
  2. spring mvc是对比struts2等mvc框架来说的,不说struts2爆出的那么多安全漏洞,而且是类拦截,所有Action变量共享,同时是filter入口的,而spring mvc是方法拦截,controller独享request response数据,采用的serlvet入口,与spring无缝对接。开发而言,spring mvc更加轻量和低入门。
  3. mybatis轻量级半自动化框架,sql由开发者编写可对语句进行调优,并且mybatis使用XML方式JAVA代码与SQL可以解耦并且支持动态SQL语句,学习成本低。

框架搭建步骤

导包

  1. 导入Spring+SpringMVC(如果不会选全倒进去就行了)
  2. 导入mybatis包(如果需要用到日志可将mybatis依赖包导入)
  3. 导入mybatis-spring-1.3.1.jar(整合必须又这个包)
  4. 导入c3p0(当然你也可以使用其他连接池)
  5. 导入数据库驱动

配置log4j.properties

由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。

  1. #设置输出级别和输出位置
  2. log4j.rootLogger=debug,Console
  3. #设置控制台相关的参数
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Console.layout=org.apache.log4j.PatternLayout
  6. log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
  7. #设置MyBatis的输出内容
  8. log4j.logger.java.sql.ResultSet=INFO
  9. log4j.logger.org.apache=INFO
  10. log4j.logger.java.sql.Connection=DEBUG
  11. log4j.logger.java.sql.Statement=DEBUG
  12. log4j.logger.java.sql.PreparedStatement=DEBUG

配置WEB.xml

1.设置编码过滤器

  1. <filter>
  2. <description>字符集过滤器</description>
  3. <filter-name>encodingFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  5. <init-param>
  6. <description>字符集编码</description>
  7. <param-name>encoding</param-name>
  8. <param-value>UTF-8</param-value>
  9. </init-param>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>encodingFilter</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>

2.添加Spring配置文件位置(等下我们创建spring-context.xml)

  1. <!-- 配置加载Spring-context文件 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:spring-context.xml</param-value>
  5. </context-param>
  6. <!--添加Spring的监听器-->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>

3.DispatcherServlet配置

  1. <!-- SprigMVC配置 -->
  2. <servlet>
  3. <servlet-name>dispatcher</servlet-name>
  4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5. <init-param>
  6. <description>springmvc 配置文件</description>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:spring-mvc.xml</param-value>
  9. </init-param>
  10. <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>dispatcher</servlet-name>
  14. <url-pattern>/</url-pattern>
  15. </servlet-mapping>

4.添加PUT DELETE支持

  1. <!-- 添加PUT DELETE支持 -->
  2. <filter>
  3. <filter-name>HiddenHttpMethodFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>HiddenHttpMethodFilter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

5.配置Sessin过期时间

  1. <!-- 配置session超时时间,单位分钟 -->
  2. <session-config>
  3. <session-timeout>15</session-timeout>
  4. </session-config>

spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  9. <!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
  10. <context:component-scan base-package="com.simple.ssm.controller">
  11. <!-- 只扫描@Controller与@ControllerAdvice修饰的类 -->
  12. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  13. <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
  14. </context:component-scan>
  15. <!-- 加入静态资源与动态资源支持 -->
  16. <mvc:default-servlet-handler/>
  17. <mvc:annotation-driven/>
  18. <!-- 视图解析器 -->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value="/WEB-INF/views/" />
  21. <property name="suffix" value=".jsp"></property>
  22. </bean>
  23. </beans>

spring-context.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
  10. <!--引入外部数据库连接信息文件-->
  11. <context:property-placeholder location="classpath:db.properties" />
  12. <!-- 扫描所有除@Controller ,@ControllerAdvice修饰的bean -->
  13. <context:component-scan base-package="com.simple.ssm">
  14. <context:exclude-filter type="annotation"
  15. expression="org.springframework.stereotype.Controller" />
  16. <context:exclude-filter type="annotation"
  17. expression="org.springframework.web.bind.annotation.ControllerAdvice" />
  18. </context:component-scan>
  19. <!-- 配置c3p0连接池 -->
  20. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  21. <property name="driverClass" value="${mysql.driverClass}" />
  22. <!-- jdbc:mysql://localhost/mybatis?characterEncoding=utf8&amp;serverTimezone=UTC -->
  23. <property name="jdbcUrl" value="${mysql.jdbcUrl}" />
  24. <!-- 连接用户名 -->
  25. <property name="user" value="${mysql.user}" />
  26. <property name="password" value="${mysql.password}" />
  27. <!-- 连接密码 -->
  28. <!-- 队列中的最小连接数 -->
  29. <property name="minPoolSize" value="15" />
  30. <!-- 队列中的最大连接数 -->
  31. <property name="maxPoolSize" value="25" />
  32. <!-- 当连接耗尽时创建的连接数 -->
  33. <property name="acquireIncrement" value="15" />
  34. <!-- 等待时间 -->
  35. <property name="checkoutTimeout" value="10000" />
  36. <!-- 初始化连接数 -->
  37. <property name="initialPoolSize" value="20" />
  38. <!-- 最大空闲时间,超出时间连接将被丢弃 -->
  39. <property name="maxIdleTime" value="20" />
  40. <!-- 每隔60秒检测空闲连接 -->
  41. <property name="idleConnectionTestPeriod" value="60000" />
  42. </bean>
  43. <!-- 配置事务管理器 -->
  44. <bean id="transactionManager"
  45. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  46. <property name="dataSource" ref="dataSource" />
  47. </bean>
  48. <!-- 拦截器方式配置事物 -->
  49. <!--
  50. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  51. <tx:attributes>
  52. <tx:method name="add*" propagation="REQUIRED" />
  53. <tx:method name="insert*" propagation="REQUIRED" />
  54. <tx:method name="save*" propagation="REQUIRED" />
  55. <tx:method name="update*" propagation="REQUIRED" />
  56. <tx:method name="delete*" propagation="REQUIRED" />
  57. <tx:method name="remove*" propagation="REQUIRED" />
  58. <tx:method name="find*" propagation="SUPPORTS" />
  59. <tx:method name="load*" propagation="SUPPORTS" />
  60. <tx:method name="search*" propagation="SUPPORTS" />
  61. <tx:method name="*" propagation="SUPPORTS" />
  62. </tx:attributes>
  63. </tx:advice>
  64. -->
  65. <!-- 配置切面 -->
  66. <!-- <aop:config> 事务入口(Service的包路径) <aop:pointcut id="transactionPointcut"
  67. expression="execution(* com.simple.ssm.service.*.*(..))" /> 将事务通知与切入点组合 <aop:advisor
  68. pointcut-ref="transactionPointcut" advice-ref="txAdvice" /> </aop:config> -->
  69. <!-- 使用注解来控制事务 -->
  70. <tx:annotation-driven transaction-manager="transactionManager" />
  71. <!-- 配置mybatis, 绑定c3p0 -->
  72. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  73. <property name="dataSource" ref="dataSource" />
  74. <!-- 配置mybatis配置文件所在位置 -->
  75. <property name="configLocation" value="classpath:mybatis-config.xml" />
  76. <!-- 配置实体类XML映射所在位置 -->
  77. <property name="mapperLocations" value="classpath:com/simple/ssm/dao/mapper/*.xml" />
  78. </bean>
  79. <!-- 扫描生成所有dao层 -->
  80. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  81. <!-- 指定持久化接口包位置 -->
  82. <property name="basePackage" value="com.simple.ssm.dao"></property>
  83. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  84. </bean>
  85. </beans>

mybaits-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <settings>
  6. <!-- 开启驼峰式命名规则 -->
  7. <setting name="mapUnderscoreToCamelCase" value="true"/>
  8. <!-- 开启二级缓存 -->
  9. <setting name="cacheEnabled" value="true"/>
  10. <!-- 开启懒加载 -->
  11. <setting name="lazyLoadingEnabled" value="true"/>
  12. </settings>
  13. <typeAliases>
  14. <package name="com.simple.ssm.entitys"/>
  15. </typeAliases>
  16. </configuration>

db.properties(可内置)

  1. mysql.driverClass=com.mysql.jdbc.Driver
  2. mysql.jdbcUrl=jdbc:mysql://localhost/mybatis?characterEncoding=utf8&serverTimezone=UTC
  3. mysql.user=root
  4. mysql.password=root

到这里其实我们的SSM已经整合完成,如果我们需要其他功能可以在加,不要忘记导入包。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号