**
喜欢有帮助记得点赞哦,加关注不迷路
**
SSM框架–详细整合过程(Spring,SpringMVC,myBatis)
SSM框架在Web项目开发过程中使用越来越频繁,此文记录在eclipse中SSM框架的整合流程,项目中没有使用Maven使用的是普通导入Jar包的方式,更适用于初学者,ssm整合的方式很多,这里给大家介绍的是个人最常用的也非常方便,大家可以在此基础上进行其他的操作,希望对大家有所帮助,那么我们开始吧!
一、前言
-
1.1 项目架构
开发工具 eclipse
Spring版本 5.1.2
myBatis版本 3.4.6
连接池 dbcp连接池
数据库 mysql5.7 -
1.2 Spring介绍
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
-
1.3 SpringMVC介绍
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
-
1.4 myBatis介绍
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
二、整合步骤
案例:商品查询
2.1 创建数据表
2.2 创建项目
2.3 在lib文件夹下引入jar包
2.4 配置springmvc文件
2.5 配置mybatis文件
2.6 配置web.xml文件
2.7 配置实体类的映射文件
2.8 完成jsp页面搭建
2.9 分别完成dao层service层web层代码
2.10 测试
三、具体实现
- 3.0 项目结构
- 3.1 创建数据表(商品表)
CREATE TABLE `goods` (
`goods_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',
`goods_name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`goods_type` varchar(255) DEFAULT NULL COMMENT '商品分类',
`goods_price` double(255,0) DEFAULT NULL COMMENT '商品单价',
`goods_count` int(11) DEFAULT NULL COMMENT '商品数量',
PRIMARY KEY (`goods_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 3.2 创建项目
使用eclipse创建项目,有部分地方做了修改,请各位仔细看呦
- 3.2.1 右击空白处创建动态web项目
- 3.2.2 填写项目名,点击next,注意点击next
- 3.2.3 修改class文件的输出位置
说明:使用eclipse创建动态web项目默认的class文件也就是java变异后的字节码文件会默认输出到build/classes问价夹中,我们的动态web项目的项目结构要求我们将class文件存放到WEB-INF下的classes文件夹中,我们这里做出修改。
WebContent\WEB-INF\classes
3.2.4 创建web.xml文件,完成项目创建,这里点击finish后有时创建时间比较久,那就需要等一下下。偶尔出现
3.2.5 项目结构
说明:这里我使用的视图为Package Explorer视图,可以调,点击 window—>show view
—>other—>搜索 Package即可,图在下方。这个视图比较干净简洁很实用
- 3.3 引入jar包,将jar包放在lib文件夹下
下载地址:https://download.csdn.net/download/qq_36386908/10765791
本来想给大家免费下载,上传资源时最少得选1积分,那就1积分吧,O(∩_∩)O哈哈~
3.4 SpringMVC配置文件,在src下创建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:mvc="http://www.springframework.org/schema/mvc"
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/aop
http://www.springframework.org/schema/aop/spring-aop.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.zhiyou100.ssm"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 如果需要文件上传,拦截器的等组件可以继续配置 -->
</beans>
3.5 Spring与Mybatis的配置文件
注意:Spring与myBatis整合是必须导入整合包,也就是mybatis-spring-1.3.1.jar。
在src下创建spring-mybatis.xml文件
<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"
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/aop
http://www.springframework.org/schema/aop/spring-aop.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.zhiyou100.ssm"/>
<!-- 读取外部的数据库配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 读取文件 -->
<property name="location" value="classpath:db.properties"></property>
</bean>
<!-- 配置数据源,交给dbcp管理 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${pass}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="initialSize" value="${initialSize}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置datasource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 读取实体类映射文件 -->
<property name="mapperLocations" value="classpath:com/zhiyou100/ssm/mapper/*.xml"></property>
</bean>
<!-- 配置扫描包,使用Dao的代理对象,实现只需要写dao层的接口,不需要写实现类的目的 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhiyou100.ssm.dao"></property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
dp.properties文件也贴出
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///day11_02
user=root
pass=root
maxActive=60
minIdle=60
initialSize=5
3.6 web.xml配置文件
<!-- 配置spring监听器
监听spring容器的状态
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring'MVC的核心控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 读取spring-mvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 设置优先启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置路径 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</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>
<!-- 读取spring-mybatis配置文件
项目启动后,读取mybatis配置文件
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
3.7 实体类
我的实体类在entity包下
public class Goods {
private Integer goods_id;//商品编号
private String goods_name;//商品名称
private String goods_type;//商品类型
private Double goods_price;//商品价格
private Integer goods_count;//商品数量
//无参构造
public Goods() {
super();
}
//全参构造
public Goods(Integer goods_id, String goods_name, String goods_type, Double goods_price, Integer goods_count) {
super();
this.goods_id = goods_id;
this.goods_name = goods_name;
this.goods_type = goods_type;
this.goods_price = goods_price;
this.goods_count = goods_count;
}
public Integer getgoods_id() {
return goods_id;
}
public void setgoods_id(Integer goods_id) {
this.goods_id = goods_id;
}
public String getgoods_name() {
return goods_name;
}
public void setgoods_name(String goods_name) {
this.goods_name = goods_name;
}
public String getgoods_type() {
return goods_type;
}
public void setgoods_type(String goods_type) {
this.goods_type = goods_type;
}
public Double getgoods_price() {
return goods_price;
}
public void setgoods_price(Double goods_price) {
this.goods_price = goods_price;
}
public Integer getgoods_count() {
return goods_count;
}
public void setgoods_count(Integer goods_count) {
this.goods_count = goods_count;
}
@Override
public String toString() {
return "goodss [goods_id=" + goods_id + ", goods_name=" + goods_name + ", goods_type=" + goods_type + ", goods_price="
+ goods_price + ", goods_count=" + goods_count + "]";
}
3.8 show.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 引入JSTL中的核心标签库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有商品</title>
</head>
<body>
<div align="center">
<!-- 使用表格显示数据
border:设置表格边框宽度单位为px(像素)
cellspacing:设置表格之间的间距,0为无间距
-->
<table border="1" cellspacing="0">
<!-- 表格标题自动居中加粗 -->
<caption>商品列表</caption>
<tbody>
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品类型</td>
<td>商品价格</td>
<td>剩余数量</td>
</tr>
<!-- 使用循环遍历所有商品 -->
<c:forEach items="${goods }" var="good">
<tr>
<td>${good.goods_id }</td>
<td>${good.goods_name }</td>
<td>${good.goods_type }</td>
<td>${good.goods_price }</td>
<td>${good.goods_count }</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
3.8 Controller层
@Controller
public class GoodsController {
//注入goodsService对象
@Resource(name="goodsService")
private IGoodsService goodsService;
/**
* 查询方法,查询所有商品,跳转到show.jsp页面
*/
@RequestMapping("show.do")
public String show(HttpServletRequest request) {
//查询所有
List<Goods> goods = goodsService.queryAll();
//将数据放到request域中
request.setAttribute("goods", goods);
//跳转页面,我们这里配置了视图解析器的前缀和后缀,这里只需要写页面的名字即可,会自动跳转到show.jsp
return "show";
}
}
3.9 Service层
3.9.1 接口
public interface IGoodsService {
List<Goods> queryAll();//查询方法
}
3.9.2 实现类
@Service("goodsService")
public class GoodsService implements IGoodsService{
//注入到层,这里dao没有实现类,@Resource中不需要写name属性,会根据Type注入dao的代理对象
@Resource
private IGoodsDao goodsDao;
//查询方法
@Override
public List<Goods> queryAll() {
return goodsDao.queryAll();
}
}
3.10 dao层接口
public interface IGoodsDao {
//方法名就是映射文件中的id属性值,根据方法名找到制定的sql语句执行
List<Goods> queryAll();
}
3.11 实体类映射文件
<?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的值是dao层接口的全路径 -->
<mapper namespace="com.zhiyou100.ssm.dao.IGoodsDao">
<!-- 配置实体类与数据表映射关系
type:实体类全路径
id:名字
-->
<resultMap type="com.zhiyou100.ssm.entity.Goods" id="goodsMap">
<!--
id标签:配置唯一字段,一般是主键
property:与实体类属性名一致
column:与数据表字段名一致
-->
<id property="goods_id" column="goods_id"/>
<!-- result标签配置非唯一字段 -->
<result property="goods_name" column="goods_name"/>
<result property="goods_type" column="goods_type"/>
<result property="goods_price" column="goods_price"/>
<result property="goods_count" column="goods_count"/>
</resultMap>
<!-- 定义表中字段 -->
<sql id="key">GOODS_ID,GOODS_NAME,GOODS_TYPE,GOODS_PRICE,GOODS_COUNT</sql>
<!-- 查询语句,查询所有
resultMap:与上边的resultMap标签的id属性值一致,将查出的数据按照配置的对应关系,封装到实体类中
-->
<select id="queryAll" resultMap="goodsMap">
SELECT <include refid="key"/> FROM GOODS;
</select>
</mapper>
注意:我们这里Dao层没有实现类,使用的是代理对象,这里有几个地方需要注意
1)实体类映射文件的namespace属性值必须是Dao层接口的全路径。
2)Dao层是根据方法名与映射文件中的id属性属性值匹配找到一条sql执行。
3.12 测试运行
在浏览器上输入服务器地址即可
至此ssm框架的搭建已经完成,不明白的可以留言或者私信
项目下载地址:https://gitee.com/stt0626/ssm_integration.git
盛年不重来,一日难再晨