SSM框架的整合--详细的步骤解读

前言:

整合环境搭建

如何进行SSM框架整合?

由于Spring MVC是Spring框架中的一个模块,所以Spring MVC与Spring之间不存在整合的问题,只要引引入相应JAR包就可以直接使用。因此SSM框架的整合就只涉及到了Spring与MyBatis的整合,以及Spring MVC与MyBatis的整合。

SSM框架整合图如下:
在这里插入图片描述
其实通常JavaEE的分层结构图如下:
在这里插入图片描述
纵观上述两幅图,不难看出spring的整合思路。
在这里插入图片描述
具体的叙述见下图:
在这里插入图片描述

整合所需框架包

在这里插入图片描述

整合应用测试

编写配置文件

在这里插入图片描述
在这里插入图片描述

(1)引 入log4j配置文件log4j.properties

# Global logging configuration 
log4j.rootLogger=DEBUG, stdout

 # Console output... 
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(2)编写数据库连接参数配置文件db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

在这里插入图片描述

(3)编写Spring配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!-- 读取db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<!--连接数据库的url -->
		<property name="url" value="${jdbc.url}" />
		<!--连接数据库的用户名 -->
		<property name="username" value="${jdbc.username}" />
		<!--连接数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
		<!--最大连接数 -->
		<property name="maxTotal" value="${jdbc.maxTotal}" />
		<!--最大空闲连接  -->
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<!--初始化连接数  -->
		<property name="initialSize" value="${jdbc.initialSize}" />
	</bean>
     <!-- 事务管理器,依赖于数据源 -->
	<bean id="transactionManager" class=
     "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>	
    <!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 配置MyBatis工厂SqlSessionFactory -->
    <bean id="sqlSessionFactory" 
                           class="org.mybatis.spring.SqlSessionFactoryBean">
         <!--注入数据源 -->
         <property name="dataSource" ref="dataSource" />
         <!--指定核MyBatis心配置文件位置 -->
  <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- 配置mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.zsj.dao"/>
	</bean>
    <!-- 扫描Service --> 
    <context:component-scan base-package="com.zsj.service" />
</beans>

(4)编写MyBatis配置文件mybatis-config.xml

<?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.zsj.po" />
	</typeAliases>
</configuration>

(5)编写SpringMVC配置文件springmvc-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 配置包扫描器,扫描@Controller注解的类 -->
	<context:component-scan base-package="com.zsj.controller" />
	<!-- 加载注解驱动 -->
	<mvc:annotation-driven />
	<!-- 配置视图解析器 -->
	<bean class=
    "org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

(6)修改web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
  	id="WebApp_ID" version="3.1">
  	<!-- 配置加载Spring文件的监听器-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
		     org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encoding</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>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 配置Spring MVC前端核心控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>
		     org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>
		<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
    	<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!--/:拦截所有请求(除了jsp)-->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

整合开发

项目目录结构:
在这里插入图片描述

1、在com.zsj.po包中创建持久化类Customer

package com.zsj.po;
/**
 * 客户持久化类
 */
public class Customer {
	private Integer id;       // 主键id
	private String username; // 客户名称
	private String jobs;      // 职业
	private String phone;     // 电话
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getJobs() {
		return jobs;
	}
	public void setJobs(String jobs) {
		this.jobs = jobs;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
}

2、在com.zsj.dao包中创建接口CustomerDao以及对应的映射文件CustomerDao.xml

CustomerDao中只定义了-一个方法findCustomerById (Integer id)。
CustomerDao.xml根据CustomerDao接口方法编写了对应的执行语句。

package com.zsj.dao;
import java.util.List;
import com.zsj.po.Customer;
/**
 * Customer接口文件
 */
public interface CustomerDao {
	/**
	 * 根据id查询客户信息
	 */
	public Customer findCustomerById(Integer id);
	public List<Customer> findCustomerByName(String name);
}

<?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.zsj.dao.CustomerDao">
	<!--根据id查询客户信息 -->
	<select id="findCustomerById" parameterType="Integer"
		                               resultType="Customer">
		select * from t_customer where id = #{id}
	</select>
	<!--根据客户名模糊查询客户信息列表-->
	<select id="findCustomerByName" parameterType="String"
	    resultType="Customer">
	 	select * from t_customer where username like '%${value}%'
		<!-- select * from t_customer where username like concat('%',#{value},'%') --> 
	</select>
</mapper>

3、在com.zsj.service包中创建服务接口CustomerService

package com.zsj.service;
import java.util.List;

import com.zsj.po.Customer;
public interface CustomerService {
	public Customer findCustomerById(Integer id);
	//根据客户名模糊查询客户信息列表
	public List<Customer> findCustomerByName(String name );
}

4、创建com.zsj.service.impl包并创建服务接口实现类CustomerServicelmpl

在这里插入代码片package com.zsj.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zsj.dao.CustomerDao;
import com.zsj.po.Customer;
import com.zsj.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	//注解注入CustomerDao
	@Autowired
	private CustomerDao customerDao;
	//查询客户
	public Customer findCustomerById(Integer id) {
		return this.customerDao.findCustomerById(id);
	}
	//模糊查询用户
	public List<Customer> findCustomerByName(String name) {
		return this.customerDao.findCustomerByName(name);
	}
}

在这里插入图片描述

5、在 com.zsj.controller包中创建控制器类CustomerController

package com.zsj.controller;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zsj.po.Customer;
import com.zsj.service.CustomerService;
@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	/**
	 * 根据id查询客户详情
	 */
	@RequestMapping("/findCustomerById")
	public String findCustomerById(Integer id,Model model) {
		Customer customer = customerService.findCustomerById(id);
		model.addAttribute("customer", customer);
		//返回客户信息展示页面
		return "customer";
	}
	@RequestMapping("/findCustomerByName")
	public String findCustomerByName(String name,Model model) {
		List<Customer> customerList = customerService.findCustomerByName(name);
		model.addAttribute("customerList", customerList);
		//返回客户信息展示页面
		return "customerList";
	}
}

在这里插入图片描述

6、在/WEB-IN/jsp目录下创建一个展示客户详情的页面customer.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>
	<table border=1>
		<tr>
			<td>编号</td>
			<td>名称</td>
			<td>职业</td>
			<td>电话</td>
		</tr>
		<tr>
			<td>${customer.id}</td>
			<td>${customer.username}</td>
			<td>${customer.jobs}</td>
			<td>${customer.phone}</td>
		</tr>
	</table>
</body>
</html>

在这里插入图片描述

7.打开浏览器,输入:http: / / localhost : 8080/ SSM/ findCus tomerById?id=1

在这里插入图片描述

扩展练习

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
	<table border=1>
		<tr>
			<td>编号</td>
			<td>名称</td>
			<td>职业</td>
			<td>电话</td>
		</tr>
		<c:forEach items="${customerList}" var="customer" >
		<tr>
			<td>${customer.id}</td>
			<td>${customer.username}</td>
			<td>${customer.jobs}</td>
			<td>${customer.phone}</td>
		</tr>
		</c:forEach>
	</table>
</body>
</html>

6.打开浏览器,输入: http: / / localhost: 8080/SSM/findCustome rByName ?name=小
在这里插入图片描述
致此,关于SSM框架整合的详细步骤到此为止。后续会添加其他框架的学习案例以及项目实战分享!!在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值