mvc框架和ssm框架区别_mvc和ssm哪个简单

最近有粉丝给我留言SSM框架三件套,很重要!自己必须要会,但是不知道该怎么做,所以今天小编给大家整理一个SSM框架的搭建与整合教程案列

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

  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。

SSM框架整合配置

开发环境

IDE: Eclipse

mvc框架和ssm框架区别_mvc和ssm哪个简单

Jdk: 1.7

数据库: MySQL

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

项目需求

客户列表查询

根据客户姓名模糊查询

整合思路

第一步:整合dao层

Mybatis和spring整合,通过spring管理mapper接口,使用mapper扫描器自动扫描mapper接口,并在spring中进行注册。

第二步:整合service层

通过spring管理service层,service调用mapper接口。使用配置方式将service接口配置在spring配置文件中,并且进行事务控制。

第三步:整合springMVC

由于springMVC是spring的模块,不需要整合。

我用的是mysql5.7版本,开发工具是ecplise

加入配置文件

mybatis——mybatis配置

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

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

log4j.properties——log日志等

spring的jar包

mvc框架和ssm框架区别_mvc和ssm哪个简单

mvc框架和ssm框架区别_mvc和ssm哪个简单

spring与mybatis的整合jar包

mvc框架和ssm框架区别_mvc和ssm哪个简单

mybatis的jar包

mvc框架和ssm框架区别_mvc和ssm哪个简单

数据库驱动包

mvc框架和ssm框架区别_mvc和ssm哪个简单

log4j包

mvc框架和ssm框架区别_mvc和ssm哪个简单

log4j配置文件

log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c:\mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 39;info' to 'debug' # log4j.rootLogger=debug, stdout

数据库连接池包

mvc框架和ssm框架区别_mvc和ssm哪个简单

jstl包

mvc框架和ssm框架区别_mvc和ssm哪个简单

整合dao

sqlMapconfig.xml

mybatis的配置文件:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 定义别名 --> <typeAliases> <package name="com.haohan.ssm.po" /> </typeAliases> <!-- 配置mapper映射文件 --> <mappers> <!-- 加载 原始dao使用映射文件 --> <!-- <mapper resource="sqlmap/User.xml" /> -- <!--批量mapper扫描 遵循规则:将mapper.xml和mapper.java文件放在一个目录 且文件名相同 ,现在由spring配置扫描--> <!-- <package name="cn.itcast.ssm.dao.mapper" /> --> </mappers> </configuration>

db.properties数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/haohan1?characterEncoding=utf8&useSSL=false jdbc.username=root jdbc.password=123456

applicationContext-dao.xml

spring在这个xml文件中配置dbcp连接池,sqlSessionFactory,mapper的批量扫描。

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"   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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--1. 数据源 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置dbcp连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.name}"></property> <property name="password" value="${jdbc.password}"</property> </bean> <!--2. sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3. mapper的批量扫描--> <!-- mapper的批量扫描 :从mapper包中扫描mapper接口,自动创建代理对象并且在spring容器中注册。 遵循的规范:需要将mapper的接口类名和mapper.xml映射文件名保持一致,且在一个目录中。 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包名 如果扫描多个包,用半角逗号分开 --> <property name="basePackage" value="cn.haohan.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>

生成po类和mapper接口和mapper.xml文件

生成如下图的文件:

mvc框架和ssm框架区别_mvc和ssm哪个简单

自定义mapper接口和xml文件,以及po的包装类

mvc框架和ssm框架区别_mvc和ssm哪个简单

CustomMapper.java

public interface CustomMapper { public List<HhCustom> findAllCustom(HhCustomVo hhCustomVo)throws Exception; }

CustomMapper.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:命名空间,作用是对sql进行分类化管理,sql隔离 --> <mapper namespace="cn.haohan.ssm.mapper.CustomMapper"> <sql id="query_custom_where"> <if test="hhCustom!=null"> <if test="hhCustom.name!=null and hhCustom.name!=''"> name like '%${hhCustom.name}%' </if> </if> </sql> <resultMap type="hhCustom" id="hhCustomResultMap"> <id column="id" property="id"/> <result column="phone_number" property="phoneNumber"/> </resultMap> <select id="findAllCustom" parameterType="cn.haohan.ssm.po.HhCustomVo" resultMap="hhCustomResultMap"> SELECT * FROM hh_custom <where> <include refid="query_custom_where"></include> </where> </select> </mapper>

HhCustomVo

//客户的包装类 public class HhCustomVo { //客户信息 private HhCustom hhCustom; public HhCustom getHhCustom() { return hhCustom; } public void setHhCustom(HhCustom hhCustom) { this.hhCustom = hhCustom; } }

数据库表结构

mvc框架和ssm框架区别_mvc和ssm哪个简单

整合service

定义service接口

public interface CustomService { public HhCustom findCustomById(Integer id)throws Exception; public List<HhCustom> findAllCustom(HhCustomVo hhCustomVo)throws Exception; }

