SSM 整合简单实现crud


    SSM 整合


    整合之前先看看各自的配置文件:

    
        Spring:Ioc控制反转、AOP面向切面。
    
            需配置web.xml(spring监听器、spring配置信息)、

            applicationContext.xml(数据库相关bean:dataSource、sessionFactory、transactionManager)


        SpringMVC:控制器,流程、页面显示的控制。

            需配置web.xml(dispatcherServlet分发器servlet的配置)、

            [servlet-name]-servlet.xml(配置自动扫描控制器,注解的启动,视图解析器)


        Mybatis:提供ORM服务,优秀的持久层框架。

            需配置mybatis.config.xml(配置数据库连接信息属性文件、数据源环境、实体类映射文件的路径)

            [entity-name]-mapper.xml(sql语句的映射)


    整合后配置文件:

            web.xml(spring监听器、spring+mybatis配置文件路径信息、dispatcherServlet分发器servlet)

            spring-mybatis.xml(结合applicationContext.xml和mybatis.config.xml)

            spring-mvc.xml(原[servlet-name]-servlet.xml,基本不变 可扩充)

            [entity-name]-mapper.xml(不变)

    配置文件的整合方法看了很多,其中最主要的是Spring对其他所有配置的bean管理。

 

1、项目目录如下:

2、其中test、tool包测试用,可以不用。使用的实体类表如下:

package com.gan.model;

public class Student {
	
	private int sno;
	private String sname;
	private double score;

       get/set
}

3、spring-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"> 
    
    <!-- 自动扫描装配所有实体类 -->  
    <context:component-scan base-package="com.gan" />  
    <mvc:annotation-driven/> 
    
    <!-- 引入配置文件 -->  
    <context:property-placeholder location="classpath:*.properties" />  
  
    <!-- 数据源 -->
    <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="${user}" />  
        <property name="password" value="${password}" />  
    </bean>  
  
    <!-- spring整合MyBatis -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com/gan/mapper/*.xml"/>  
    </bean>  
  
    <!-- mapper接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.gan.mapper" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>  
  
    <!-- (事务管理)transaction manager -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
</beans>  

3、spring-mvc.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">  
   
   
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
    <context:component-scan base-package="com.gan" />  
    
    <mvc:default-servlet-handler />
    <
  • 3
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值