SSM框架整合配置詳解(spring,spring mvc,mybatis)
當今SSM框架已經成為了一種主流,其中spring,spring mvc和mybatis框架的功能很強大,給我們程式設計師節省了很多力氣,可以說這三種框架簡直就是我們程式設計師的福音,但是我們都知道,框架在自身帶來便捷的同時,也存在很多的配置檔案,更別說當三個框架整合的時候那就更加的困難了,所以我將這三個框架的環境搭建做了一下整理,希望可以給初學者一點引導.
搭建一個SSM專案主要分為以下幾個步驟:
1.建立一個web專案
2.匯入jar包(maven和手動)
其中用到的主要jar包有:(注意,因為版本之間存在不相容情況,所以對於初學者強烈建議使用一下版本)
spring4.37(context,mvc,jdbc,tx),jsckson2.81,mybatis3.4.5,
mytatis-spring1.3.1,druid1.0.14,mysql-connection5.0.8
commons-fileupload1.3.2+Apache poi3.13
3.建立java類
com.tarena.dao 介面
com.tarena.service 業務的介面
com.tarena.service.impl 業務介面的實現
com.tarena.controller springmvc的controller類
com.tarena.entity 例項類,序列化
com.tarena.vo 值物件,序列化
com.tarena.util 工具
4.配置檔案
a.專案入口web.xml
b.spring.xml 配置spring容器
c.spring_mvc.xml 配置springmvc容器
d.spring_mybatis.xml 配置mybatis
5.跑專案
6.新增專案靜態資源(html,css,js,image,video等)
7.跑專案
8.邊寫程式碼,邊除錯
下面就將每一步的程式碼貼上
-
1.建立一個web專案
我建立的maven專案,注意是war專案 -
2.匯入jar包(maven和手動)
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lxk</groupId> <artifactId>tes_ssm</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.7.RELEASE</spring.version> </properties> <dependencies> <!-- spring context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring jdbc 依賴包,為了新增宣告式事務 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring tx依賴包,為了新增宣告式事務 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- spring mvc的輔助包,json轉換工具 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <!-- mybatis 的依賴包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mybatis整合spring的依賴包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- mysql驅動的依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> <!-- 阿里巴巴的資料庫連線池的依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.14</version> </dependency> <!-- 檔案上傳的依賴包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <!-- 微軟的office文件操作 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</version> </dependency> <!-- log4j 日誌的依賴包 --> <!-- <dependency> --> <!-- <groupId>log4j</groupId> --> <!-- <artifactId>log4j</artifactId> --> <!-- <version>1.2.16</version> --> <!-- </dependency> --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> </project>
-
3.建立java類
com.tarena.dao 介面
com.tarena.service 業務的介面
com.tarena.service.impl 業務介面的實現
com.tarena.controller springmvc的controller類
com.tarena.entity 例項類,序列化
com.tarena.vo 值物件,序列化
com.tarena.util 工具 -
4.配置檔案(最重要的一點)
a.專案入口web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>tes_ssm</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 全域性初始化資料,spring的監聽器讀取此配置檔案 多個配置檔案用分號分隔 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:conf/spring.xml; classpath:conf/spring_mybatis.xml </param-value> </context-param> <!-- spring容器初始化的監聽器,會讀取全域性初始化的資料(xml檔案) --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring處理中文亂碼問題 --> <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> <!-- spring mvc的入口 載入spring mvc 前端控制器 restful --> <!-- restful模式,必須注意在spring_mvc.xml中配置,刨除靜態資源 --> <servlet> <servlet-name>dispatcher_restful</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring_mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher_restful</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
b.spring.xml 配置spring容器
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 載入屬性檔案 此種方式載入屬性檔案是給spring的配置檔案使用的 -->
<context:property-placeholder
location="classpath:conf/mysql.properties"/>
<!-- 載入屬性檔案,用於在屬性檔案內通過@Value註解注入java物件中 -->
<util:properties id="manyProperties"
location="classpath:conf/page.properties"></util:properties>
<!-- 掃描service包,例項化帶有@Service註解 -->
<context:component-scan base-package="com.lxk.service"></context:component-scan>
<!-- 掃描util包,例項化帶有@Component註解 -->
<context:component-scan base-package="com.lxk.util"></context:component-scan>
</beans>
c.spring_mvc.xml 配置springmvc容器
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 在restful模式下,新增靜態資源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/" mapping="/**"></mvc:resources>
<!-- 掃描spring的元件 -->
<context:component-scan base-package="com.lxk.controller"></context:component-scan>
<!-- 掃描 spring mvc的註解 @RequestMapping @ResponseBody -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- spring mvc 檔案上傳 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--能配置多少個property,可以查文件和查詢原始碼 -->
<!--最大上傳檔案的大小 -->
<property name="maxUploadSize" value="8388608"></property>
<property name="resolveLazily" value="true"></property>
</bean>
</beans>
d.spring_mybatis.xml 配置mybatis
這是spring-mybatis的清單檔案
因為spring沒有整合mybatis,所以mybatis自己向spring靠攏
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 資料庫連線池 commons-dbcp ,c3p0,proxool,阿里巴巴druid -->
<bean id="alibabaDataSource"
class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"
destroy-method="close">
<!-- 資料庫連線的4項 -->
<property name="driverClassName">
<value>${jdbc_driverClass}</value>
</property>
<property name="url">
<value>${jdbc_url}</value>
</property>
<property name="username">
<value>${jdbc_userName}</value>
</property>
<property name="password">
<value>${jdbc_userPassword}</value>
</property>
<!-- 連線池中的最大連線數 -->
<property name="maxActive">
<value>5</value>
</property>
<!-- 初始化的連線數 -->
<property name="initialSize">
<value>2</value>
</property>
<!-- 獲取連線的最大等待時間 -->
<property name="maxWait">
<value>6000</value>
</property>
<!-- 連線池的最大空閒 -->
<property name="maxIdle">
<value>2</value>
</property>
<!-- 連線池的最小空閒 -->
<property name="minIdle">
<value>2</value>
</property>
<!-- 自動清除無用的連線 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 自動清除無用的連線的等待時間 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 連線屬性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
<!-- 例項化MyBatis的SqlSessionFactoryBean物件-->
<!--mybatis配置,讀取配置檔案(掃描配置檔案)-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="alibabaDataSource"
p:configLocation="classpath:conf/configuration.xml"
p:mapperLocations="classpath:mapper/*.xml">
</bean>
<!-- 掃描所有XXXMapper的物件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.lxk.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory">
</bean>
<!-- spring 事務管理開始 -->
<!-- Spring jdbc 的事務管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="alibabaDataSource"/>
</bean>
<!-- 掃描事務有關的註解@Transactional -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- Spring事務管理結束 -->
</beans>
mybatis的總清單檔案
<?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>
<typeAliases>
</typeAliases>
</configuration>
這樣整個專案就算基本搭建完成了
搭建完之後的樣子是下面這樣的:
其中mapper裡面兩個xml是mybatis的對映檔案.還有conf中的mysql.properties和page.properties是屬性檔案,這個根據自己的實際情況去配置,我就不贅述了.
然後就可一啟動Tomcat了,如果出現下面的資訊就表明你配置成功了,
到這就萬事大吉了!