SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方。之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些。以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下。这次,先说说三大框架整合过程。个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助。不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想。实践出真知。(可通过图片水印查看博客地址)


1、基本概念


1.1、Spring


        Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。


1.2、SpringMVC

     

        Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。


1.3、MyBatis


       MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。



2、开发环境搭建



如果需要,参看之前的博文:http://blog.csdn.net/zhshulin/article/details/30779873



3、Maven Web项目创建


如果需要,参看之前的博文:http://blog.csdn.net/zhshulin/article/details/37921705



4、SSM整合

      

        下面主要介绍三大框架的整合,至于环境的搭建以及项目的创建,参看上面的博文。这次整合我分了2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。完整目录结构如下(最后附上源码下载地址,不建议直接使用源码,因为此教程已经有了全部代码):


使用框架都是较新的版本:

       Spring 4.0.2 RELEASE

       Spring MVC 4.0.2 RELEASE

       MyBatis 3.2.6


4.1、Maven引入需要的JAR包

         为了方便后面说的时候不需要引入JAR包,我这里直接给出所有需要的JAR包,这都是基本的JAR包,每个包的是干什么的都有注释,就不再多说了。

pom.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <properties>  
  2.         <!-- spring版本号 -->  
  3.         <spring.version>4.0.2.RELEASE</spring.version>  
  4.         <!-- mybatis版本号 -->  
  5.         <mybatis.version>3.2.6</mybatis.version>  
  6.         <!-- log4j日志文件管理包版本 -->  
  7.         <slf4j.version>1.7.7</slf4j.version>  
  8.         <log4j.version>1.2.17</log4j.version>  
  9.     </properties>  
  10.   
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>junit</groupId>  
  14.             <artifactId>junit</artifactId>  
  15.             <version>4.11</version>  
  16.             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->  
  17.             <scope>test</scope>  
  18.         </dependency>  
  19.         <!-- spring核心包 -->  
  20.         <dependency>  
  21.             <groupId>org.springframework</groupId>  
  22.             <artifactId>spring-core</artifactId>  
  23.             <version>${spring.version}</version>  
  24.         </dependency>  
  25.   
  26.         <dependency>  
  27.             <groupId>org.springframework</groupId>  
  28.             <artifactId>spring-web</artifactId>  
  29.             <version>${spring.version}</version>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-oxm</artifactId>  
  34.             <version>${spring.version}</version>  
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-tx</artifactId>  
  39.             <version>${spring.version}</version>  
  40.         </dependency>  
  41.   
  42.         <dependency>  
  43.             <groupId>org.springframework</groupId>  
  44.             <artifactId>spring-jdbc</artifactId>  
  45.             <version>${spring.version}</version>  
  46.         </dependency>  
  47.   
  48.         <dependency>  
  49.             <groupId>org.springframework</groupId>  
  50.             <artifactId>spring-webmvc</artifactId>  
  51.             <version>${spring.version}</version>  
  52.         </dependency>  
  53.         <dependency>  
  54.             <groupId>org.springframework</groupId>  
  55.             <artifactId>spring-aop</artifactId>  
  56.             <version>${spring.version}</version>  
  57.         </dependency>  
  58.   
  59.         <dependency>  
  60.             <groupId>org.springframework</groupId>  
  61.             <artifactId>spring-context-support</artifactId>  
  62.             <version>${spring.version}</version>  
  63.         </dependency>  
  64.   
  65.         <dependency>  
  66.             <groupId>org.springframework</groupId>  
  67.             <artifactId>spring-test</artifactId>  
  68.             <version>${spring.version}</version>  
  69.         </dependency>  
  70.         <!-- mybatis核心包 -->  
  71.         <dependency>  
  72.             <groupId>org.mybatis</groupId>  
  73.             <artifactId>mybatis</artifactId>  
  74.             <version>${mybatis.version}</version>  
  75.         </dependency>  
  76.         <!-- mybatis/spring包 -->  
  77.         <dependency>  
  78.             <groupId>org.mybatis</groupId>  
  79.             <artifactId>mybatis-spring</artifactId>  
  80.             <version>1.2.2</version>  
  81.         </dependency>  
  82.         <!-- 导入java ee jar 包 -->  
  83.         <dependency>  
  84.             <groupId>javax</groupId>  
  85.             <artifactId>javaee-api</artifactId>  
  86.             <version>7.0</version>  
  87.         </dependency>  
  88.         <!-- 导入Mysql数据库链接jar包 -->  
  89.         <dependency>  
  90.             <groupId>mysql</groupId>  
  91.             <artifactId>mysql-connector-java</artifactId>  
  92.             <version>5.1.30</version>  
  93.         </dependency>  
  94.         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->  
  95.         <dependency>  
  96.             <groupId>commons-dbcp</groupId>  
  97.             <artifactId>commons-dbcp</artifactId>  
  98.             <version>1.2.2</version>  
  99.         </dependency>  
  100.         <!-- JSTL标签类 -->  
  101.         <dependency>  
  102.             <groupId>jstl</groupId>  
  103.             <artifactId>jstl</artifactId>  
  104.             <version>1.2</version>  
  105.         </dependency>  
  106.         <!-- 日志文件管理包 -->  
  107.         <!-- log start -->  
  108.         <dependency>  
  109.             <groupId>log4j</groupId>  
  110.             <artifactId>log4j</artifactId>  
  111.             <version>${log4j.version}</version>  
  112.         </dependency>  
  113.           
  114.           
  115.         <!-- 格式化对象,方便输出日志 -->  
  116.         <dependency>  
  117.             <groupId>com.alibaba</groupId>  
  118.             <artifactId>fastjson</artifactId>  
  119.             <version>1.1.41</version>  
  120.         </dependency>  
  121.   
  122.   
  123.         <dependency>  
  124.             <groupId>org.slf4j</groupId>  
  125.             <artifactId>slf4j-api</artifactId>  
  126.             <version>${slf4j.version}</version>  
  127.         </dependency>  
  128.   
  129.         <dependency>  
  130.             <groupId>org.slf4j</groupId>  
  131.             <artifactId>slf4j-log4j12</artifactId>  
  132.             <version>${slf4j.version}</version>  
  133.         </dependency>  
  134.         <!-- log end -->  
  135.         <!-- 映入JSON -->  
  136.         <dependency>  
  137.             <groupId>org.codehaus.jackson</groupId>  
  138.             <artifactId>jackson-mapper-asl</artifactId>  
  139.             <version>1.9.13</version>  
  140.         </dependency>  
  141.         <!-- 上传组件包 -->  
  142.         <dependency>  
  143.             <groupId>commons-fileupload</groupId>  
  144.             <artifactId>commons-fileupload</artifactId>  
  145.             <version>1.3.1</version>  
  146.         </dependency>  
  147.         <dependency>  
  148.             <groupId>commons-io</groupId>  
  149.             <artifactId>commons-io</artifactId>  
  150.             <version>2.4</version>  
  151.         </dependency>  
  152.         <dependency>  
  153.             <groupId>commons-codec</groupId>  
  154.             <artifactId>commons-codec</artifactId>  
  155.             <version>1.9</version>  
  156.         </dependency>  
  157.           
  158.           
  159.     </dependencies>  

