ssm项目集成

说明:ssm指的是 spring + springMvc+ mybatis,是目前开发比较流行的集成方式,可以较好的实现前后端分离

spring与mybatis的集成,是通过配置文件applicaContext.xml实现的,其中将mybatis的核心对象SqlSessionFactory通过工厂创建bean对象的方法注入到spring容器中

这样就不需要使用new的方式创建对象,步骤简单,底层帮忙实现其中的mapper对象的封装,mapper对象使我们操作数据库的最终对象。

springmvc+spring 的集成:配置web.xml,在启动服务器的时候会读取web.xml文件,在读取的时候监听spring容器,加载spring核心配置文件和启动springMvc容器,加载springMvc核心配置文件,最后配置过滤器,解决编码问题。

步骤:

1.创建一个maven或者普通的web项目

使用ider'或者eclipse创建一个web项目(该结构就不再赘述)

2.导包

导包完成后,build path 或者 addlibary 解析,如果是maven项目,则在pop.xml 写依赖即可

3.写配置文件

1.写spring的核心配置文件:applicationContext.xml


<!--加载配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--扫描service层-->
<context:component-scan base-package="cn.itsource.ssm.service"/>
<!--配置数据库连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!--配置四大金刚-->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="url" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置mybatis核心对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置映射文件mapper.xml文件的位置-->
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"/>
<!--配置别名-->
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.domain
cn.itsource.ssm.query
</value>
</property>
</bean>
<!--配置扫描接口包,扫描mapper接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper"/>
</bean>
<!--配置事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--引入连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

写springMv核心配置文件:

<?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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描controller-->
<context:component-scan base-package="cn.itsource.ssm.controller" />
<!--静态资源处理-->
<mvc:default-servlet-handler />
<!--识别@requestMappering等注解支持-->
<mvc:annotation-driven />
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
写web.xml配置文件:
 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听spring的启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置启动springmvc-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--加载的时候就启动springmvc-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- 编码过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

4.测试:

创建一个包:domain  mapper service(包括实现类) controller test(用来写测试类)

domain:写数据库关联对象,提供get,set 和toSting方法

mapper:接口,相当于dao层,提供接口方法

service:业务层--提供与mapper层一样的接口方法,实现类:注入mapper接口,调用mapper中的方法

controller:控制页面跳转,(也可以返回json格式数据)

配置tomcat,配置完成后,启动服务器tomcat,访问资源

ssm项目集成的更多相关文章

  1. SSM项目集成Lucene+IKAnalyzer在Junit单元测试中执行异常

    个人博客 地址:http://www.wenhaofan.com/article/20181108132519 问题描述 在项目运行以及main方法中能够正常运行,但是使用junit单元测试时却报如下 ...

  2. SSM项目实战 之 权限管理系统

    目录 SSM权限管理系统 项目搭建 1.创建Maven-webapp工程 2.SSM框架集成 3.添加代码生成器 主页搭建 EasyUI主页 员工列表 1.在tree当中指定跳转的地址--暂时用tre ...

  3. Spring+SpringMVC+Mybatis(SSM)框架集成搭建

    Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...

  4. SSM框架集成各配置文件

    SSM框架集成各配置文件 Spring Spring MVC Mybatis 的整合SpringMVC相当于Spring的一个组件 本来就是一个家族的不存在整合的问题,所以主要就是Spring于Myb ...

  5. ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)

    Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...

  6. 现有iOS项目集成React Native过程记录

    在<Mac系统下React Native环境搭建>配置了RN的开发环境,然后,本文记录在现有iOS项目集成React Native的过程,官方推荐使用Cocoapods,项目一开始也是使用 ...

  7. SSH和SSM项目的打通各个页面的方式

    SSH项目: 这里采用的action的形式: 即在表现层为页面在action中配置一个返回值,然后在Struts.xml的配置文件中进行配置. SSM项目中,SpringMVC中利用注解来配置每个页面 ...

  8. SSH项目与SSM项目的进入首页的方法

    SSH项目中: jsp页面一般都是存放在WEB-INF下面的目录下,这样我们就不能直接访问到这些jsp页面了,保证了页面的安全性. 在struts的管理中,是利用action来实现页面的跳转,进入in ...

  9. 使用idea建立gradle+SSM项目

    目录: 一.创建一个gradle项目   二 .在gradle中创建SSM项目 一 .创建一个gradle项目 第一步: 第二步:选择gradle,并选中web,然后点击Next进入下一步 第三步:此 ...

随机推荐

  1. gRPC asp.net core自定义策略认证

    在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们. 项目名称 ...

  2. 为React绑定事件,并修改state中的值

    import React from 'react' export default class ClickS extends React.Component { constructor () { sup ...

  3. veu npm run dev指定host

    通常,我们可以在vue项目中的config/index.js指定host,,如下(解host的注释) 但是,在接手的目前项目中,解注释host后,npm run dev并有变为 http://192. ...

  4. 微调(Fine-tune)原理

    在自己的数据集上训练一个新的深度学习模型时,一般采取在预训练好的模型上进行微调的方法.什么是微调?这里已VGG16为例进行讲解,下面贴出VGGNet结构示意图. 上面圈出来的是VGG16示意图,也可以 ...

  5. (四)OpenStack---M版---双节点搭建---Glance安装和配置

    ↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 1.创建glance数据库 2.获得 admin 凭证来获取只有管理员能执行的命令的访问权限 3 ...

  6. 使用zuul实现验证自定义请求头中的token

    路由:她会把外部所有对请求转发到具体的微服务实例上,是实现外部访问同一接口的基础 过滤: 就是权限的检查, 判断当前的请求是否有权限区访问那些服务集群 搭建后台网关: 导入eureka - clien ...

  7. CSS中如果实现元素浮动,看这篇文章就足够了

    浮动基本介绍 在标准文档流中元素分为2种,块级元素和行内元素,如果想让一些元素既要有块级元素的特点也同时保留行内元素特点,只能让这些元素脱离标准文档流即可. 浮动可以让元素脱离标准文档流,可以实现让多 ...

  8. MATLAB数值计算——0

    目录 MATLAB数值计算 1.solve() 2.fzero() 3.fsolve() MATLAB数值计算 MATLAB中文论坛基础板块常见问题归纳(出处: MATLAB中文论坛) 登录http: ...

  9. 免费试用 | 多模 NoSQL 服务GeminiDB for Cassandra 全球首发

    PS:多模NoSQL服务GeminiDB重磅公测,免费体验,参与公测还有华为AI音响好礼相送~ 7月5日,华为云多模 NoSQL 服务GeminiDB for Cassandra正式对外定向邀测.华为 ...

  10. EventSource 实时传输数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...