SSM框架整合
环境搭建
准备所需JAR包
配置文件
在项目下选择Source Folder(源文件夹,也就项目根目录下)创建config文件夹,配置文件全放在里面。
Spring配置文件ApplicationContext.xml
- 创建db.properties文件(数据库常量配置)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
<!--最大连接数、最大空闲数、初始化连接数-->
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
- 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工厂 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.syl.dao"/>
</bean>
<!-- 扫描Service -->
<context:component-scan base-package="com.syl.service"/>
</beans>
在此配置文件中,包含了如下配置:
- 读取db.properties文件
<context:property-placeholder location="classpath:db.properties"/>
- 配置数据源
<!--采用了dbcp2包中的BasicDataSource类完成数据源配置-->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--接下来就是数据源的配置-->
<!--采用上文中读取到的db.properties文件中的变量-->
<!--数据库驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--url-->
<!--用户名-->
<!--密码-->
<!--最大连接数:maxTotal-->
<!--最大空闲数:maxIdle-->
<!--初始化连接数:initialSize-->
</bean>
- 事务管理器
<!--采用了jdbc包中的DataSourceTransactionManager类作为事务管理器-->
<bean id="transaction"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"/>
<!--依赖数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
- 开启事务注解
<!--指定使用哪个事务管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
- 配置SqlSessionFactory
<!--采用Spring和MyBatis整合的JAR包mybatis-spring中的spring包中的SqlSesionFactoryBean类作为会话工厂(SqlSessionFactory-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--需要注入数据源-->
<property name="dataSource" ref="dataSoure"/>
<!--指定MyBatis配置文件mybatis-config.xml所在位置-->
<property name="configLocation"
value="classpath:mybatis-config.xml"/>
</bean>
- 配置mapper扫描器
<!--
对数据库操作采用Spring和MyBatis整合的JAR包
mybatis-spring中的spring包中的
MapperScannerConfigurer类
此类还是基于MapperFactoryBean类对数据库操作的
采用基于接口的操作方式
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描dao包中实现数据库操作的接口创建实例-->
<property name="basePackage" value="com.syl.dao"/>
</bean>
- 扫描service包中的注解
<!-- 扫描service包中的注解然后注入属性 -->
<context:component-scan base-package="com.syl.service"/>
MyBatis配置文件mybatis-config.xml
数据源已经配置好了,所以在此对po包中的别名进行配置
<?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.syl.po"/>
</typeAliases>
</configuration>
Spring MVC配置文件springmvc-config.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: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.syl.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>
- 扫描controller包中的注解,完成属性自动化注入
<!-- 配置包扫描器,扫描@Controller类 -->
<context:component-scan base-package="com.syl.controller"/>
- 配置视图解析器
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--设置前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--设置后缀-->
<property name="suffix" value=".jsp"/>
</bean>
采用web JAR包中的servlet包中InternalResourceViewResolver类进行视图解析。
Tomcat配置文件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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>chapter17</display-name>
<!-- 自动加载Spring配置文件,Web服务器实例化ApplicationContext容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--指定用ContextLoaderListener方式启动Spring容器-->
<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>*.action</url-pattern>
</filter-mapping>
<!-- Spring前端控制器 -->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 自动加载Spring配置文件
<!-- 自动加载Spring配置文件,Web服务器实例化ApplicationContext容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--指定用ContextLoaderListener方式启动Spring容器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
自动加载Spring的配置文件相当于ApplicationContext a = classPathXmlApplicationContext(“applicationContext.xml”);操作。
- 加一个对中文编码的过滤器
- 前端控制器
DAO层开发
- 创建PO类(com.syl.po)
package com.syl.po;
public class Customer {
private Integer 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;
}
}
- 基于接口的开发方式(com.syl.dao)
package com.syl.dao;
import com.syl.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}
- 映射文件(com.syl.dao)
<?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.syl.dao.CustomerDao">
<!-- 根据ID查询客户信息 -->
<select id="findCustomerById" parameterType="Integer"
resultType="Customer">
select * from t_customer where id=#{id}
</select>
</mapper>
Service层开发
- 执行数据库操作的接口(com.syl.service)
package com.syl.service;
import com.syl.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
- 执行数据库操作(com.syl.service.impl)
package com.syl.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.syl.dao.CustomerDao;
import com.syl.po.Customer;
import com.syl.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
此处注解都已在applicationContext.xml配置文件中解释
Controller层开发
package com.syl.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.syl.dao.CustomerDao;
import com.syl.po.Customer;
import com.syl.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
//注入CustomerDao对象
private CustomerDao customerDao;
@Override
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
View层开发
WEB-INF文件夹下创建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>
访问
在Controller类中采用RESTful风格设计请求参数。启动Tomcat在浏览器输入如下请求:http://localhost:8080/chapter17/finCustomerById/1
@Syl 2021/08/30 22:13 周一 晴 回宿舍