基础篇——搭建SSM框架配置总结

搭建SSM框架难点主要在于四个xml的配置文件:

    spring.xml配置文件的主要功能是操作后端数据库,怎么操作呢,通过在Service层注入接口dao来操作,首先配置数据源获得一个connection连接数据库,dao的注入需要接口与mapper.xml映射,这样就不需要自己再写dao的实现类,注入dao的时候,属性命名要与接口名相同首字母小写,然后用dao可以进行增删改查。

    //spring.xml头文件

image

    //扫描service实现类包

image

    //自动注入

image

    //导入资源文件,配置数据源

image

    //注册sqlSession

image

    //映射接口

image

    //配置事务

image

    spring-mybatis.xml配置文件的主要功能是设置别名

    //spring-mybatis.xml头文件

image

    //设置别名

image

    spring-mvc.xml配置的主要功能是前端视图层的返回,主要通过在controller层注入service接口进行与后台连接获取数据在controller层,这个service的注入需要自己写实现类,需要在实现类上标注@Service才能进行自动装配。

    //spring-mvc.xml头文件

image

    //配置自动扫描的包

image

    //自动注入

image

    //配置视图解析器

image

    //配置处理静态资源的Servlet

image

    //配置mvc:annotation-driven

image

    web.xml文件的主要功能拦截客户端的所有请求,交给上面的springmvc处理,容器加载spring.xml配置文件。

    //web.xml文件的头文件

image

    //加载spring.xml(applicationContext.xml)

image

    //加载spring-mvc.xml,将请求交给它处理

image

    //过滤器filter配置编码格式

image

1、spring.xml中注入的SqlSesssionFactory类在mybatis包中而不是在spring包中。

2、mybatis中的trim标签中的prefixOverride是去掉第一个前缀,suffixOverride是去掉最后一个后缀。

3、spring整合mybatis时在spring.xml配置文件中需要

    //配置数据源dataSource,class=“org.springframework.jdbc.dataSource.DriverManagerDataSource”

    //配置sqlSessionFactory,class=“org.mybatis.spring.SqlSessionFactoryBean”,属性property=“dataSource”,property=“mappingLocations”。

    //配置动态映射mapping与dao,class=“org.mybatis.spring.mapper.MapperScannerConfigurer”属性property=“basePackage”,property=“SqlSessionFactoryBeanName”

4、加载属性文件的配置<context:property-placeholder location=”classpath:jdbc.properties”。

5、配置数据源可以直接用org.springframework.jdbc.datasource.DriverManagerDataSource类

6、配置数据源加载,创建SessionFactory时需要SqlSessionFactoryBean,类中有两个属性property,一个是dataSource数据连接,另一个是mapping映射xml文件的路径。

7、配置springMvc的视图解析器主要用到org.springframework.web.servlet.view.InternalResourceViewResolver

8、加载spring容器时通过监听器listener实现的,org.springframework.web.context.ContextLoaderListener。

9、新建一个web项目,顶行出现红叉,可以取消xml文件验证的build校验。

10、spring-mvc.xml,spring.xml,spring-mybatis.xml文件加载都用classpath:*.xml。

11、classpath路径在项目的classes包下,classes包在build包下,如果没有build包则默认在web-inf包下

12、完整的拷贝一个项目的注意事项:1 新建web空项目,2 复制WebContext下的内容覆盖空项目中的WebContext,3 将src下的内容覆盖空项目中的src,4 复制conf中的文件复制到src下。

13、自动扫描包<context:component-scan base-package=”com.zzb.www.controller”/>

14、自动装配<context:annotation-config/>

原文地址:https://www.cnblogs.com/zzb-yp/p/9295397.html