引言
Spring5+SpringMVC+MyBatis3+Druid+Thymeleaf...整合
在经历了springboot、springcloud的洗礼之后,突然发现传统的ssm项目竟然已经忘记如何搭建,所以这里做一下整理。
介绍
早年间,学习Spring的时候,是用来搭建传统web服务的。最终项目会被打成一个war包,部署到一个tomcat服务器上。本文就是基于Linux tomcat8部署咱们的ssm项目war包。
大致说明
1、本项目依旧基于maven,不用maven的java项目是没有灵魂的。本项目所依赖的spring jar包不会重复引用,争做极简;
2、本项目基于web容器部署,所以带有传统的web.xml配置,用于web容器访问项目,Servlet分发、Servlet Filter、Listener等;
3、Spring组件创建、依赖注入,关键bean是在Spring相关xml中做的配置,其他的都是基于自动扫描依赖注入;
4、本项目针对多环境配置(dev、pro)有做分析(之前学的都是springboot多环境配置);
5、虽说现在流行前后端分离RESTful风格项目架构,但是本文依旧会带着大家熟悉我们的后端渲染模板引擎JSP、Thymeleaf;
6、基于slf4j整合logback;
创建项目——idea、Java8、maven-archetype-webapp
1、利用工具初始化创建。非核心步骤略...
image.png
2、如果创建出来的项目resources、java...结构不完整,自行添加;
image.png
3、我的项目结构;
image.png
pom依赖
附上一张依赖结构图,让大家清晰知道jar包之间的关系,不要做无谓的jar包依赖
image.png
<!--参数配置-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<spring.version>5.0.8.RELEASE</spring.version>
</properties>
<!--依赖-->
<dependencies>
<!-- 使用SpringMVC需配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--用于spring测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring封装的jdbc数据库访问-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring提供的对AspectJ框架的整合-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Mysql数据库连接客户端-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!--Mybatis提供的用于跟Spring的整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
<!--Mybatis框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--使用fastJSON对spring mvc message-converters升级-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<!--整合实现slf4j的logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.5</version>
</dependency>
<!--druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.23</version>
</dependency>
<!--使用jsp-jstl语法需要的依赖-->
<!--<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>-->
<!--使用thymeleaf的依赖-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
</dependencies>
maven buid组件
<build>
<!--整合mvn package时需要的资源文件夹-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<finalName>app-s1</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
web.xml
<display-name>My Web Application</display-name>
<!--项目门面资源-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring组件配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<!-- spring 多环境配置文件设置 -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>pro,common</param-value>
</context-param>
<!--指定logback配置-->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>classpath:/logback-spring.xml</param-value>
</context-param>
<!-- logback监听器 -->
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 添加SpringMvc servlet配置 -->
<servlet>
<servlet-name>manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>springmvc</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 乱码处理filter -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
核心关注,多环境配置
如果需要切换dev环境,直接修改pro为dev即可,common为公共配置
<!-- spring 多环境配置文件设置 -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>pro,common</param-value>
</context-param>
多环境配置体现——applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<beans profile="pro">
<!-- 加载配置属性文件 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:/config-pro.properties"/>
</beans>
<beans profile="dev">
<!-- 加载配置属性文件 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:/config-dev.properties"/>
</beans>
<beans profile="common">
<!-- 自动加载构建bean -->
<context:component-scan base-package="com.fc.app.s1"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<!--class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:/mappers/**/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:/spring-mybatis.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fc.app.s1.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- - - - - - - spring 声明式事务 - - - - - - - -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--使用AOP注解-->
<aop:aspectj-autoproxy/>
</beans>
</beans>
image.png
image.png
这里区分了dev跟pro的数据库配置
image.png
spring-mvc.xml配置SpringMVC相关组件
<?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.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">
<!-- 自动加载构建bean -->
<!--<context:component-scan base-package="com.fc.app.s1.controller"/>-->
<!-- 启动注解驱动,支持对象与json的转换。 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器二选一 -->
<!-- 视图解析器jsp -->
<!--<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>-->
<!-- 视图解析器thymeleaf -->
<bean id="templateResolver"
class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="1"/>
<property name="templateMode" value="HTML5"/>
<property name="cacheable" value="false"/>
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
<!--静态资源配置-->
<mvc:resources mapping="/static/**" location="/static/" cache-period="0"/>
<!--兼容fastJson升级-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
<value>application/json;charset=UTF-8</value>
<value>application/atom+xml</value>
<value>application/x-www-form-urlencoded</value>
<value>application/octet-stream</value>
<value>application/pdf</value>
<value>application/rss+xml</value>
<value>application/xhtml+xml</value>
<value>application/xml</value>
<value>image/gif</value>
<value>image/jpeg</value>
<value>image/png</value>
<value>text/event-stream</value>
<value>text/html</value>
<value>text/markdown</value>
<value>text/plain</value>
<value>text/xml</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
spring-mybatis.xml配置指定实体包
<configuration>
<!-- 别名 -->
<typeAliases>
<package name="com.fc.app.s1.model"/>
</typeAliases>
</configuration>
logback-spring.xml略....网上有
JSP、Thymeleaf测试
image.png
image.png
打包部署tomcat
mvn package——target目录会生成war包 或
image.png
上传至linux服务器tomcat webapp,如果置于其他目录,需要修改tomcat server.xml 解析目录配置
image.png
<Host name="localhost" appBase="D:\appBase\"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="E:\Devolope\WebRoot"workDir="D:\appBaseRoot\" />
</Host>
appBase代表应用的基础目录,原始默认位置为“webapps”即对应于tomcat根目录下的文件夹webapps;
docBase相当于指定的虚拟目录对应的应用程序的绝对路径;workDir是运行编译成为java二进制代码时候存放的目录。
启动项目远程访问
image.png










网友评论