SSM框架

一.SSM框架简单介绍

SSM(Spring+SpringMVC+MyBatis)框架搜索集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。

其中spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个我们就不用去new来创建对象。

SpringMVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制,相当于controller(等价于传统的servlet和struts的action),也就是说可以拦截网站上的请求,调用controller层中相应的方法去处理这个请求,比如说提交一张表单,springMVC就会拦截这个请求,并调用该相应方法去处理(对表单数据的增删查改),然后返回到相应页面或者是返回客户数据。

MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架,也就是来管理sql,与数据库打交道的。

具体的内容可以看这篇SSM知识总结

 

二.SSM与SSH之间的区别

刚学习后端开发都会先学习SSH框架,SSH框架是由spring+Struts+hibernate整合的,那跟现在学的SSM有什么区别呢?

首先都用到了sping,前面讲过了sping在这里也不提了。其次Struts被替换成springMVC,hibernate替换成了MyBatis。

1. SpringMVC与Struts

两者都是实现了页面分离控制的功能,但Struts框架是类级别的拦截,每次请求就会创建一个Action,SpringMVC是方法级别的拦截,一个方法对应一个Request上下文,所以方法直接基本上是独立的,独享request,response数据,而每个方法同时又何一个url对应,参数的传递是直接注入到方法中的,虽然Struts中Action的一个方法可以对应一个url,而其类属性却被所有方法共享,这也就无法用注解或其他方式标识其所属方法了。

所以主要区别就在于struts是基于类开发,而springmvc是基于类中的方法开发,在配置过程中springMVC相当于要简单,springmvc使用更加简洁。

2. MyBatis与hibernate

hibernate开发中,sql语句已经被封装,直接可以使用,加快系统开发,Mybatis 属于半自动化,sql需要手工完成,稍微繁琐,但封装了并不一定好,如果对于庞大复杂的系统项目来说,发杂语句较多,选择hibernate 就不是一个好方案,所以手动编写sql,可以避免不需要的查询,提高系统性能。
所以说MyBatis简单,方便,而hibernate比较复杂,强大。

总结:两个框架各有各的优势,并不能说哪个框架就一定比哪个好,要根据项目来进行选择。

 

三.SSM框架运行流程

首先要有控制(Controller)层,负责具体的业务模块流程的控制,也就是去拦截客户发来的请求。

然后服务(Service)层,负责业务模块的逻辑应用设计,调用DAO层已定义的接口,去实现Service具体的实现类,也就是去和数据库(DAO)层打交道。

Dao层负责与数据库进行交互设计,用来处理数据的持久化工作,DAO层的数据源配置,以及有关数据库连接的参数都在Spring的配置文件中进行配置。

最后一层View层,负责前台jsp页面的展示,此层需要与Controller层结合起来开发,可以由控制层来跳转到某个jsp页面。

总结:控制层来拦截到客户端发来的请求,并把数据交给服务层,服务层通过与DAO层打交道,利用DAO层对数据的增删查改,最后控制层和view层打交道,展现到某个jsp页面。

 

所以说在写后端过程中都是先写实体类entity,定义对象的属性,再写Mapper.xml,Dao接口,再服务层,控制层,最后就是写JSP页面调用,请求哪些参数,需要获取什么数据。

 

四.SSM框架的整合

简单实现了一个登录注册功能,运用注解来实现的,你自己也可以去配置xml来实现,个人觉得配置xml没有那么方便好用。

1.按照我上面的流程的话,先是建一个实体类user

private int id;
private String name;
private String password;

2.然后mybatis配置文件

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

