首页 文章资讯内容详情

Spring+SpringMVC+MyBatis企业级应用

2026-06-01 4 花语

本文内容纲要:

-古老的SSM企业级应用 -框架搭建步骤

目录

古老的SSM企业级应用

框架搭建步骤

导包 配置log4j.properties 配置WEB.xml spring-mvc.xml spring-context.xml mybaits-config.xml db.properties(可内置)

古老的SSM企业级应用

Author:SimpleWu

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

spring核心ioc、aop技术,ioc解耦,使得代码复用,可维护性大幅度提升,aop提供切面编程,同样的增强了生产力。提供了对其他优秀开源框架的集成支持 springmvc是对比struts2等mvc框架来说的,不说struts2爆出的那么多安全漏洞,而且是类拦截,所有Action变量共享,同时是filter入口的,而springmvc是方法拦截,controller独享requestresponse数据,采用的serlvet入口,与spring无缝对接。开发而言,springmvc更加轻量和低入门。 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.添加PUTDELETE支持

<!--添加PUTDELETE支持--> <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 <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!--让扫描spring扫描这个包下所有的类,让标注spring注解的类生效--> <context:component-scanbase-package="com.simple.ssm.controller"> <!--只扫描@Controller与@ControllerAdvice修饰的类--> <context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/> <context:include-filtertype="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!--加入静态资源与动态资源支持--> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <!--视图解析器--> <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"> <propertyname="prefix"value="/WEB-INF/views/"/> <propertyname="suffix"value=".jsp"></property> </bean> </beans> spring-context.xml <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!--引入外部数据库连接信息文件--> <context:property-placeholderlocation="classpath:db.properties"/> <!--扫描所有除@Controller,@ControllerAdvice修饰的bean--> <context:component-scanbase-package="com.simple.ssm"> <context:exclude-filtertype="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filtertype="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!--配置c3p0连接池--> <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"> <propertyname="driverClass"value="${mysql.driverClass}"/> <!--jdbc:mysql://localhost/mybatis?characterEncoding=utf8&serverTimezone=UTC--> <propertyname="jdbcUrl"value="${mysql.jdbcUrl}"/> <!--连接用户名--> <propertyname="user"value="${mysql.user}"/> <propertyname="password"value="${mysql.password}"/> <!--连接密码--> <!--队列中的最小连接数--> <propertyname="minPoolSize"value="15"/> <!--队列中的最大连接数--> <propertyname="maxPoolSize"value="25"/> <!--当连接耗尽时创建的连接数--> <propertyname="acquireIncrement"value="15"/> <!--等待时间--> <propertyname="checkoutTimeout"value="10000"/> <!--初始化连接数--> <propertyname="initialPoolSize"value="20"/> <!--最大空闲时间,超出时间连接将被丢弃--> <propertyname="maxIdleTime"value="20"/> <!--每隔60秒检测空闲连接--> <propertyname="idleConnectionTestPeriod"value="60000"/> </bean> <!--配置事务管理器--> <beanid="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <propertyname="dataSource"ref="dataSource"/> </bean> <!--拦截器方式配置事物--> <!-- <tx:adviceid="txAdvice"transaction-manager="transactionManager"> <tx:attributes> <tx:methodname="add*"propagation="REQUIRED"/> <tx:methodname="insert*"propagation="REQUIRED"/> <tx:methodname="save*"propagation="REQUIRED"/> <tx:methodname="update*"propagation="REQUIRED"/> <tx:methodname="delete*"propagation="REQUIRED"/> <tx:methodname="remove*"propagation="REQUIRED"/> <tx:methodname="find*"propagation="SUPPORTS"/> <tx:methodname="load*"propagation="SUPPORTS"/> <tx:methodname="search*"propagation="SUPPORTS"/> <tx:methodname="*"propagation="SUPPORTS"/> </tx:attributes> </tx:advice> --> <!--配置切面--> <!--<aop:config>事务入口(Service的包路径)<aop:pointcutid="transactionPointcut" expression="execution(*com.simple.ssm.service.*.*(..))"/>将事务通知与切入点组合<aop:advisor pointcut-ref="transactionPointcut"advice-ref="txAdvice"/></aop:config>--> <!--使用注解来控制事务--> <tx:annotation-driventransaction-manager="transactionManager"/> <!--配置mybatis,绑定c3p0--> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <!--配置mybatis配置文件所在位置--> <propertyname="configLocation"value="classpath:mybatis-config.xml"/> <!--配置实体类XML映射所在位置--> <propertyname="mapperLocations"value="classpath:com/simple/ssm/dao/mapper/*.xml"/> </bean> <!--扫描生成所有dao层--> <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定持久化接口包位置--> <propertyname="basePackage"value="com.simple.ssm.dao"></property> <propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"></property> </bean> </beans> mybaits-config.xml <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--开启驼峰式命名规则--> <settingname="mapUnderscoreToCamelCase"value="true"/> <!--开启二级缓存--> <settingname="cacheEnabled"value="true"/> <!--开启懒加载--> <settingname="lazyLoadingEnabled"value="true"/> </settings> <typeAliases> <packagename="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已经整合完成,如果我们需要其他功能可以在加,不要忘记导入包。

本文内容总结:古老的SSM企业级应用,框架搭建步骤,

原文链接:https://www.cnblogs.com/SimpleWu/p/9792466.html