【职坐标】SSM框架整合
来源:互联网 发布:php urldecode解码 编辑:程序博客网 时间:2024/06/05 19:05
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.Driverurl=jdbc:mysql://localhost:3306/students_datausername=rootpassword=rootinitialSize=0maxActive=20maxIdle=20minIdle=1maxWait=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;@Controllerpublic class AdminController { @Resource private 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"; }}
阅读全文
0 0
- 【职坐标】SSM框架整合
- SSM:简单SSM框架整合
- SSM框架整合
- ssm框架整合
- SSM框架整合例子
- SSM框架整合
- SSM框架整合
- ssm框架整合
- SSM框架整合
- SSM 框架整合
- ssm框架总体整合
- SSM框架整合
- SSM 框架整合案例
- SSM框架整合
- SSM框架整合
- SSM框架整合
- SSM框架整合
- ssm框架整合开发
- 负载均衡(Load Balancing)学习笔记一
- (十三)redis 复制(Replication)
- java jvm 内存调优几个设置的一些背景知识
- hadoop2.74+zookeeper3.4.10+hbase1.2.6完全分布式HA集群搭建
- HDU-2222-AC自动机
- 【职坐标】SSM框架整合
- spring是什么?
- php 数组取数据(主要是删除指定的元素)
- 从两个函数来学习js闭包的概念
- Android 窗口的跳转(切换),结束
- gerrit提交代码(风格审查)出错处理
- 索引
- 轻松搞定面试中的红黑树问题
- eclipse的Ctrl+shift+F快捷键不能使用的问题