4.2、Spring与MyBatis的整合

所有需要的JAR包都引入以后,首先进行Spring与MyBatis的整合,然后再进行JUnit测试,先看一个项目结构图:


4.2.1、建立JDBC属性文件

jdbc.properties(文件编码修改为utf-8)

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. driver=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://10.221.10.111:8080/db_zsl  
  3. username=demao  
  4. password=demao  
  5. #定义初始连接数  
  6. initialSize=0  
  7. #定义最大连接数  
  8. maxActive=20  
  9. #定义最大空闲  
  10. maxIdle=20  
  11. #定义最小空闲  
  12. minIdle=1  
  13. #定义最长等待时间  
  14. maxWait=60000  

4.2.2、建立spring-mybatis.xml配置文件

        这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。

spring-mybatis.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
  8.                         http://www.springframework.org/schema/context    
  9.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd    
  10.                         http://www.springframework.org/schema/mvc    
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  12.     <!-- 自动扫描 -->  
  13.     <context:component-scan base-package="com.cn.hnust" />  
  14.     <!-- 引入配置文件 -->  
  15.     <bean id="propertyConfigurer"  
  16.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  17.         <property name="location" value="classpath:jdbc.properties" />  
  18.     </bean>  
  19.   
  20.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  21.         destroy-method="close">  
  22.         <property name="driverClassName" value="${driver}" />  
  23.         <property name="url" value="${url}" />  
  24.         <property name="username" value="${username}" />  
  25.         <property name="password" value="${password}" />  
  26.         <!-- 初始化连接大小 -->  
  27.         <property name="initialSize" value="${initialSize}"></property>  
  28.         <!-- 连接池最大数量 -->  
  29.         <property name="maxActive" value="${maxActive}">

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/575566.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java基础知识 多线程