service接口实现

public class CustomServiceImpl implements CustomService{ @Autowired HhCustomMapper hhCustomMapper; @Autowired CustomMapper customMapper; @Override public HhCustom findCustomById(Integer id) throws Exception { // TODO Auto-generated method stub return hhCustomMapper.selectByPrimaryKey(id); } @Override public List<HhCustom> findAllCustom(HhCustomVo hhCustomVo) throws Exception { // TODO Auto-generated method stub return customMapper.findAllCustom(hhCustomVo); } }

在spring容器配置service(applicationContext-service)

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"   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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="CustomServiceImpl" class="cn.haohan.ssm.service.impl.CustomServiceImpl"></bean> </beans>

事务控制(applicationContext-transaction)

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置事务管理器 对mybatis操作数据库事务控制,spring使用jdbc事务控制类--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务增强(通知) --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 设置进行事务操作的方法匹配规则 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS"/> <tx:method name="get*" propagation="SUPPORTS"/> <tx:method name="select*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <!-- aop操作 --> <aop:config> <aop:advisor advice-ref="txadvice" pointcut="execution(* cn.haohan.ssm.serivce.impl.*.*(..))"/> </aop:config> </beans>

整合springMVC

springmvc.xml

在springmvc.xml中配置适配器映射器、适配器处理器、视图解析器

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描加载handler --> <context:component-scan base-package="cn.haohan.ssm.controller"></context:component-scan> <!-- 注解映射器 --> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> 注解适配器 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> --> <!-- 使用mvc:annotation-driven可代替上面的注解映射器和注解适配器mvc:annotation-driven默认加载许多参数绑定,比如json转换解析器,实际开发用mvc:annotation-driven--> <mvc:annotation-driven> </mvc:annotation-driven> <!-- 视图解析器 解析jsp,默认使用jstl标签,--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

配置前端控制器(web.xml)

<!-- 配置springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- contextConfigLocation:加载springmvc的配置文件(配置处理器适配器、映射器、视图解析器 默认加载的是/WEB-INF/servlet名称-servlet.xml( springmvc-servlet.xml) --> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <!--第一种:*.action ,访问以.action结尾的,由DispatcherServlet解析。 第二种:/ ,所有访问的地址都由DispatcherServlet解析,对于静态文件需要配置不让DispatcherServlet解析。 可以实现Restful风格。 --> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>

编写controller

@Controller public class CustomController { @Autowired CustomService customService; //模糊查询客户 @RequestMapping("/findAllCustom") public ModelAndView findAllCustom(HhCustomVo hhCustomVo) throws Exception { List<HhCustom> customlist = customService.findAllCustom(hhCustomVo); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("customlist", customlist); modelAndView.setViewName("customlist"); return modelAndView; } //根据客户id查询 public ModelAndView findCustomByid(Integer id) throws Exception { HhCustom hhCustom = customService.findCustomById(id); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hhCustom", hhCustom); modelAndView.setViewName("customlist"); return modelAndView; } }

编写jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>客戶列表</title> </head> <body> <form name="customForm" action="${pageContext.request.contextPath}/findAllCustom.action" method="post"> 查询条件: <table width="100%" border=1> <tr> <td>客戶名称:<input name="hhCustom.name" /> </td> <%-- <td>客戶类型: <select name="customType"> <c:forEach items="${customType}" var="customType"> <option value="${customType.key }">${customType.value}</option> </c:forEach> </select> </td> --%> <td><button type="submit" value="查询" >查询</button></td> </tr> </table> 客戶列表: <table width="100%" border=1> <tr> <th>选择</th> <th>客戶名称</th> <th>客戶邮箱</th> <th>客戶电话</th> <th>客户类型</th> <!-- <th>操作</th> --> </tr> <c:forEach items="${customlist}" var="custom"> <tr> <td><input type="checkbox" name="custom_id" value="${custom.id}" /></td> <td>${custom.name }</td> <td>${custom.mail }</td> <td>${custom.phoneNumber }</td> <td>${custom.category }</td> <%--<td><fmt:formatDate value="${custom.birthday }" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id }">修改</a></td> --%> </tr> </c:forEach> </table> </form> </body> </html>

加载spring容器(web.xml)

<!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

Post方法中文乱码(web.xml)

mvc框架和ssm框架区别_mvc和ssm哪个简单

本文【mvc框架和ssm框架区别_mvc和ssm哪个简单】由作者: 前端后端 提供,本站不拥有所有权,只提供储存服务,如有侵权,联系删除!
本文链接:https://www.cuoshuo.com/blog/4206.html

(0)
上一篇 2023-03-10 08:59:35
下一篇 2023-03-10 09:03:42

相关推荐

  • 初学编程100个代码大全_最基础的编程代码

    汇编语言编程简介 汇编语言编程是一种低级别的编程语言,它提供了一个与计算机硬件的直接接口。它是软件和硬件之间的接口,用于创建系统级程序,包括操作系统、设备驱动程序和固件。汇编语言编程被认为是计算机科学和计算机工程的一个基本方面,对于理解计算机系统的内部工作原理至关重要。 汇编语言编程的指令集 指令集是汇编语言程序的基本构建块。指令集是计算机CPU能够理解和执…

    2023-03-08
    600
  • 案例式c语言程序设计答案

    C语言程序设计王芳课后习题答案 2018-12-25 计算机/信号/编程 书籍名称:C语言程序设计 作 者:王芳 出 版 社:高等教育出版社 更多课后习题答案请 搜索公众号名称: 学糕课后答案解析

    2023-03-14
    100
  • 奇数页页眉设置为每章标题_如何页眉偶数页每一页都不同

    今天教大家在Word文件中设置页眉,在奇偶页设置不同页眉,并给一个文件中的不同章节设置不同的页眉,具体步骤如下。 1.给Word文件设置页眉: 打开Word文件,依次点击顶部工具栏的【插入】,【页眉】,然后选择一种页眉格式: 完成之后就进入页眉编辑状态,可以输入页眉内容: 输入页眉内容后,也可以对页眉的字体格式进行调整: 以上就完成了页眉的设置! 2.奇偶页…

    2023-03-14
    400
  • lcd1602程序编写与连接,lcd1602液晶显示屏原理图

    1.硬件原理 液晶屏的使用还是挺多的电子设备上用到的,最常见的就是电脑,手机,电视,还有小家电上。本次实验讲解用arduino来驱动1602液晶屏。1602液晶屏是一种字符型液晶,它的主控芯片是HD44780或者其它兼容芯片,可以显示数字,字母和符号,但是不能显示汉字,因为上面的点阵是5×7排列的,显示一个汉字至少需要8×6的点阵才可以显…

    2023-03-15
    100
  • linux运维最佳实践pdf linux吴光科

    在Linux运维领域中,什么是广大系统管理员们的“利器”呢?在我看来,系统管理员的“利器”有3个,一个是方法论,一个是经验,最后一个是积极饱满的学习精神。 我们面对的是一个不断变化的世界,业务需求在变,技术架构在变,开源工具与商业系统异构部署,新工具和技术概念层出不穷,唯有一套科学的技术方法论才能应对这些变化。很多时候,我们在面对新的问题时,会束手无措,这恰…

    2023-03-18
    100
  • html具体含义(html叫什么中文)

    1 什么是HTML HTML 是用来描述网页的一种语言。HTML 是一种在 Web 上使用的通用标记语言。HTML 允许你格式化文本,添加图片,创建链接、输入表单、框架和表格等等,并可将之存为文本文件,浏览器即可读取和显示。 HTML 指的是超文本标记语言: HyperText Markup Language HTML 不是一种编程语言,而是一种标记语言 标…

    2023-03-21
    000
  • java怎么设置一个变量 Java定义一个变量

    前言 在之前的文章中,壹哥给大家讲解了Java的第一个案例HelloWorld,并详细给大家介绍了Java的标识符,而且现在我们也已经知道该使用什么样的工具进行Java开发。那么接下来,壹哥会集中精力带大家学习Java的各种细节内容,比如什么是变量?Java里有哪些数据类型?请大家赶紧拿出小本本,做好笔记哦。 ———&#8…

    2023-03-19
    000
  • 学Android那些Java代码看不懂,python代码看不懂怎么办

    能看懂代码,需要掌握一门编程语言的语法以及代码逻辑,能够看万行代码,需要你对代码的架构和框架有一定的认知。 如何阅读代码?大家可以遵照下面的步骤: 第一步,明确代码功能:阅读官方说明文档或者运行源代码进行验证; 第二步,了解代码逻辑:梳理出这份代码第一步做了啥,第二步做了啥; 第三步,模仿或修改源代码:尝试修改代码,并按你的预期输出; 对于初学者,想要看懂代…

    2023-03-16
    200
  • 私有云公有云哪个成本更高,私有云比公有云便宜吗

    企业上云价值 现如今在云计算、大数据、物联网和人工智能领域,安全、可信、开放的云服务和资源成为了企业关注的重点。借助系统+云的快速共通力量可以更高效的构建面向企业数字化转型的集成办公、销售、营销等多场景解决方案,帮助企业顺利驶入发展快车道。 云平台分类 即便上云的重要性很高,但是云平台的安全性总是会被质疑,毕竟不是每一个软件都可以有多种云部署方式。 以Mic…

    2023-03-17
    100
  • linux环境变量保存在哪里_linux查看环境变量的值

    常见问题解决 环境变量一不注意就会造成大部分命令显示找不到命令 我们可以设置一个临时生效的环境变量,使命令暂时可用 export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin   进入vim /etc/profile中检查是否有输入错符合或者变量之类的,路径不对等的 修改完再次source即可 …

    2023-03-10
    200

发表回复

登录后才能评论
返回顶部
错说博客上线啦!