首页 > 代码库 > Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
依赖导入以及框架整合
(1).打开项目的pom.xml文件,声明依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hafiz.www</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ssm Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.2.6.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis-spring.version>1.2.2</mybatis-spring.version> <servlet.version>3.0.1</servlet.version> <druid.version>1.0.13</druid.version> <jsp-api.version>2.1</jsp-api.version> <jstl.version>1.2</jstl.version> <mysql.connector.version>5.1.38</mysql.connector.version> <aspectj.version>1.8.6</aspectj.version> <commons.fileupload.version>1.3.1</commons.fileupload.version> <jackson.version>2.5.4</jackson.version> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.7</slf4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons.fileupload.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> <build> <finalName>ssm</finalName> </build></project>
(2).在项目的resources文件夹下新建jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.20.196:3306/dev?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=123456jdbc.maxActive=30jdbc.minIdle=5
(3).在项目的resources文件夹下新建log4j.properties文件
### set log levels ###
log4j.rootLogger = ERROR , stdout , D , E , F
### 输出到控制台 ###
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 = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}] %m%n
## 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern = ‘.‘yyyyMMdd
log4j.appender.D.File = logs/wap/warn.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = WARN
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.encoding=UTF-8
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}] %m%n
## 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = logs/wap/error.log
log4j.appender.E.DatePattern = ‘.‘yyyyMMdd
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.encoding=UTF-8
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}] %m%n
## 保存异常信息到单独文件 ###
log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
log4j.appender.F.File = logs/wap/debug.log
log4j.appender.F.DatePattern = ‘.‘yyyyMMdd
log4j.appender.F.Append = true
log4j.appender.F.Threshold = DEBUG
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.encoding=UTF-8
log4j.appender.F.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}] %m%n
(4).在项目的resources文件夹下新建mapper、spring、mybatis三个文件夹分别用来存放mybatis的mapper xml文件以及spring的配置文件以及mybatis的配置文件。
(5).在项目的resources文件夹下的mybatis文件夹下新建mybatis.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> <!-- 全局配置setting,根据需要进行配置 --> <!--<settings> <setting name="" value=""/> </settings>--> <!-- 设置别名 --> <typeAliases> <!-- 批量进行设置别名 --> <package name="com.hafiz.www.po"/> </typeAliases> <!-- 使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers --> <!-- <mappers> <package name="com.hafiz.www.mapper" /> </mappers> --></configuration>
(6).在项目的resources文件夹下的spring文件夹下新建以下xml文件并进行整合配置
创建spring的配置集中文件spring.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-transaction.xml"/></beans>
创建spring的dao层配置文件spring-dao.xml并进行以下配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 加载配置文件jdbc.properties中的内容,jdbc.properties中key的命名要有一定的特殊规则 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源,数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="http://www.mamicode.com/${jdbc.driver}"/> <property name="url" value="http://www.mamicode.com/${jdbc.url}"/> <property name="username" value="http://www.mamicode.com/${jdbc.username}"/> <property name="password" value="http://www.mamicode.com/${jdbc.password}"/> <property name="maxActive" value="http://www.mamicode.com/${jdbc.maxActive}"/> <property name="minIdle" value="http://www.mamicode.com/${jdbc.minIdle}" /> </bean> <!-- 配置sqlSessionFactory交由spring进行管理 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 设置数据源引用 --> <property name="dataSource" ref="dataSource"></property> <!-- 加载mybatis的全局配置文件路径 --> <property name="configLocation" value="http://www.mamicode.com/classpath:mybatis/mybatis-config.xml"></property> <property name="mapperLocations" value="http://www.mamicode.com/classpath:mapper/*.xml"/> </bean> <!-- 配置mapper接口扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 要扫描的包的名称,有多个包时,包名之间用半角逗号隔开 --> <property name="basePackage" value="http://www.mamicode.com/com.hafiz.www.mapper"></property> <property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sqlSessionFactory"></property> </bean> </beans>
创建spring的service层配置文件spring-service.xml并进行以下配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!--配置组件扫描,只扫描Controller注解之外的组件--> <context:component-scan base-package="com.hafiz.www"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan></beans>
创建spring的transaction事务相关配置文件spring-transaction.xml并进行以下配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 事务管理器 对mybatis操作数据库进行事务控制,spring使用jdbc的事务控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- dataSource在applicationContext-dao.xml中进行了配置 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 定义通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="create*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS"/> <tx:method name="get*" propagation="SUPPORTS"/> <tx:method name="select*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <!-- 定义aop切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hafiz.www.service.impl.*.*(..))"/> </aop:config></beans>
创建springMvc的配置文件spring-mvc.xml并进行以下配置
<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/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 配置视图解析器 解析jsp视图,默认使用jstl标签,classpath下得有jstl包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp文件的前缀和后缀 --> <property name="prefix" value="http://www.mamicode.com/WEB-INF/jsp/"/> <property name="suffix" value="http://www.mamicode.com/.jsp"/> </bean> <!--配置注解控制器映射器--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>--> <!--配置注解控制器映射器--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>--> <!-- 使用mvc:annotation-driven代替上边的 注解控制器适配器 和 注解控制器映射器 配置, mvc:annotation-driven默认加载了很多参数绑定方法,比如: json转换器就默认加载了,如果使用mvc:annotation-driven就不用再进行上面RequestMappingHandlerAdapter和RequestMappingHandlerMapping的配置 实际开发过程中往往使用mvc:annotation-driven --> <!-- 扫描注解驱动,如:@Required, @Autowired, @PostConstruct, @PersistenceContext, @ Resource, @ PreDestroy.... --> <!--<context:annotation-config />--> <!-- 扫描mvc注解驱动,如:@RequestMapping, @Controller,@RequestBody,@ResponseBody.... --> <mvc:annotation-driven /> <!-- 配置组件扫描,springmvc容器中只扫描Controller注解 --> <context:component-scan base-package="com.hafiz.www"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置全局异常处理器 只要实现了HandlerExceptionResolver接口就是全局异常处理器类,每个系统只能有一个全局异常处理器类 --> <!--<bean class="com.hafiz.www.exception.CustomExceptionResolver"/>--> <!-- 配置文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为5MB --> <property name="maxUploadSize"> <value>5242880</value> </property> </bean> <!-- 配置静态资源文件的解析,包括js、css、img.... --> <mvc:resources location="/js/" mapping="/js/**"/><!-- /**表示拦截文件夹下的所有文件,包括子路径 --> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/img/" mapping="/img/**"/></beans>
(6).在web.xml文件中配置spring listener 和springmv的DispatcherServlet以及log4j listener以及配置文件路径和POST 乱码Filter
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>pegasus-scm</display-name> <!-- param --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring.xml</param-value> </context-param> <!-- 配置log4j配置文件路径 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 60s 检测日志配置 文件变化 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!-- EncodingFilter --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- 配置Log4j监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- spring mvc --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc config file</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
(7).在webapp下面创建js、css、img三个文件夹分别用来存放网站的脚本文件、样式文件以及图片文件
至此,Spring、SpringMvc、Mybatis的整合完毕!
Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)