package org.base.practise9; import org.junit.Test; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * Created with IntelliJ IDEA. * User: cutter.li * Date: 14-3-11 * Time: 上午9:40 * 多线程基础知识练习 */ public class Pract…

最新版的SSM框架spring5.0搭建教程(附源码)

<p>用SSM框架已经有一段时间了&#xff0c;但都没有完整地搭建过一次工程。前段时间比较有时间就自己试着搭建了一下&#xff0c;差不多都是用的最新的spring版本了&#xff0c;然后就在这个基础上做了很多的实验测试。最近想着还是记录一下整个搭建的过程&#xff0c;以…

node.js 针对不同的请求路径(url) 做出不同的响应

边看这个边写的: http://wenku.baidu.com/link?urlC4yLe-TVH6060u_x4t34H3Ze8tjoL7HjJaKgH-TvHnEYl-T_gAMYwhmrCeM0Ji59WBPSkoEXPTWk8dPIZVpbFg_by_gN6DJNGYfjlFuYxE_ 上篇文章讲到了浏览器中访问 http://127.0.0.1:8888/ 输出 "hello world", 但是实际当中, 用户访…

MyBatis 为什么需要通用 Mapper ?

版权声明&#xff1a;版权归博主所有&#xff0c;转载请带上本文链接&#xff01;联系方式&#xff1a;abel533gmail.com https://blog.csdn.net/isea533/article/details/83045335 </div>在早期项目文档中有过类似主题的内容…

Oracle教程-安装、结构(一)

本文安装的是Oracle中的11G版本一、 将文件win32_11gR2_database_1of2.zip和win32_11gR2_database_2of2.zip解压。注意&#xff1a;这两个文件解压到同一个目录下&#xff0c;即&#xff1a;将Components目录合并到一起二、 双击“setup.exe”&#xff0c;弹出以下安装向导。去…

SpringBoot视频教程

SpringBoot视频教程 百度云 置顶 2018年08月02日 11:56:26 SoXiaTea 阅读数 8811 SpringBoot视频教程 百度云 史上最全最精辟的SpringBoot视频教程 B站视频地址 https://www.bilibili.com/video/av33985898 百度云保存地址 全网最实用1.5版本SpringBoot教程 链接&#xf…

[041] 微信公众帐号开发教程第17篇-应用实例之智能翻译

内容概要 本篇文章为大家演示怎样在微信公众帐号上实现“智能翻译”&#xff0c;本例中翻译功能是通过调用“百度翻译API”实现的。智能翻译是指用户随意输入想要翻译的内容&#xff08;单词或句子&#xff09;&#xff0c;系统能自己主动识别用户採用的语言&#xff0c;并将其…

读书,上学,上名校!!!!!

摘自读者上的一篇文章 “龟兔赛跑&#xff0c;如果兔子一直在跑&#xff0c;会发生什么....” 原文作者&#xff1a;王凤 一 念高中时&#xff0c;常听班主任提起一个学姐。她几乎不跟周围的人说话&#xff0c;也没什么朋友&#xff0c;直到高考&#xff0c;她考进全省前10名…

思维模式

人生是可以设计的&#xff0c;生涯是可以规划的&#xff0c;幸福是可以准备的。现在就可以开始。在你穷的时候&#xff0c;要少在家里&#xff0c;多在外面。在你富有的时候&#xff0c;要多在家里&#xff0c;少在外面。这就是生活的艺术。穷得时候&#xff0c;钱要花给别人&a…

