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

1. 前言

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

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

2. 概述

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

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

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

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

3. SSM框架整合配置

3.1 开发环境

IDE Eclipse Mars2 spring

Jdk: 1.7sql

数据库: MySQL数据库

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

 

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 整合思路

一、Dao层:
Mybatis的配置文件:mybatis-config.xml
不须要配置任何内容,须要有文件头。文件必须存在。
spring-dao.xml:mybatis整合spring,经过由spring建立数据库链接池,spring管理SqlSessionFactory、mapper代理对象。须要mybatis和spring的整合包。
二、Service层:
spring-service.xml:全部的service实现类都放到spring容器中管理。并由spring管理事务。
三、表现层:
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顺序不分排名,按照上方截图依次提供

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

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

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

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

五、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=****

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

 

我建立了一个java相关的公众号,用来记录本身的学习之路,感兴趣的小伙伴能够关注微信公众号:niceyoo