您的位置:首页 > 其它

ssm框架的集成

2017-12-28 13:13 92 查看
整合三大框架的思路:

Dao层:整合mybatis和Spring需要的JAR包:

1、mybatis的JAR包    2、Mysql数据库驱动包    3、数据库连接池

4、mybatis和Spring的整合包    5、Spring的JAR包

配置文件:

1、mybatis的配置文件:SqlMapConfig.xml   2、Spring的配置文件:applicationContext-dao.xml

a.数据源  b.连接池   c.配置sqlSessionFactory(mybatis和Spring整合包中的) d.配置mapper文件的扫描器

 

Service层:使用的jar包:Spring的jar包

1.配置文件:applicationContext-service.xml(包扫描器,扫描带有@Service注解的类)

2.事务配置:配置文件:applicationContext-trans.xml

配置一个事务管理器,配合tx,配置切面。

 

表现层:使用SpringMVC    使用的JAR包:使用SpringMVC和Spring的JAR包  

配置文件:1.springmvc.xml   2.配置注解驱动和视图解析器以及包扫描器(扫描Controller)

web.xml中:

1.配置SpringMVC的前端控制器  2.Spring容器初始化的listener

由于方便起见,就在一个配置文件中配置了(具体拆分看自己吧,哈哈哈,不完整以后再来补充)

/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">     <!-- 配置 Spring监听器,用于加载spring核心配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 配置springmvc,使用的是控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 配置springmvc,加载springmvc配置文件,默认的路径和配置文件名称,也可以自己定义
-->

            <param-value>classpath:springmvc/springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

classpath:spring/applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
    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/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
    <!-- 扫描service、dao组件 -->
    <context:component-scan base-package="" />
    <!-- 分解配置 jdbc.properites -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 数据源c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
        <property name="minPoolSize" value="${c3p0.pool.size.min}" />
        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
    </bean>
    <!-- sqlsessionFactory 将spring和mybatis整合 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
       
<!-- 加载核心文件 -->

        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
        <!-- 加载mapper文件 -->

        <property name="mapperLocations" value="classpath:/mapper/*.xml" />    
    </bean>
    <!-- 事务,
         事务管理的类型由数据访问层决定,由于是mybaits,所以使用datasourceTransactionManager
         如果使用hibernate,就需要使用HibernateTransactionManager

    -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="find" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="切点表达式"
            id="pointCut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />
    </aop:config>
</beans>            

classpath:springmvc/springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    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/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
    <!-- 注解方式 -->
    <mvc:annotation-driven />
    <!-- 自动扫描Controller -->
    <context:component-scan base-package="扫描包" />
    <!-- 配置一个springmvc框架的视图解析器 ,这个只是jsp的视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 通过setter方法注入前缀 /WEB-INF/jsps/showMsg.jsp-->
        <property name="prefix" value="/WEB-INF/jsps/" />
        <!-- 通过setter方法注入后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- 支持文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
</beans>
classpath:mybatis/sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 定义别名,一般没什么用 -->
    <typeAliases>
        <typeAlias type="权限定名称" alias="Person"/>
    </typeAliases>
    <!-- 这里不需要再注册mybatis的SQL映射文件,在Spring的配置文件中配置sqlSessionFactory时注册了mapper的路径-->
</configuration>    

jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
c3p0.pool.size.max=20
c3p0.pool.size.min=5
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssm 框架