【职坐标】SSM框架整合
SSM框架是目前为止企业中最为流行的开发框架。SSM框架中包含spring框架、springMVC框架以及Mybatis框架
搭建SSM的步骤如下所示
1.创建Java Web工程,并导入相应的jar包
SSM框架所需jar包的下载地址:http://pan.baidu.com/s/1kUCvuxp
2.配置Web.xml文件
对spring进行配置
<!-- 配置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>
对springMVC进行配置
<!-- 配置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:spring-mvc.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>
配置Web工程中的欢迎界面
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
3.配置springMVC文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"><!-- 设置使用注解的类所在的包 --> <context:component-scan base-package="com.iotek.controller" /> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/view/" /> <property name="suffix" value=".jsp" /> </bean>
</beans>
视图解析器会对controller方法的返回值进行加工,例如:controller的返回值为String类型的“success”时,则经过视图解析器解析之后需要跳转的页面为“/view/success.jsp”
4.使用spring对springMVC和Mybatis进行整合
spring整合Mybatis
装载数据源及sqlSessionFactory(spring-db.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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/iotek/mapper/*.xml"></property></bean> </beans>
数据源配置文件(jdbc.properties)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/students_data
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000
DAO层配置文件
<?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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。 可指定多个包,包与包之间用逗号或分号分隔--> <property name="basePackage" value="com.iotek.dao" /> <property name="annotationClass" value="org.springframework.stereotype.Repository" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
Mybatis事务处理
<?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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 使用注解式注入 --> <context:annotation-config /> <!-- 自动扫描 --> <context:component-scan base-package="com.iotek" /> <!-- 导入DAO配置 --> <import resource="spring-dao.xml"/> <!-- 导入数据库配置 --> <import resource="spring-db.xml"/> <!-- 导入数据库配置 --> <import resource="spring-tx.xml"/> </beans>
控制层代码
package com.iotek.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import com.iotek.enity.Admin;
import com.iotek.service.AdminService;@Controller
public class AdminController {@Resourceprivate AdminService adminService;@RequestMapping("/regist")public String regist(String adminName,String adminPass){Admin admin = new Admin();admin.setAdminName(adminName);admin.setAdminPass(adminPass);adminService.addAdmin(admin);return "success";}}
【职坐标】SSM框架整合相关推荐
- SSM Chapter 12 SpringMVC扩展和SSM框架整合
SSM Chapter 12 SpringMVC扩展和SSM框架整合 笔记 本章目标: 掌握JSON对象的处理 理解数据转换和格式化 了解本地化 掌握Spring MVC+Spring+MyBatis ...
- (转)淘淘商城系列——SSM框架整合之Dao层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...
- SpringMvc框架及SSM框架整合
SpringMvc框架及SSM框架整合 一.SpringMvc相关知识 1.Spring和SpringMvc的关系 1.1.Spring是IOC和AOP的容器框架,SpringMVC是基于Spri ...
- JavaEE——SSM框架整合实现学生信息注册案例
目录 十.SSM框架整合实现学生信息注册案例 1. 创建06-ssm的web项目 2. 修改web.xml版本为4.0 3. 更新pom.xml文件 4. jdbc的属性文件和日志文件 5. appl ...
- 全栈开发实战 | SSM框架整合完整教程
"一个人最好的状态:梦想藏在心里,行动落于腿脚." 目录 1.前言 2.基本概念 2.1 MyBatis 2.2 Spring 2.3 SpringMVC 3.开发环境搭建 3.1 ...
- java ssm小案例_简易的SSM框架整合小案例
简易的SSM框架整合小案例 一.创建一个web工程的maven项目 1.项目名随便起 2.选择好你的maven路径,然后finish 二.配置pom.xml文件 org.springframework ...
- SSM框架整合完整案例
SSM框架整合 一.整合思路 二.案例实战 1. 项目前期准备 2. 整合dao层 ① mybatis全局配置文件(SqlConfig.xml) ② 配置spring.xml ③ 编写POJO类(ja ...
- SSM框架整合所需相关jra包的maven地址
SSM框架整合: jdk1.7 +maven 3.11+tomcat7+mysql+velocity <project xmlns="http://maven.apache.org/P ...
- SSM框架整合配置文件
SSM框架整合配置文件 SSM框架整合的各个基本配置文件的模板,学习狂神SSM视频总结的配置笔记,方便以后SSM项目的搭建复用. 1. 项目的整体结构 2 基本环境配置 2.1 相关的Maven依赖: ...
- SSM框架整合+简单案例实现
SSM框架整合+简单案例实现 文章目录 前言 一.Spring+SpringMVC+Mybatis框架整合 1.建立一个新的web项目 2.所需jar包 3.建立数据库表与实体类之间的映射 4.web ...
最新文章
- 在隐私的博弈时代,BCH为你保驾护航
- 为何python不好找工作k-为何有人说Python不好找工作?
- 科大星云诗社动态20210529
- R语言进行文件夹操作示例(转)
- 一文了解Innodb中的锁
- 思科路由器配置命令(二)
- Windows 11 新版 22598 发布!引入 4K 聚焦壁纸,优化全新任务管理器和媒体播放器...
- 又一个程序猿的奋斗史——第二章 实习
- 怎样理解python是解释型语言
- 服务器虚拟机化对应云计算的,服务器虚拟化与云计算
- 短视频高流量的秘诀,上热门全靠这些技巧
- 我国村庄规划发展历程
- 严格身份证格式校验,真实身份证号校验方法文件CheckIdCardUtil.js
- Mac家谱制作软件MacFamilyTree 9好用吗?如何在MacFamilyTree 9中创建数据库的备份?
- CDH安装Spark2
- 火车订票系统属于哪方面的计算机应用,客机、火车订票系统属于()。 - 问答库...
- ijkplayer视频解码播放架构分析
- 市场力不从心,只剩死多头的口号
- 基于SSM的手表商城系统设计与实现
- json文件格式标准