SM框架整合篇
SSM整合
目前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语句,学习成本低。
框架搭建步骤
如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。
导包导入Spring+SpringMVC(如果不会选全倒进去就行了)导入mybatis包(如果需要用到日志可将mybatis依赖包导入)导入mybatis-spring-1.3.1.jar(整合必须又这个包)导入c3p0(当然你也可以使用其他连接池)导入数据库驱动配置log4jperties
由于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.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置WEB.xml
1.设置编码过滤器
字符集过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
字符集编码
encoding
UTF-8
encodingFilter
/*
2.添加Spring配置文件位置(等下我们创建spring-context.xml)
contextConfigLocation
classpath:spring-context.xml
org.springframework.web.context.ContextLoaderListener
3.DispatcherServlet配置
dispatcher
org.springframework.web.servlet.DispatcherServlet
springmvc 配置文件
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcher
/
4.添加PUT DELETE支持
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
5.配置Sessin过期时间
15
spring-mvc.xml
xmlns:xsi="w3/2001/XMLSchema-instance"
xmlns:mvc="springframework/schema/mvc"
xmlns:context="springframework/schema/context"
xsi:schemaLocation="springframework/schema/mvc springframework/schema/mvc/spring-mvc-4.2.xsd
springframework/schema/beans springframework/schema/beans/spring-beans.xsd
springframework/schema/context springframework/schema/context/spring-context-4.2.xsd">
spring-context.xml
xmlns:xsi="w3/2001/XMLSchema-instance" xmlns:tx="springframework/schema/tx"
xmlns:context="springframework/schema/context"
xmlns:aop="springframework/schema/aop"
xsi:schemaLocation="springframework/schema/beans springframework/schema/beans/spring-beans.xsd
springframework/schema/context springframework/schema/context/spring-context-4.2.xsd
springframework/schema/aop springframework/schema/aop/spring-aop-4.2.xsd
springframework/schema/tx springframework/schema/tx/spring-tx-4.2.xsd">
expression="org.springframework.stereotype.Controller" />
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
mybaits-config.xml
"mybatis/dtd/mybatis-3-config.dtd">
dbperties(可内置)
mysql.driverClass=com.mysql.jdbc.Driver
mysql.jdbcUrl=jdbc:mysql://localhost/mybatis?characterEncoding=utf8&serverTimezone=UTC
mysql.user=root
mysql.password=root
到这里其实我们的SSM已经整合完成,如果我们需要其他功能可以在加,不要忘记导入包。

浙公网安备 33010602011771号