SSM框架整合篇
程序员文章站
2022-03-10 14:42:20
[TOC] SSM整合 Author:SimpleWu github(已上传SSMrest风格简单增删该查实例):https://gitlab.com/450255266/code/ 目前Spring+SpringMVC+Mybatis是一套非常流行的配套开发框架。 1. spring核心ioc、a ......
目录
ssm整合
author:simplewu
github(已上传ssmrest风格简单增删该查实例):https://gitlab.com/450255266/code/
目前spring+springmvc+mybatis是一套非常流行的配套开发框架。
- spring核心ioc、aop技术,ioc解耦,使得代码复用,可维护性大幅度提升,aop提供切面编程,同样的增强了生产力。提供了对其他优秀开源框架的集成支持
- spring mvc是对比struts2等mvc框架来说的,不说struts2爆出的那么多安全漏洞,而且是类拦截,所有action变量共享,同时是filter入口的,而spring mvc是方法拦截,controller独享request response数据,采用的serlvet入口,与spring无缝对接。开发而言,spring mvc更加轻量和低入门。
- mybatis轻量级半自动化框架,sql由开发者编写可对语句进行调优,并且mybatis使用xml方式java代码与sql可以解耦并且支持动态sql语句,学习成本低。
框架搭建步骤
导包
- 导入spring+springmvc(如果不会选全倒进去就行了)
- 导入mybatis包(如果需要用到日志可将mybatis依赖包导入)
- 导入mybatis-spring-1.3.1.jar(整合必须又这个包)
- 导入c3p0(当然你也可以使用其他连接池)
- 导入数据库驱动
配置log4j.properties
由于mybatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置 log4j.rootlogger=debug,console #设置控制台相关的参数 log4j.appender.console=org.apache.log4j.consoleappender log4j.appender.console.layout=org.apache.log4j.patternlayout log4j.appender.console.layout.conversionpattern=%d [%t] %-5p [%c] - %m%n #设置mybatis的输出内容 log4j.logger.java.sql.resultset=info log4j.logger.org.apache=info log4j.logger.java.sql.connection=debug log4j.logger.java.sql.statement=debug log4j.logger.java.sql.preparedstatement=debug
配置web.xml
1.设置编码过滤器
<filter> <description>字符集过滤器</description> <filter-name>encodingfilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.添加spring配置文件位置(等下我们创建spring-context.xml)
<!-- 配置加载spring-context文件 --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring-context.xml</param-value> </context-param> <!--添加spring的监听器--> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener>
3.dispatcherservlet配置
<!-- sprigmvc配置 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <description>springmvc 配置文件</description> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
4.添加put delete支持
<!-- 添加put delete支持 --> <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>
5.配置sessin过期时间
<!-- 配置session超时时间,单位分钟 --> <session-config> <session-timeout>15</session-timeout> </session-config>
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 --> <context:component-scan base-package="com.simple.ssm.controller"> <!-- 只扫描@controller与@controlleradvice修饰的类 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.controlleradvice"/> </context:component-scan> <!-- 加入静态资源与动态资源支持 --> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/views/" /> <property name="suffix" value=".jsp"></property> </bean> </beans>
spring-context.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!--引入外部数据库连接信息文件--> <context:property-placeholder location="classpath:db.properties" /> <!-- 扫描所有除@controller ,@controlleradvice修饰的bean --> <context:component-scan base-package="com.simple.ssm"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.controlleradvice" /> </context:component-scan> <!-- 配置c3p0连接池 --> <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"> <property name="driverclass" value="${mysql.driverclass}" /> <!-- jdbc:mysql://localhost/mybatis?characterencoding=utf8&servertimezone=utc --> <property name="jdbcurl" value="${mysql.jdbcurl}" /> <!-- 连接用户名 --> <property name="user" value="${mysql.user}" /> <property name="password" value="${mysql.password}" /> <!-- 连接密码 --> <!-- 队列中的最小连接数 --> <property name="minpoolsize" value="15" /> <!-- 队列中的最大连接数 --> <property name="maxpoolsize" value="25" /> <!-- 当连接耗尽时创建的连接数 --> <property name="acquireincrement" value="15" /> <!-- 等待时间 --> <property name="checkouttimeout" value="10000" /> <!-- 初始化连接数 --> <property name="initialpoolsize" value="20" /> <!-- 最大空闲时间,超出时间连接将被丢弃 --> <property name="maxidletime" value="20" /> <!-- 每隔60秒检测空闲连接 --> <property name="idleconnectiontestperiod" value="60000" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <!-- 拦截器方式配置事物 --> <!-- <tx:advice id="txadvice" transaction-manager="transactionmanager"> <tx:attributes> <tx:method name="add*" propagation="required" /> <tx:method name="insert*" propagation="required" /> <tx:method name="save*" propagation="required" /> <tx:method name="update*" propagation="required" /> <tx:method name="delete*" propagation="required" /> <tx:method name="remove*" propagation="required" /> <tx:method name="find*" propagation="supports" /> <tx:method name="load*" propagation="supports" /> <tx:method name="search*" propagation="supports" /> <tx:method name="*" propagation="supports" /> </tx:attributes> </tx:advice> --> <!-- 配置切面 --> <!-- <aop:config> 事务入口(service的包路径) <aop:pointcut id="transactionpointcut" expression="execution(* com.simple.ssm.service.*.*(..))" /> 将事务通知与切入点组合 <aop:advisor pointcut-ref="transactionpointcut" advice-ref="txadvice" /> </aop:config> --> <!-- 使用注解来控制事务 --> <tx:annotation-driven transaction-manager="transactionmanager" /> <!-- 配置mybatis, 绑定c3p0 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <!-- 配置mybatis配置文件所在位置 --> <property name="configlocation" value="classpath:mybatis-config.xml" /> <!-- 配置实体类xml映射所在位置 --> <property name="mapperlocations" value="classpath:com/simple/ssm/dao/mapper/*.xml" /> </bean> <!-- 扫描生成所有dao层 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <!-- 指定持久化接口包位置 --> <property name="basepackage" value="com.simple.ssm.dao"></property> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> </bean> </beans>
mybaits-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="mapunderscoretocamelcase" value="true"/> <!-- 开启二级缓存 --> <setting name="cacheenabled" value="true"/> <!-- 开启懒加载 --> <setting name="lazyloadingenabled" value="true"/> </settings> <typealiases> <package name="com.simple.ssm.entitys"/> </typealiases> </configuration>
db.properties(可内置)
mysql.driverclass=com.mysql.jdbc.driver mysql.jdbcurl=jdbc:mysql://localhost/mybatis?characterencoding=utf8&servertimezone=utc mysql.user=root mysql.password=root
到这里其实我们的ssm已经整合完成,如果我们需要其他功能可以在加,不要忘记导入包。