钉钉上手体会:阿里实用主义的野望

钉钉出自阿里之手&#xff0c;而阿里是电商出身&#xff0c;在移动办公和协同办公方面不算老司机&#xff0c;但钉钉却凭借阿里的背书声称拿下了这个市场的最大份额&#xff0c;甚至超过后面9名的总和&#xff08;数据来源为钉钉官网发布的《2018中国智能移动办公行业趋势报告》…

WAF与IPS的区别总结

谁是最佳选择&#xff1f; Web应用防护无疑是一个热门话题。由于技术的发展成熟和人们对便利性的期望越来越高&#xff0c;Web应用成为主流的业务系统载体。在Web上“安家”的关键业务系统中蕴藏的数据价值引起攻击者的青睐&#xff0c;网上流传的Web漏洞挖掘和攻击工具让攻击的…

企业微信:腾讯的“佛系”办公江湖

、 在协同办公领域&#xff0c;近几年来移动办公伴随着智能手机蓬勃发展起来&#xff0c;特别是腾讯和阿里的入场&#xff0c;改变了业内中小微市场群雄逐鹿的大混战态势&#xff0c;开启了楚汉争霸的局面。相比于钉钉强势的攻城略地&#xff0c;企业微信的“淡定”让人感觉很佛…

for语句中声明变量

在C语言中,局部变量应该在函数的可执行语句之前定义,但在C中变量可在任何语句位置定义,只要允许程序语句的地方,都允许定义变量。 在C99标准中C同C一样允许在for循环语句中定义变量。并且这个变量作用域被限定在for循环中,在for循环外就成为了未定义变量&#xff08;C也是&…

Eclipse安装STS插件

Eclipse安装STS插件 1、下载STS插件 地址&#xff1a;https://spring.io/tools/sts/all/ 最上面是已经安装好STS插件的Eclipse软件&#xff0c;可以点击上图红框中的“previous Spring Tool Suite™ versions”&#xff0c;查看其它版本的Eclipse。下面依次为更新文件、更新站…

转载:CSS hack技巧大全

原文地址&#xff1a;http://www.duitang.com/static/csshack.html part2 —— CSS hack技巧大全 ——作者&#xff1a;吴雷君兼容范围&#xff1a;IE:6.0&#xff0c;FireFox:2.0&#xff0c;Opera 10.0&#xff0c;Sarari 3.0&#xff0c;Chrome参考资料&#xff1a; 各游览器…

Eclipse安装STS插件并解决安装缓慢问题

原 Eclipse安装STS插件并解决安装缓慢问题 2018年11月20日 10:22:29 MyronCham 阅读数 1198 标签&#xff1a; sts 更多 个人分类&#xff1a; 开发工具 Eclipse安装springsource Tool Suite&#xff08;STS)插件&#xff1a;

TextBox控件中只输入整数的几种方法

方法一. if(e.KeyChar!8&&!Char.IsDigit(e.KeyChar)&&e.KeyChar!.) { e.Handled true; } 方法二: if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar ! 8) &&e.KeyChar!.) { e.Handled true; } 方法三:if (!Cha…

InvalidCharacterError: Failed to execute 'setAttribute' on 'Element': ')' is not a valid

问题描述&#xff1a; 在webStorm开发angular应用时候 或者vue等&#xff0c;页面没内容&#xff0c;浏览器控制台报错&#xff0c;报诸如题目类似的错误&#xff0c;无误定位性。没找到问题。 查错过程&#xff1a; 1、检查程序有误明显异常 2、检查依赖是否正常 3、检查对应…

Sencha touch API

Sencha touch API http://docs.sencha.com/touch/2.3.1/#!/guide/getting_started 转载于:https://www.cnblogs.com/wuyida/p/6300382.html

eclispe Springboot项目修改html,jsp 页面不能及时刷新

Springboot静态文件不更新的解决办法,以及Springboot实现热部署 正确答案是把菜单 Project > Build Automatically 。&#xff08;之前不知道怎么手瞎把这个给去了&#xff09;。否则再怎么设置缓存&#xff0c;devtools都是白瞎。