一.因为SpringMVC是Spring框架中的一个模块,所以SpringMVC与Spring之间不存在整合的问题,因此SSM框架的整合就是Spring与MyBatis的整合(项目名称SSM)
二、准备所需的JAR包
三、编写配置文件
1.在src下创建db.properties数据配置文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybatis
jdbc.username = root
jdbc.password = kangxg198811
jdbc.maxTotal = 30
jdbc.maxIdle = 10
jdbc.initialSize = 5
2.在src下创建applicationContext.xml Spring配置文件
<?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"></property>
</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>
<bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kangxg.dao"/>
</bean>
<context:component-scan base-package ="com.kangxg.service" />
</beans>
3.在src下创建mybatis-config.xml MyBatis配置文件
<?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.kangxg.po"/>
</typeAliases>
</configuration>
4.在src下创建springmvc-config.xml SpringMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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.xsd">
<context:component-scan base-package="com.kangxg.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>
5.配置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>*.action</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC前端核心控制器 -->
<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>
<!-- 表示容器在启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
四、整合应用测试
1.在src目录下创建com.kangxg.po 包,并在包中创建持久化类Customer
package com.kangxg.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;
}
}
2.在src目录下创建com.kangxg.dao 包,并在包中创建接口文件CustomerDao 以及对应的映射文件CustomerDao.xml
package com.kangxg.dao;
import com.kangxg.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}
<?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 表示命名空间 -->
<mapper namespace="com.kangxg.dao.CustomerDao">
<select id="findCustomerById" parameterType = "Integer" resultType="Customer">
select * from t_customer
where
id =#{id}
</select>
</mapper>
3..在src目录下创建com.kangxg.service 包,并在包中创建接口文件CustomerService
package com.kangxg.service;
import com.kangxg.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
4..在src目录下创建com.kangxg.service.impl 包,并在包中创建接口CustomerService的实现类CustomerServiceImpl
package com.kangxg.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kangxg.dao.CustomerDao;
import com.kangxg.po.Customer;
import com.kangxg.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
public Customer findCustomerById(Integer id)
{
return this.customerDao.findCustomerById(id);
}
}
5..在src目录下创建com.kangxg.controller 包,并在包中创建用于处理页面请求的控制器类CustomerController
package com.kangxg.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.kangxg.po.Customer;
import com.kangxg.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("/findCustomerById")
public String findCustomerById(Integer id,Model model)
{
Customer customer = this.customerService.findCustomerById(id);
model.addAttribute("customer",customer);
return "customer";
}
}
6 在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>
五。debug 测试
http://localhost:8080/SSM/findCustomerById?id=1
项目目录结构