<mapper namespace="com.ssm.dao.user.UserDao">

	<!--注意sql语句没有分号结尾 -->
	<sql id='TABLE_NAME'>t_user</sql>

	<insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="User">
		insert into
		<include refid="TABLE_NAME" />
		(name, password)
		values ( #{name}, #{password})
	</insert>

	<!--注意这里的参数类型是parameterType而不是parameterMap,因为返回的是单个类型 -->
	<select id="findUser" parameterType="String"
		resultType="User">
		<!-- select t.name,t.password from t_user t where name=#{name}
		and password=#{password} -->
		select * from t_user where name=#{name} and password=#{password}
	</select>

</mapper>

3.dao接口

public void addUser(User user);
public User findUser(@Param("name")String name, @Param("password")String password);

4.服务层

public interface UserService {
	
	void regist(User user);
	
	User login(String name, String password);

}
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public void regist(User user) {
		
		userDao.addUser(user);
	}

	@Override
	public User login(String name, String password) {
		
		User user=userDao.findUser(name,password);
		 if(user != null){
	            return user;
	        }
	        return null;
	}
}

不要忘记加上@Service注解

5.然后控制层

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("regist")
	public String regist(User user,Model model){
		userService.regist(user);
		
		model.addAttribute("msg", "注册成功");
		return "success";
	}
	
	@RequestMapping("login")
	public String login(String name,String password,Model model){
		User user=userService.login(name,password);
		
		if(user != null){
            model.addAttribute("msg", "登录成功");
    		model.addAttribute("username",user.getName());
    		
    		return "success";
        }
        return "fail";
	}
}

6.配置文件

1.spring-dao.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"
        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过程 -->
    <!-- 1:配置数据库相关参数 properties的属性:${url}-->
    <context:property-placeholder location="classpath:db.propertise"/>
    <!-- 2:数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${dataSource.driverClass}"></property>
        <property name="jdbcUrl" value="${dataSource.jdbcUrl}"></property>
        <property name="user" value="${dataSource.user}"></property>
        <property name="password" value="${dataSource.password}"></property>

        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"></property>
        <property name="minPoolSize" value="10"></property>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"></property>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="1000"></property>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"></property>
    </bean>

    <!-- 约定大于配置 -->
    <!-- 3:配置sqlsessionfactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.ssm.bean" />
    </bean>

    <!-- 4:配置扫描Dao接口的包,动态实现Dao接口,注入到Spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlsessionfactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 给出需要扫描的Dao接口包 -->
        <property name="basePackage" value="com.ssm.dao.user"></property>
    </bean>
</beans>

2.spring-mvc.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-3.0.xsd
        ">
    <!-- 配置springMVC -->
    <!-- 1:开启springMVC注解模式 -->
    <!-- 简化配置:
         (1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
         (2)提供一系列:数据绑定,数字和日期的format  @NumberFormat,@DataTimeFormat
        xml,json默认读写支持
     -->
    <mvc:annotation-driven/>

    <!-- 2:配置jsp显示ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- 3:扫描web相关的bean -->
    <context:component-scan base-package="com.ssm" />
</beans>

3.spring-service.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"
        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">
    <!-- 扫描service包下所有使用注解的类型 -->
    <context:component-scan base-package="com.ssm.service"/>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库的连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置基于注解的声明式事务
        默认使用注解来管理事务行为
     -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

最后一个db.propertise

dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/test
dataSource.user=root
dataSource.password=123456

从配置文件名字就可以看出spring-mvc.xml是配置控制层的,spring-dao.xml配置与数据库的属性,spring-service.xml配置服务层的。

7.view层的jsp页面

1.登录界面lojin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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 action="${pageContext.request.contextPath }/user/login.action">
		<table border="1">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="text" name="password"></td>
			</tr>
			<tr>
				<td><input type="submit" value="登录"></td>
			</tr>
		</table>
	</form>
</body>
</html>

2.注册页面rigst.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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 action="${pageContext.request.contextPath }/user/regist.action">
		<table border="1">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="text" name="password"></td>
			</tr>
			<tr>
				<td><input type="submit" value="注册"></td>
			</tr>
		</table>
	</form>
</body>
</html>

3.成功界面success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
    ${username}</br>
	${msg }</br>
	${abc}</br>
	<a href="${pageContext.request.contextPath }/login.jsp">去登录</a>
	<a href="${pageContext.request.contextPath }/regist.jsp">去注册</a>
</body>
</html>

4.失败界面fail.jsp



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>失败</title>
</head>
<body>
        u are loser,this is a fail page!
</body>
</html>

 

  • 20
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值