最基础的SSM框架整合篇
一、简单理解
Spring、Spring MVC和MyBatis的整合主要原理就是将我们在单独使用Spring MVC和MyBatis过程中需要自己实例化的类都交由Ioc容器来管理,过程分为两步:
第一步整合Spring和Spring MVC,前提是项目已单独配置Spring和Spring MVC且能正常运行,主要步骤为先在项目中创建对应的service接口和它们的实现类,并通过注解实现类和在Spring配置文件中开启注解扫描的方式将接口实现类交由Ioc容器管理。接着在Controller响应请求的类中添加接口为成员变量,并且也通过注解的方式将其交由Ioc容器管理,最后,我们需要把Spring配置文件的加载设置为项目启动时,这里通过在web.xml文件中配置Spring监听器实现,至此就可以实现Spring和Spring MVC的整合。
第二整合Spring和MyBatis,前提也是项目已单独配置Spring和MyBatis且能正常运行,这里需要导入额外的jar包mybatis-spring.jar,这里的版本需要根据MyBatis版本来确认,且本项目通过c3p0来配置数据库连接池,也需要导入c3p0的jar包。主要步骤为先将MyBatis配置文件中的内容(配置连接池部分)转移到Spring配置文件中(原先的MyBatis配置文件也就可以删除了),并且在Spring配置文件中配置SqlSessionFactroy工厂和sql语句对应接口所在包,这也就是将工厂和接口都交由Ioc容器管理,至此就可以实现Spring和MyBatis的整合。
二、代码展示
1.配置文件
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">
<display-name>SSMProject</display-name>
<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> <!-- 配置Spring监听器,默认加载WEB-INF目录下的applicationContext.xml配置文件 -->
<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> <!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring MVC配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 表示容器在启动时立即加载dispatcherServlet -->
<load-on-startup>1</load-on-startup>
</servlet> <!-- 让Spring MVC控制器拦截前端所有请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
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"> <!-- 开启注解扫描,非controller -->
<context:component-scan base-package="com.yh">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- Spring整合MyBatis -->
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<!-- 驱动类名 -->
<property name="jdbcUrl"
value="jdbc:sqlserver://127.0.0.1:1433;databaseName=onlineshoppingmall" /><!--
url访问地址 -->
<property name="user" value="sa" /><!-- 链接数据库的用户名 -->
<property name="password" value="12345yehuan" /><!-- 链接数据库的密码 -->
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="0" />
</bean>
<!-- 配置SqlSessionFactroy工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置接口所在包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yh.mybatis.mapper"></property>
</bean>
<!-- 配置Spring框架声明式事务管理 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP增强 -->
<aop:config>
<aop:pointcut expression="execution(* com.yh.service.*.*(..))" id="txPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
</beans>
springmvc.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: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.yh.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- 视图解析器对象 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean> <!-- 解决访问html等其他资源404 -->
<mvc:default-servlet-handler /> <!-- 开启SpringMVC注解支持 -->
<mvc:annotation-driven /> </beans>
2.service接口和它的实现类
AddressService.java
package com.yh.service;
import java.util.List;
import com.yh.entity.Address;
public interface AddressService {
int addAddress(Address address);
List<Address> loadAddress();
}
AddressServiceImpl.java
package com.yh.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.yh.entity.Address;
import com.yh.mybatis.mapper.AddressMapper;
import com.yh.service.AddressService; @Service("addressService")
public class AddressServiceImpl implements AddressService { @Autowired
private AddressMapper am; @Override
public int addAddress(Address address) {
return am.addAddress(address);
} @Override
public List<Address> loadAddress() {
System.out.println("业务层查找地址信息");
return am.loadAddress();
} }
3.MyBatis的sql语句配置文件和接口
AddressMapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.mapper.AddressMapper"> <select id="loadAddress" resultType="com.yh.entity.Address">
select* from address
</select> <insert id="addAddress" parameterType="com.yh.entity.Address">
insert into address
(buyerid,consignee)values(#{buyerId},#{consignee})
</insert> </mapper>
AddressMapper.java
package com.yh.mybatis.mapper; import java.util.List; import org.springframework.stereotype.Repository; import com.yh.entity.Address; @Repository
public interface AddressMapper { int addAddress(Address address); List<Address> loadAddress();
}
4.响应前端请求的Controller类
AddressController.java
package com.yh.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.yh.entity.Address;
import com.yh.service.AddressService; @Controller
@RequestMapping(value = "/address")
public class AddressController { @Autowired
private AddressService addressService; @ResponseBody
@RequestMapping(value = "/loadAddress", produces = "application/json; charset=utf-8")
public String loadAddress() {
System.out.println("表现层查询所有地址");
Address addr = new Address();
addr.setBuyerId(99);
addr.setConsignee("叶欢");
int result = addressService.addAddress(addr);
System.out.println(result != 0 ? "成功" : "失败");
return null;
} }
5实体类
Address.java
package com.yh.entity;
import java.io.Serializable;
public class Address implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int buyerId;
private String consignee;public int getBuyerId() {
return buyerId;
}
public void setBuyerId(int buyerId) {
this.buyerId = buyerId;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
@Override
public String toString() {
return "Address [addressId=" + addressId + ", buyerId=" + buyerId + ", consignee=" + consignee + ", telephone="
+ telephone + ", detail=" + detail + ", defaultAddress=" + defaultAddress + "]";
}
}
最基础的SSM框架整合篇的更多相关文章
- SSM框架整合篇
目录 SSM整合 框架搭建步骤 SSM整合 Author:SimpleWu github(已上传SSMrest风格简单增删该查实例):https://gitlab.com/450255266/code ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- SpringMVC--从理解SpringMVC执行流程到SSM框架整合
前言 SpringMVC框架是SSM框架中继Spring另一个重要的框架,那么什么是SpringMVC,如何用SpringMVC来整合SSM框架呢?下面让我们详细的了解一下. 注:在学习SpringM ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- ssm框架整合-过程总结(第二次周总结)
距离上次写博客已经有4.5天的时间了. 这次写博客目的是总结一下项目开始到现在,过程中遇到的问题.和学到的知识.经验. 初略总结下自己从中学到的: Spring :在学习中被反复强调的Ioc(反转控制 ...
- SSM框架整合项目 :租房管理系统
使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...
- 基于maven的ssm框架整合
基于maven的ssm框架整合 第一步:通过maven建立一个web项目. 第二步:pom文件导入jar包 (1 ...
- JavaWeb之ssm框架整合,用户角色权限管理
SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...
- springmvc(二) ssm框架整合的各种配置
ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...
随机推荐
- 如何保证redis中存放的都是热点数据
当redis使用的内存超过了设置的最大内存时,会触发redis的key淘汰机制,在redis 3.0中有6种淘汰策略: noeviction: 不删除策略.当达到最大内存限制时, 如果需要使用更多内存 ...
- 大一C语言学习笔记(8)---指针篇--动态内存是什么?与静态内存有什么区别?怎么使用动态内存,有什么需要注意的地方?
静态内存指的是在编译时系统自动给其分配的内存,运行结束后会自动释放:静态内存是在栈中分配的: 动态内存是我们程序员手动分配的内存,正常情况下,程序运行结束后,也不会自动释放,所以为了避免发生未知的错误 ...
- 记一次 .NET 某电商无货源后端服务 死锁分析
一:背景 1. 讲故事 这个月初,星球里的一位朋友找到我,说他的程序出现了死锁,怀疑是自己的某些写法导致mongodb出现了如此尴尬的情况,截图如下: 说实话,看过这么多dump,还是第一次遇到真实的 ...
- Java_map
1 package Test; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class MapTest { 7 p ...
- FastAPI(六十二)实战开发《在线课程学习系统》需求分析
前言 基础的分享我们已经分享了六十篇,那么我们这次分享开始将用一系列的文章分享实战课程.我们分享的系统是在线学习系统.我们会分成不同的模块进行分享.我们的目的是带着大家去用fastapi去实战一次,开 ...
- js--迭代器总结
前言 我们已经熟练使用set.map.array几种集合类型了,掌握了map(),for..of..,filter()等迭代集合的方法,你是否思考过,js引擎是怎么迭代的,怎么判断迭代是否结束,本文来 ...
- 使用protobuf-java-format包 JsonFormat转Json部分默认值字段消失问题
使用protobuf-java-format包 JsonFormat转Json部分默认值字段消失问题 1.产生的bug XXXXXXXXRequest.Builder request = XXXXXX ...
- 洛谷 P3285 - [SCOI2014]方伯伯的OJ(平衡树)
洛谷题面传送门 在酒店写的,刚了一整晚终于调出来了-- 首先考虑当 \(n\) 比较小(\(10^5\) 级别)的时候怎么解决,我们考虑将所有用户按排名为关键字建立二叉排序树,我们同时再用一个 map ...
- 洛谷 P6860 - 象棋与马(找性质+杜教筛)
题面传送门 首先我们来探究一下什么样的 \((a,b)\) 满足 \(p(a,b)=1\).不难发现只要点 \((1,0)\) 能够到达,那么网格上所有点都能到达,因为由于 \((1,0)\) 能够到 ...
- 3D-DNA 挂载染色体
3D-DNA是一款简单,方便的处理Hi-C软件,可将contig提升到染色体水平.其githup网址:https://github.com/theaidenlab/3d-dna 3D-DNA流程简介 ...