SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

1. 前言

最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下。

附上源码:https://gitee.com/niceyoo/jeenotes-ssm

2. 概述

在写代码之前我们先了解一下这三个框架分别是干什么的?

  1. SpringMVC:它用于web层,相当于controller(等价于传统的servlet和struts的action),用来处理用户请求。举个例子,用户在地址栏输入http://网站域名/login,那么springmvc就会拦截到这个请求,并且调用controller层中相应的方法,(中间可能包含验证用户名和密码的业务逻辑,以及查询数据库操作,但这些都不是springmvc的职责),最终把结果返回给用户,并且返回相应的页面(当然也可以只返回json/xml等格式数据)。springmvc就是做前面和后面过程的活,与用户打交道!!

  2. Spring:太强大了,以至于我无法用一个词或一句话来概括它。但与我们平时开发接触最多的估计就是IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用这个类的时候为它初始化,很少看到关键字new。另外spring的aop,事务管理等等都是我们经常用到的。

  3. MyBatis:如果你问我它跟鼎鼎大名的Hibernate有什么区别?我只想说,他更符合我的需求。第一,它能自由控制sql,这会让有数据库经验的人(当然不是说我啦~捂脸~)编写的代码能搞提升数据库访问的效率。第二,它可以使用xml的方式来组织管理我们的sql,因为一般程序出错很多情况下是sql出错,别人接手代码后能快速找到出错地方,甚至可以优化原来写的sql。

3. SSM框架整合配置

3.1 开发环境

IDE: Eclipse Mars2

Jdk: 1.7

数据库: MySQL

注:本例演示采用的开发工具是Eclipse,不要让开发工具限制了你的学习,按照自己的需要来创建就好,用什么工具就按照什么步骤来创建。

3.2 创建Java WEB项目

新建Dynamic Web Project
File->New->Other->Web->Dynamic Web Project

以下是在Package Explorer 视图下的完整目录结构,具体内容看图:

3.3 导入所需jar包

  1. spring(包括springmvc)
  2. mybatis
  3. mybatis-spring整合包
  4. 数据库驱动
  5. 第三方连接池。
  6. Json依赖包Jackson

   jar包最后会提供下载地址。

3.4 整合思路

1、Dao层:
Mybatis的配置文件:mybatis-config.xml
不需要配置任何内容,需要有文件头。文件必须存在。
spring-dao.xml:mybatis整合spring,通过由spring创建数据库连接池,spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包。
2、Service层:
spring-service.xml:所有的service实现类都放到spring容器中管理。并由spring管理事务。
3、表现层:
Springmvc框架,由springmvc管理controller。
Springmvc的三大组件。

3.5 加入配置文件

首先来张图,有图有真相,打马赛克部分暂时用不到,在src下创建resources文件夹... 如下图所示依次创建。

mybatis——mybatis配置

spring——spring+springmvc+spring和mybatis配置

jdbc.properties——数据库配置文件

log4j.properties——log日志

补充:spring包下的文件配置有个知识点需要注意一下,因为spring的配置太多,在这里分为spring-dao、spring-service、spring-mvc(相当于spring-web,本身springmvc就是起到web层的作用)三层,通常我们在别人的项目里看到的就只有spring-context、spring-mvc,其实你可以理解成:sprig-context = spring-dao + spring-service ,我拆分开只是为了更加直观,无论是拆成几个,本身内容是不会变的,只要最后在web.xml把文件配置进去能够实例化即可,无须纠结。

如下xml顺序不分排名,按照上方截图依次提供

1、mybatis-config(mybatis配置)

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5
 6     <!-- 别名 -->
 7     <!-- 起別名后,不用在mappring resultType 填写全类名 -->
 8     <typeAliases>
 9         <package name="com.jeenotes.ssm.pojo"/>
10     </typeAliases>
11
12 </configuration>

View Code

