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

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 阅读(...) 评论(...) 编辑 收藏

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

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

相关文章

Spring事务管理详解_基本原理_事务管理方式

Spring事务管理详解_基本原理_事务管理方式 1. 事务的基本原理 Spring事务的本质其实就是数据库对事务的支持&#xff0c;使用JDBC的事务管理机制,就是利用java.sql.Connection对象完成对事务的提交&#xff0c;那在没有Spring帮我们管理事务之前&#xff0c;我们要怎么做。 C…

ORA-28000: the account is locked

首先使用具有sysdba权限的账户登陆&#xff0c;如sys账户和system账户 新建一个sql窗体&#xff0c;并执行语句解锁被锁定的账户&#xff0c;如我这里sgyw账户&#xff1a; alter user sgyw account unlock; 执行成功后再次用这个账户就可以登陆系统了。

工作227:小程序学习1开始布局页面

<template><view class"box u-p-l-35 u-p-r-35 "><view class"title"><text>请选择您要管理的市场</text></view><view :class" [u-flex,u-row-center,{choose_market: selectIndex 0},{market : selectIndex…

windows下安装和使用scrapy

首先&#xff0c;要确保已经正确安装了python环境&#xff0c;并安装了pip包 接着&#xff0c;打开cmd或者powershell &#xff0c;输入命令 pip install scrapy 。安装完之后 运行scrapy性能测试命令&#xff1a; scrapy bench 。此时可能出现“import win32api&#xff0c;Im…

spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

1. 声明式事务管理分类 声明式事务管理也有两种常用的方式&#xff0c; 一种是基于tx和aop名字空间的xml配置文件&#xff0c;另一种就是基于Transactional注解。 显然基于注解的方式更简单易用&#xff0c;更清爽。 2. spring事务特性 spring所有的事务管理策略类都继承自org.…

oracle中如何设置主键并且让其自动增长

1、创建数据库 create table USERINFO ( USERNO NUMBER not null, USERNAME NVARCHAR2(20), USERPWD NVARCHAR2(20) ) 2、创建触发器 create sequence SEQ_Userinf start with 1 increment by 1 nomaxvalue nominvalue nocache; CREATE OR REPLACE TRIGGER tg_test BEFORE …

工作228:小程序学习2开始布局页面2

<template><view class"box u-p-l-35 u-p-r-35 "><view class"title"><text>请选择您要管理的市场</text></view><view :class" [u-flex,u-row-center,{choose_market: selectIndex 0},{market : selectIndex…

AngularJs通过路由传参解决多个页面资源浪费问题

在实际开发中会遇到很多类似模块界面大体都一致只是极少的细节部分不一样&#xff0c;这时不管是在html页面还有js及数据交互的时候我们就没必要因为这些不同的页面分出不同的文件&#xff0c;这样很浪费内存及效率&#xff0c;于是我在开发中想到一种办法&#xff1a;通过ng路…

mybatis BindingException: Invalid bound statement (not found)

错误截图 解决措施 此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的。 通常我们在配置SqlSessionFactory时会有如配置 1 <!-- 配置SqlSessionFactory -->2 <bean class"org.mybatis.spring.SqlSessionFactoryBean">…

myeclipse快捷生成代码块

1、快捷键啊 altshifts 2、右键 -——Source

前端学习(2712):重读vue电商网站32之让菜单栏展开与折叠

通过点击一个按钮&#xff0c;让侧边栏进行展开与折叠。通过 isCollapse 的值来动态变化侧边栏的宽度。 其中 cursor: pointer 设置是为了让鼠标放在折叠与展开处会有一个手指指向。 letter-spacing: 0.2em 是为了让 ||| 有一定间隔

Myeclipse下Maven的配置

1.配置    window--Preferences JDK的配置(执行命令&#xff0c;控制台可能无法输出) -Dmaven.multiModuleProjectDirectory$MAVEN_HOME 新建 Maven Project 转载于:https://www.cnblogs.com/xdalsh/p/8399299.html

Hibernate访问数据库,HibernateTemplate操作数据库,实现增删改查

1、首先创建封装Hibernate的Dao类 import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.stereotype.Component; Component public class U…

前端学习(2713):重读vue电商网站33之实现首页路由重定向

我们想要在登录之后重定向到 /welcome 路径&#xff0c;于是需要添加子路由children 属性&#xff0c;然后设置 redirect重定向到我们的子路由。 然后在我们组件 Home.vue 的右侧主体区域&#xff0c;放置一个路由占位符 router-view 即可。

@Value(${xxxx})注解的配置及使用

代码中的用法 Spring 通过Value注解获取*.porperties文件code的内容&#xff0c;然后赋值给使用该注解的Code属性上。 Value("${code}") private String Code; 看一下这个resource.properties文件&#xff08;resource为文件名称&#xff09; code002 spring中的配置…

国家语言,语言代码,locale id对应表

国家语言&#xff0c;语言代码&#xff0c;locale id对应表。比如 en_US对应的id为1033&#xff0c; 中文的localezh_CN&#xff0c;id2052. LocaleLanguagecodeLCIDstringLCIDDecimalLCIDHexadecimalCodepageAfrikaansafaf10784361252Albaniansqsq1052 1250Amharicamam1118 A…

myeclipse链接数据库,SQL语句实现增删改查

1、创建properties文件 #oracle #driveroracle.jdbc.OracleDriver #urljdbc\:oracle\:thin\:localhost\:1521\:xe #usernamesystem #passwordmanager #mysql drivercom.mysql.jdbc.Driver urljdbc\:mysql\://192.168.0.121\:3306/test?user\root&password\root&chara…

前端学习(2714):重读vue电商网站34之侧边栏路由改造

通过给侧边栏设置 router 属性为 true&#xff0c;我们就能开启 vue-router 路由模式&#xff0c;然后将我们二级菜单的 index 属性绑定我们的数据 path 即可。

(转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)

1. 过滤器跟拦截器的区别 在说拦截器之前&#xff0c;不得不说一下过滤器&#xff0c;有时候往往被这两个词搞的头大。 其实我们最先接触的就是过滤器&#xff0c;还记得web.xml中配置的<filter>吗~ 你应该知道spring mvc的拦截器是只拦截controller而不拦截jsp,html 页…

ASP.NET Core源码学习(一)Hosting

ASP.NET Core源码的学习&#xff0c;我们从Hosting开始&#xff0c; Hosting的GitHub地址为&#xff1a;https://github.com/aspnet/Hosting.git 朋友们可以从以上链接克隆或是下载。 为什么是从Hosting开始学习呢&#xff1f;我们来看看ASP.NET Core MVC项目中的Pragram.CS中的…