SSM框架整合:各种配置文件的整合和详解

SSM框架整合:各种配置文件的整合和详解

SSM框架整合:各种配置文件的整合和详解

前言

学习了ssm框架的整合之后,对于数量众多的配置文件,和各种不同的配置方式感到甚是头疼,接下来教给大家一个清晰明白的配置,分门别类的配置不同的xml文件。

项目目录

名称作用mappermybatis映射文件springspring家族配置文件=context为父(全局性)+mybatis整合+transaction事务+mvcconfigdruid配置文件log4jlog4j.properties文件webapp前端页面项目依赖

junit

junit

4.13

test

org.springframework

spring-web

5.1.6.RELEASE

org.springframework

spring-context

5.1.6.RELEASE

org.springframework

spring-webmvc

5.1.6.RELEASE

org.springframework

spring-jdbc

5.1.6.RELEASE

org.springframework

spring-aspects

5.1.6.RELEASE

org.springframework

spring-test

5.1.6.RELEASE

com.baomidou

mybatis-plus

3.3.2

mysql

mysql-connector-java

5.1.49

org.projectlombok

lombok

1.16.20

log4j

log4j

1.2.17

com.github.pagehelper

pagehelper

5.1.10

com.alibaba

druid

1.1.10

com.fasterxml.jackson.core

jackson-databind

2.9.8

javax.servlet

servlet-api

2.5

provided

javax.servlet

jsp-api

2.0

provided

这是整个ssm项目的所有依赖,其中有些依赖不是必须的,可以适量删减。

NOTE: 在这里重点强调,如果导入了苞米豆的mybatis-plus依赖,就不要再导入mybatis依赖和mybatis-spring依赖,因为mp对mybatis-spring其中的Configuration类进行了继承和修改,如果重新配置会报错!!!这都是博主踩过的坑啊,o(╥﹏╥)o

配置文件

web.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

contextConfigLocation

classpath:spring/spring-context.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

dispatchServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/spring-mvc.xml

1

dispatchServlet

/

解释一哈:

Listener:这是创建父子容器最关键的一步,contextConfigLocation配置只是告诉让listener去哪里找这个配置文件,而listener会把这个扫描的文件做为父容器,在spring底层有一个方法,把spring-context容器设置为spring-mvc容器的父容器,具体哪个方法我忘了,有兴趣的可以问度娘。dispatcherServlet: 是所有请求的分配者,其中主要的方法为doDispatch主要把前端来的请求,或其他方法的结果调度给不同的方法。

druid 数据库连接池

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/openapi?useSSL=false&useUnicode=true&characterEncoding=utf8

jdbc.username=root

jdbc.password=147258

initialSize=10

maxActive=30

minIdle=5

maxWait=5000

log4j

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# MyBatis logging configuration...

log4j.logger.org.mybatis.example.BlogMapper=TRACE

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%nxxxxxxxxxx # Global logging configurationlog4j.rootLogger=DEBUG, stdout# MyBatis logging configuration...log4j.logger.org.mybatis.example.BlogMapper=TRACE# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%nstatus = errorname = PropertiesConfigfilters = thresholdfilter.threshold.type = ThresholdFilterfilter.threshold.level = debugappenders = consoleappender.console.type = Consoleappender.console.name = STDOUTappender.console.layout.type = PatternLayoutappender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%nrootLogger.level = debugrootLogger.appenderRefs = stdoutrootLogger.appenderRef.stdout.ref = STDOUT123456789101112131415161718p

log4j.properties文件必须要放在resources文件夹下,不要放子文件下,因为日志默认会去.class文件的根目录下去寻找,这也是博主踩过的坑啊!!!

重点!spring配置文件整合

老大哥:全局spring-context文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

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

除了子容器中的@controller注解,其他注解全部扫描到父容器中,在这里导入了spring-context-mybatis.xml文件和spring-context-transaction.xml文件,使条例更清晰,维护更方便。

二哥:spring-context-mybatis整合

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

com/xxx/master/mapper/*.xml

这个文件把之前的mybatis文件和spring进行了整合,之前的配置全部放到了MybatisSqlSessionFactoryBean工厂配置中,在这里用的SqlSessionFactoryBean为mybatis-plus的bean工厂,如果使用了mybatis-plus的工厂bean,那么它的configuration必须为MybatisConfiguration。

日志在MybatisConfiguration的bean下进行配置,也可以使用mybatis的标准输出日志StdOutImpl。

三哥:事务处理

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

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

事务推荐使用注解开发,如果service比较多可以使用配置的方式,在这里使用了注解开发。建议使用CGlib的动态代理,jdk的动态代理如果根据类对象来获得bean会出错,这也是踩过的坑啊o(╥﹏╥)o

儿子:mvc

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"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

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

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

子容器只扫描@controller注解,使用default-servlet解决静态资源无法访问的问题,个人感觉这样配置是最简单方便的。

为什么要这么配?->父子容器问题

理论

有没有发现一个巨大的问题:为什么要把controller单独拉出来,单独进行扫描呢?一次性扫描完了,不就完事了?还这么费劲?这里一个比较重要的概念:父子容器,父亲(Spring) 儿子(mvc)。在spring和spring-mvc中,分别是父容器和子容器,父容器不能访问子容器的bean,子容器可以访问父容器的bean,为什么要这样配呢,配置一个容器不是更方便呢?如果配置一个容器,很难达到分门别类的效果,而且会很冗余,而且从逻辑上来说service不应该调用controller,因此采用了父子容器的方式。

操作

第一种就是我们固定包扫描不全局扫描第二种,我已经启用了全局扫描,这个时候可以通过include-filter和exclude-filter进行包含和剔除,就像儿子mvc中写的那样也能完成~

几点建议:

自动注入建议采用@resource注解,不采用@Autowired注解,因为@resource为基于name来注入,如果找不到再基于type注入,而且最重要的原因是idea不会在编写的时候出现报错。

分页插件建议使用pagehelper,不用mybatis-plus的分页。

建议将mybatis的原始配置文件放到spring中,整合成spring-context-mybatis.xml文件,减少了配置文件的数量,而且清晰明了,不会有配置文件的冗余。

建议使用log4j的日志,而不是StdOut的日志,因为log4j通过文件配置的方式,打印的信息更全面,更整齐。

总结

最重要的是各种配置文件的配置方式,以及什么配置应该放在哪个文件夹下。

一定要深刻理解父子容器的概念。

让我耿耿于怀的是log4j.properties文件不能放到conf文件夹下,让有强迫症的我很是难受啊o(╥﹏╥)o,如果有大神知道如何修改日志的默认查找路径,还请告知,不胜感激

相关推荐

社保最低档次一个月交多少钱?以四个城市为例
DNF国服2月1日起源版本十大绝版改动内容介绍?
粉体学基础知识一:粒径和粒度分布
365bet赌场官网

粉体学基础知识一:粒径和粒度分布

07-04 🌱 4502