一:整合环境搭建
1.要实现SSM框架的整合,首先要准备这三个框架的JAR包,以及其他整合所需的JAR。 在Eclipse中,创建一个名为chapter17的Web项目,将整合所需的JAR包添加到项目的lib目录中,并发布到类路径下。
2. 在chapter17项目,创建一个名为config的源文件夹(Source Folder),在该文件夹中分别创建数据库常量配置文件db.properties、Spring配置文件applicationContext.xml,以及MyBatis的配置文件mybatis-config.xml。
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
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">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<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"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
<context:component-scan base-package="com.itheima.service" />
</beans>
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.itheima.po" />
</typeAliases>
</configuration>
由于在Spring中已经配置了数据源信息以及mapper接口文件扫描器,所以在MyBatis的配置文件中只需要根据POJO类路径进行别名配置即可。
3.在config文件夹中,创建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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan
base-package="com.itheima.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>
在web.xml中,配置Spring的文件监听器、编码过滤器以及Spring MVC的前端控制器等信息。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置加载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>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>*.action</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>
二:整合应用测试
在src目录下,创建com.itheima.po包,并在包中创建持久化类Customer:
package com.itheima.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;
}
}
在src目录下,创建一个com.itheima.dao包,并在包中创建接口文件CustomerDao以及对应的映射文件CustomerDao.xml,编辑后分别如下所示:
package com.itheima.dao;
import com.itheima.po.Customer;
/**
* Customer接口文件
* @author Administrator
*
*/
public interface CustomerDao {
/**
* 根据id查询客户信息
*/
public Customer findCustomerById(Integer id);
}
CustomerDao.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">
<mapper namespace="com.itheima.dao.CustomerDao">
<!-- 根据id查询客户信息 -->
<select id="findCustomerById" parameterType="Integer" resultType="Customer" >
select * from t_customer where id=#{id}
</select>
</mapper>
在src目录下,创建com.itheima.service包,然后在包中创建接口文件CustomerService,并在CustomerService中定义通过id查询客户的方法:
package com.itheima.service;
import com.itheima.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
在src目录下,创建一个com.itheima.service.impl包,并在包中创建CustomerService接口的实现类CustomerServiceImpl:
package com.itheima.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
//注解注入Customer
@Autowired
private CustomerDao customerDao;
//查询客户
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
在src目录下,创建一个com.itheima.controller包,并在包中创建用于处理页面请求的控制类CustomerController:
package com.itheima.controller;
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.itheima.po.Customer;
import com.itheima.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
* 根据id查询客户详情
*
*/
@RequestMapping("/info")
public String findCustomerById(Integer id,Model model) {
Customer customer = customerService.findCustomerById(id);
model.addAttribute("customer",customer);
//返回客户信息展示页面
return "customer";
}
}
在WEB-INF目录下,创建一个jsp文件夹,在该文件夹下创建一个用于展示客户详情的页面文件customer.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WINNER</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>
将项目发布到Tomcat服务器并启动,在浏览器中访问地址http://localhost:8081/chapter17/findCustomerById?id=1,显示效果如下
所示:
来源:CSDN
作者:Eider1998
链接:https://blog.csdn.net/Eider1998/article/details/104335265