最容易的ssm三大框架整合(spring+springmvc+mybatis)教程

LucienDon發表於2017-11-21

第一步:開啟eclipse,建立maven project專案,我的專案名為testDemo2,然後替換掉pom.xml檔案,這個檔案就是三大框架的jar包座標位置,有了它就會自動從網路中下載jar包了,請將版本號註釋上面的資訊頭換成你自己的。

<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.baidu.testDemo</groupId>
  <artifactId>testDemo2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
<!-- 集中定義依賴版本號 -->
<properties>
  <junit.version>4.10</junit.version>
  <spring.version>4.1.3.RELEASE</spring.version>
  <mybatis.version>3.2.8</mybatis.version>
  <mybatis.spring.version>1.2.2</mybatis.spring.version>
  <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
  <mysql.version>5.1.32</mysql.version>
  <slf4j.version>1.6.4</slf4j.version>
  <jackson.version>2.4.2</jackson.version>
  <druid.version>1.0.9</druid.version>
  <jolbox.version>0.8.0.RELEASE</jolbox.version>
  <httpclient.version>4.3.5</httpclient.version>
  <jstl.version>1.2</jstl.version>
  <servlet-api.version>2.5</servlet-api.version>
  <jsp-api.version>2.0</jsp-api.version>
  <mapper.version>2.3.4</mapper.version>
  <pagehelper.version>3.7.5</pagehelper.version>
  <jsqlparser.version>0.9.1</jsqlparser.version>
  <joda-time.version>2.5</joda-time.version>
  <commons-lang3.version>3.3.2</commons-lang3.version>
  <commons-io.version>1.3.2</commons-io.version>
  <commons-codec.version>1.9</commons-codec.version>
  <hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
 </properties>
  <dependencies>
   <!-- 單元測試 -->
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
   </dependency>
   <!-- Spring -->
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
   </dependency>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
   </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-aspects</artifactId>
    <version>${spring.version}</version>
   </dependency>
   <!-- Mybatis -->
   <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>
   <!-- 通用Mapper -->
   <dependency>
    <groupId>com.github.abel533</groupId>
    <artifactId>mapper</artifactId>
    <version>${mapper.version}</version>
   </dependency>
   
   <!-- 分頁助手 -->
   <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>${pagehelper.version}</version>
   </dependency>
   <dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>${jsqlparser.version}</version>
   </dependency>
   <!-- MySql -->
   <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.version}</version>
   </dependency>
   <!-- 日誌 -->
   <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>${slf4j.version}</version>
   </dependency>
   <!-- Jackson Json處理工具包 -->
   <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
   </dependency>
   <!-- 連線池 -->
   <dependency>
    <groupId>com.jolbox</groupId>
    <artifactId>bonecp-spring</artifactId>
    <version>${jolbox.version}</version>
   </dependency>
   
   <!-- httpclient -->
   <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>${httpclient.version}</version>
   </dependency>
   <!-- JSP相關 -->
   <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>${jstl.version}</version>
   </dependency>
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>${servlet-api.version}</version>
    <scope>provided</scope>
   </dependency>
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>${jsp-api.version}</version>
    <scope>provided</scope>
   </dependency>
   <!-- 時間操作元件 -->
   <dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>${joda-time.version}</version>
   </dependency>
   <!-- Apache工具元件 -->
   <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>${commons-lang3.version}</version>
   </dependency>
   <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>${commons-io.version}</version>
   </dependency>
   <!-- 加密解密 -->
   <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>${commons-codec.version}</version>
   </dependency>
   <!-- 資料校驗 -->
   <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>${hibernate-validator.version}</version>
   </dependency>
  </dependencies>
</project>

第二步:配置web.xml檔案,用來啟動spring配置和檢視解析器。

<?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"
	id="MyWebApp" version="2.5">
	<display-name>testDemo2</display-name>
	
	<!-- 啟動spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext*.xml</param-value>
	</context-param>
	
	<!-- applicationContext -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 編碼過濾器,以UTF8編碼 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 將POST請求轉化為DELETE或者是PUT 要用_method指定真正的請求方法 -->
	<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>
	
	<!-- 解決PUT請求無法提交Form的問題 -->
	<filter>
		<filter-name>HttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 載入springMVC -->
	<servlet>
		<servlet-name>testDemo2</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/maizhe-manage-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>testDemo2</servlet-name>
		<url-pattern>/*</url-pattern>		
	</servlet-mapping>
/**	<servlet-mapping>   
         <servlet-name>default</servlet-name>   
         <url-pattern>*.css</url-pattern>   
     </servlet-mapping>      
     <servlet-mapping>   
         <servlet-name>default</servlet-name>   
         <url-pattern>*.gif</url-pattern>   
     </servlet-mapping>      
     <servlet-mapping>   
         <servlet-name>default</servlet-name>   
         <url-pattern>*.jpg</url-pattern>   
     </servlet-mapping>   
   	 <servlet-mapping>   
         <servlet-name>default</servlet-name>   
         <url-pattern>*.png</url-pattern>   
     </servlet-mapping>
     <servlet-mapping>   
         <servlet-name>default</servlet-name>   
         <url-pattern>*.js</url-pattern>   
     </servlet-mapping> **/  
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