2、spring-dao(持久层,spring和mybatis的结合)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8     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
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10
11     <!-- 配置 读取properties文件 jdbc.properties -->
12     <context:property-placeholder location="classpath:resources/jdbc.properties" />
13
14     <!-- 配置 数据源 -->
15     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16         <property name="driverClassName" value="${jdbc.driver}" />
17         <property name="url" value="${jdbc.url}" />
18         <property name="username" value="${jdbc.username}" />
19         <property name="password" value="${jdbc.password}" />
20     </bean>
21
22     <!-- 配置SqlSessionFactory -->
23     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
24         <!-- 设置MyBatis核心配置文件 -->
25         <property name="configLocation" value="classpath:resources/mybatis/mybatis-config.xml" />
26         <!-- 设置数据源 -->
27         <property name="dataSource" ref="dataSource" />
28         <!-- 它表示我们的Mapper文件存放的位置,当我们的Mapper文件跟对应的Mapper接口处于同一位置的时候可以不用指定该属性的值。 -->
29         <property name="mapperLocations" value="classpath:/mappings/**/*.xml" />
30         <!-- 那么在Mapper文件里面就可以直接写对应的类名 而不用写全路径名了  -->
31         <!-- 跟mybatis中<typeAliases>作用一样 -->
32         <!-- <property name="typeAliasesPackage" value="com.jeenotes.ssm.pojo"/> -->
33     </bean>
34
35     <!-- 配置Mapper扫描 -->
36     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
37         <!-- 设置Mapper扫描包 -->
38         <property name="basePackage" value="com.jeenotes.ssm.dao" />
39     </bean>
40
41 </beans>

View Code

3、spring-service(配置service扫描)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8     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
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10
11     <!-- 配置Service扫描 -->
12     <context:component-scan base-package="com.jeenotes.ssm.service" />
13 </beans>

View Code

4、spring-mvc

 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 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9     <!-- 配置Controller扫描 -->
10     <context:component-scan base-package="com.jeenotes.ssm.controller" />
11
12     <!-- 配置注解驱动 -->
13     <mvc:annotation-driven />
14
15     <!-- 对静态资源放行  -->
16     <mvc:resources location="/css/" mapping="/css/**"/>
17     <mvc:resources location="/js/" mapping="/js/**"/>
18     <mvc:resources location="/fonts/" mapping="/fonts/**"/>
19     <mvc:resources location="/frame/" mapping="/frame/**"/>
20     <mvc:resources location="/images/" mapping="/images/**"/>
21     <mvc:resources location="/style/" mapping="/style/**"/>
22     <!-- 配置视图解析器 -->
23     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
24         <!-- 前缀 -->
25         <property name="prefix" value="/WEB-INF/jsp/" />
26         <!-- 后缀 -->
27         <property name="suffix" value=".jsp" />
28     </bean>
29 </beans>
30     

View Code

5、jdbc.properties

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
3 jdbc.username=root
4 jdbc.password=****

6、log4j.properties

1 # Global logging configuration
2 log4j.rootLogger=DEBUG, stdout
3 # Console output...
4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

3.6 试搭建环境

到这里,其实整个框架的搭建已经基本了,毕竟整点就是上边这堆配置文件嘛,接下来,就是测试一下了。

是时候让你看一下部分马赛克了,参照如下架构可自行创建包

com.jeenotes.ssm:存放control、dao、pojo、service

随便创建一个control,可参考如下:

@Controller
@RequestMapping(value = "user")
public class LoginControl {@RequestMapping("login")public String dologin() {return "index";}}

然后再创建一个index.jsp,jsp文件最好放在WEB-INF目录下,至于为何,自行百度。

在这我就直接放在外面了,其实尽管不去请求url,项目运行同样会进入index.jsp,原因就在于web.xml配置文件中这段配置,

