【职坐标】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 {
    @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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值