第三步:配置連線資料庫的properties檔案

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.5.11:3388/maizhe01?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=1a33ee
jdbc.idleConnection=60
jdbc.idleMaxAge=30
jdbc.maxConnectionNum=200
jdbc.minConnectionNum=5

第四步:配置mybatis-config.xml檔案,這個檔案是用來配置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>
	<!-- 引入外部資源配置檔案 -->
	<properties resource="jdbc.properties" />
	<plugins>
		<!-- 分頁助手 -->
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<property name="dialect" value="mysql" />
			<!-- 設定為true時,使用RowBounds分頁會進行count查詢 -->
			<property name="rowBoundsWithCount" value="true" />
		</plugin>

		<!-- 通用mapper外掛 -->
		<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
			<!--主鍵自增回寫方法,預設值MYSQL,詳細說明請看文件 -->
			<property name="IDENTITY" value="mysql" />
			<!--通用Mapper介面,多個通用介面用逗號隔開 -->
			<property name="mappers" value="com.github.abel533.mapper.Mapper" />
		</plugin>
	</plugins>
	<!-- 配置環境,制定資料庫連線資訊 -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
	</environments>

	<mappers>

	</mappers>
</configuration>

第五步:配置spring與mybatis整合檔案,以及dao介面的包掃描,檔名為applicationContext-mybatis.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:p="http://www.springframework.org/schema/p"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置資料來源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- mybatis全域性檔案 -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
		<!-- 掃描mapper對映檔案 -->
		<property name="mapperLocations" value="classpath:com/maizhe/mapper/*.xml"/>		
		<!-- 配置別名包掃描 -->
		<property name="typeAliasesPackage" value="com.maizhe.entity"/>
		
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.maizhe.dao"/>
	</bean>		
</beans>

第六步:配置spring的applicationContext.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:p="http://www.springframework.org/schema/p"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 使用spring自帶的佔位符替換功能 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 允許JVM引數覆蓋 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略沒有找到的資原始檔 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置資原始檔 -->
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
		</bean>
		<context:component-scan base-package="com.maizhe" />
		
		<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<!-- 資料庫驅動 -->
		<property name="driverClass" value="${jdbc.driver}" />
		<!-- 相應驅動的jdbcUrl -->
		<property name="jdbcUrl" value="${jdbc.url}" />
		<!-- 資料庫的使用者名稱 -->
		<property name="username" value="${jdbc.username}" />
		<!-- 資料庫的密碼 -->
		<property name="password" value="${jdbc.password}" />
		<!-- 檢查資料庫連線池中空閒連線的間隔時間,單位是分,預設值:240,如果要取消則設定為0 -->
		<property name="idleConnectionTestPeriod" value="${jdbc.idleConnection}" />
		<!-- 連線池中未使用的連結最大存活時間,單位是分,預設值:60,如果要永遠存活設定為0 -->
		<property name="idleMaxAge" value="${jdbc.idleMaxAge}" />
		<!-- 每個分割槽最大的連線數 -->
		<property name="maxConnectionsPerPartition" value="${jdbc.maxConnectionNum}" />
		<!-- 每個分割槽最小的連線數 -->
		<property name="minConnectionsPerPartition" value="${jdbc.minConnectionNum}" />
	</bean>
</beans>

第七步:配置事務,這裡是用另外一個xml檔案配置的,你也可以直接把配置資訊放在applicationContext.xml檔案裡面,檔名為applicationContext-transaction.xml。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 定義事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 定義事務策略 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!--所有以query開頭的方法都是隻讀的 -->
			<tx:method name="query*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			<!--其他方法使用預設事務策略 -->
			<tx:method name="*" rollback-for="Throwable"/>
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="myPointcut" expression="execution(* com.maizhe..service.*.*(..))" />
		<!--將定義好的事務處理策略應用到上述的切入點 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
	</aop:config>
	
</beans>

第八步:配置檢視解析器,名字為maizhe-manage-servlet.xml,在web.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:p="http://www.springframework.org/schema/p"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 註解驅動 -->
	<mvc:annotation-driven/>

	<!-- 描述conctroller -->
	<context:component-scan base-package="com.maizhe.controller"/>
	
	<!-- 檢視直譯器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 字首 -->
		<property name="prefix" value="/WEB-INF/views/"/>
		<!-- 字尾 -->
		<property name="suffix" value=".jsp"/>
	</bean>
	<!-- 定義圖片直譯器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
		<!-- 設定預設編碼 -->
		<property name="defaultEncoding" value="utf-8"></property>
		<!-- 上傳圖片最大大小5M--> 
		<property name="maxUploadSize" value="5242440"></property>  
	</bean>  
</beans>

經過上面的配置之後,我們再進行測試,測試的時候dao介面繼承Mapper通用介面就可以用它所有的通用方法了,不需要dao層實現類,因為我們已經在xml對映檔案上實現了該方法。dao層只寫我們的一些特殊的方法就可以了。

相關文章