welcome-file-list是一个配置在web.xml中的一个欢迎页,用于当用户在url中输入工程名称或者输入web容器url(如http://localhost:8080/jeenotes-ssm/)时直接跳转的页面.

<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>

程序运行后,在浏览器访问http://localhost:8080/jeenotes-ssm/user/login 同样可以访问到index.jsp证明测试成功。

3.7 SSM框架应用实例

如上只是对web层的一次请求测试,接下来就是对整个ssm环境进行测试了,在这我们通过一个例子继续。

待补充

SSM(Spring+SpringMVC+Mybatis)框架环境搭建(应用实例)(二)

项目地址:https://gitee.com/niceyoo/jeenotes-ssm

本文地址:http://www.cnblogs.com/niceyoo/articles/8729379.html

posted @ 2018-04-06 23:18 niceyoo 阅读(...) 评论(...) 编辑 收藏

SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)相关推荐

  1. Spring+SpringMVC+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  2. Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据、人工智能、机器学习项目开发

    Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户.物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据.人工智能.机器学习项目开发Mus ...

  3. Java语言开发在线美食推荐网 美食推荐系统 基于用户、物品的协同过滤推荐算法实现 SSM(Spring+SpringMVC+Mybatis框架 人工智能、大数据、机器学习项目开发

    Java语言开发在线美食推荐网 美食推荐系统 基于用户.物品的协同过滤推荐算法实现 SSM(Spring+SpringMVC+Mybatis框架 人工智能.大数据.机器学习项目开发FoodRecomm ...

  4. spring boot+mybatis框架环境搭建

    配置spring boot+mybatis框架环境搭建 一, spring boot 环境搭建 以下步骤为 1,新建maven工程 2.在pom文件中添加: spring-boot-starter-p ...

  5. Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)

    依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  6. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

  7. [Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP

    上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现JDBC 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringA ...

  8. Java语言开发在线购物推荐网 购物商城推荐系统 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据、人工智能、机器学习项目开发

    Java语言开发在线购物推荐网 购物商城推荐系统 基于用户.物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据.人工智能.机器学习项目开发ShopRec ...

  9. 使用Java+SSM(Spring+SpringMVC+Mybatis)开发在线美食推荐网 美食推荐系统 美食天下美食爬虫 基于用户、物品的协同过滤推荐算法实现 大数据、人工智能、机器学习项目开发

    使用Java+SSM(Spring+SpringMVC+Mybatis)开发在线美食推荐网 美食推荐系统 美食天下美食爬虫 基于用户.物品的协同过滤推荐算法实现 大数据.人工智能.机器学习项目开发Fo ...

最新文章

  1. 接口里面的方法都是抽象方法吗_正确的敷面膜方法 你真的都掌握了吗
  2. Spark配置高可用(HA)
  3. 在Linux上挂载Windows共享文件夹
  4. Android Studio签名打包的两种方式
  5. linux 查看系统版本 32位 or 64位
  6. [Zhuan]Lua about
  7. 一步步编写操作系统81 att内嵌汇编语法
  8. 值得借鉴:360推荐系统架构演进
  9. Ubuntu赋予普通用户特定目录权限
  10. 对变量移位顺序读写_Java多线程并发读写锁ReadWriteLock实现原理剖析
  11. myeclipse中JSP页面不能更新时候的做法
  12. FL Studio21.0中文版本FL水果娘发布更新
  13. OpenModelica使用入门
  14. Codeforces 450A. Jzzhu and Children
  15. 心形代码来了,Java表白大师
  16. WiFi 5G频段差分巴伦电路对接收灵敏度的影响
  17. win10离线安装.NetFrameWork3.5出现0x800F081F解决办法。
  18. python实训报告怎么写_python实验报告
  19. 5G时代的一个杀手级应用,可能是“云上电脑”?
  20. matlab分布鲁棒优化程序 是学习wasserstein 距离 分布鲁棒的好程序 文章是基于综合能源的分布鲁棒优化

热门文章

  1. [css] span与span之间有看不见的空白间隔是什么原因引起的?有什么解决办法?
  2. [css] padding会影响到元素的大小,那不想让它影响到元素的宽度应该怎么办?
  3. 工作291:当前账号是否绑定操作
  4. 前端学习(2705):重读vue电商网站26之路由导航守卫控制访问权限
  5. 工作160:总结VUE几种页面刷新方法
  6. 前端学习(2057):vue.js的安装方法
  7. 前端学习(1550):$scope和调试工具
  8. 前端学习(494):在XHTML中得用法
  9. shiro学习(10):servelet实现权限认证一
  10. java学习(136):带泛型的类