发表于: 2017-02-10 00:15:54

1 1933


今天完成:

ssm的整合

参考:丁杰的日报:http://www.jnshu.com/daily/6559?uid=3933

http://www.cnblogs.com/shanheyongmu/p/5868420.html(一篇ssm的整合)

http://blog.csdn.net/gebitan505/article/details/44455235/

关于pom.xml中的依赖就不贴了,因为我也是从博客中复制粘贴

项目的结构

分为三个部分来创建

1.整合dao层。mybatis和spring整合,通过spring管理mapper接口。 

使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

mybatis的全局配置文件几乎可以省略了,如果有需要比如:别名,也可以加上这个配置文件

这里是对dao的整合,所以起名为ApplicationContext-dao.xml

配置文件的大致结构为:

<beans>

 <!-- 引入jdbc配置文件 --> 

 <!-- 配置数据源 -->

 <!-- 配置Mybatis的事务管理器-->

 <!-- DAO接口所在包名,Spring会自动查找其下的类 -->

 <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,-->

 <!-- 自动扫描注解的bean -->

</beans>

然后创建po类和mapper(参考文章使用逆向工具生成的代码,我还没有使用过)

到这里dao层就整合好了,下面来做个测试,整合的时候一定要步步为营,别啪啪啪整合完了再一起测试,到时候出错再找就不太方便了。

测试类



2.整合service层。通过spring管理 service接口。 

创建service接口及实现类

在service层配置ApplicationContext-transaction.xml(配置事务)

3.整合springmvc。由于springmvc是spring的模块,不需要整合。

web.xml

<!-- Spring和mybatis的配置文件 -->

<context-param>  

        <param-name>contextConfigLocation</param-name>  

        <param-value>classpath:ApplicationContext-*.xml</param-value>  

</context-param>

<!-- Spring监听器 -->  

    <listener>  

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

    </listener>

 <!-- Spring MVC servlet -->  

    <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>  

        <async-supported>true</async-supported>  

    </servlet>  

    <servlet-mapping>  

        <servlet-name>SpringMVC</servlet-name>  

        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  

        <url-pattern>/</url-pattern>  

    </servlet-mapping>  

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: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-4.2.xsd

        http://www.springframework.org/schema/mvc 

        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 一个配置节解决映射器和适配器的配置注解配置 --> 

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 扫描所有的Controller -->

    <context:component-scan base-package="ssm.controller"></context:component-scan>

    <!-- 配置视图解析器 

        进行jsp解析,默认使用jstl标签,classpath下得有jstl的包

    -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

</beans>


现在已经整合成这样了,但是还没有调通,tomcat这边报告service层实现类没有依赖(注入?),具体什么错等明天再查吧,今天太晚了。


返回列表 返回列表
评论

    分享到