最容易的ssm三大框架整合(spring+springmvc+mybatis)教程
第一步:開啟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層只寫我們的一些特殊的方法就可以了。
相關文章
- SSM框架——詳細整合教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- SSM框架——具體整合教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- SSM三大框架整合詳細教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- Spring+SpringMVC+Mybatis框架整合搭建教程SpringMVCMyBatis框架
- [Android]後端之路--整合SSM(Spring+SpringMVC+MyBatis)框架(2)Android後端SSMSpringMVCMyBatis框架
- SSM框架的整合SSM框架
- SSM三大框架整合詳細教程SSM框架
- SSM框架整合SSM框架
- 整合SSM框架SSM框架
- Spring+SpringMvc+Mybatis框架整合搭建教程二(依賴配置及框架整合)SpringMVCMyBatis框架
- Spring+SpringMvc+Mybatis框架整合搭建教程三(框架整合測試程式開發)SpringMVCMyBatis框架
- SSM(Spring+SpringMVC+Mybatis)框架搭建詳細教程【附原始碼Git】SSMSpringMVCMyBatis框架原始碼Git
- Spring+SpringMVC+MyBatis框架整合SpringMVCMyBatis框架
- 【從零開始 圖文詳解】IDEA整合SSM框架:Spring+SpringMVC+MybatisIdeaSSM框架SpringMVCMyBatis
- Spring+SpringMvc+Mybatis框架整合搭建教程一(專案建立)SpringMVCMyBatis框架
- SSM框架整合流程SSM框架
- SSM框架整合開發SSM框架
- ssm框架整合筆記SSM框架筆記
- SSM框架快速整合redisSSM框架Redis
- 快速整合搭建SSM框架SSM框架
- 【SSM整合】-Maven管理SSM框架的pom.xml配置SSMMaven框架XML
- SSM框架整合(配置檔案)SSM框架
- 新手搭建SSM(Spring+SpringMVC+MyBatis)入門級教程SSMSpringMVCMyBatis
- 【Java】SSM框架整合 附原始碼JavaSSM框架原始碼
- mybatis逆向工程和SSM框架整合MyBatisSSM框架
- Spring+SpringMvc+Mybatis框架整合搭建教程四(專案部署及測試)SpringMVCMyBatis框架
- Spring+SpringMvc+Mybatis框架整合搭建教程五(專案原始碼釋出到GitHub)SpringMVCMyBatis框架原始碼Github
- SpringMVC(3)- 校驗框架、SSM整合SpringMVC框架SSM
- SSM框架整合——書籍管理系統SSM框架
- SSM:spring+springmvc+mybatis框架中的XML配置檔案功能詳細解釋SSMSpringMVCMyBatis框架XML
- ssm框架整合redis(demo)---通過cache方式SSM框架Redis
- ssm整合SSM
- Spring+SpringMVC+MyBatis的整合SpringMVCMyBatis
- SSM 框架快速整合例項--學生查詢SSM框架
- 史上最詳細的IDEA優雅整合Maven+SSM框架(詳細思路+附帶原始碼)IdeaMavenSSM框架原始碼
- SSM框架SSM框架
- Maven整合SSM框架(maven+spring+springmvc+mybatis)MavenSSM框架SpringMVCMyBatis
- SSM學習筆記3——整合 SpringMVC、整合SSMSSM筆記SpringMVC