SSM项目之【千里之行,始于足下】
创建由【SSM】框架搭建的JavaWeb项目的步骤如下:
Ⅰ、创建动态Web项目
Ⅱ、拷贝SSM项目所需的jar包
将前期【SSM项目】所需的【32】个jar包拷贝到【WebContent】——【WEB-INF】——【lib】目录下
![]()
Ⅲ、后台源码模块分化&&前端页面模块分化
![]()
【src_head】存储客户端 【back】后台页面 【src_config】存储配置文件 【head】前端页面 【src_common】存储公共代码 【src_back】存储开发人员代码 【src_demo】是项目模板案例
Ⅳ、配置文件
【Log4J2.xml】、【applicationContext_*.xml】、【jdbc.properties】、【mybatis-config.xml】、【web.xml】
配置文件的位置为:
![]()
Ⅰ、【log4j2.xml】
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <!-- status=debug 可以查看log4j的装配过程 --> <configuration status="off" monitorInterval="1800"><appenders><!-- 定义控制台输出 --><Console name="Console" target="SYSTEM_OUT" follow="true"><PatternLayoutpattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /></Console><!-- 系统打印日志fileName:文件的名字filePattern:文件名字的格式; --><RollingRandomAccessFile name="System"fileName="logs/mvchain/system.log" filePattern="logs/mvchain/system_%d{yyyy-MM-dd}_%i.log"><PatternLayoutpattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /><Policies><!-- 多长时间生成一个文件;默认1天 --><TimeBasedTriggeringPolicy interval="1"modulate="true" /><!-- 多大的文件要切分 --><SizeBasedTriggeringPolicy size="5K" /></Policies></RollingRandomAccessFile><!-- 外部打印日志 --><RollingRandomAccessFile name="Outer"fileName="logs/mvchain/outer.log" filePattern="logs/mvchain/outer_%d{yyyy-MM-dd}_%i.log"><PatternLayoutpattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /><Policies><TimeBasedTriggeringPolicy interval="1"modulate="true" /><SizeBasedTriggeringPolicy size="100M" /></Policies></RollingRandomAccessFile><!-- 线程打印日志 --><RollingRandomAccessFile name="Timer"fileName="logs/mvchain/timer.log" filePattern="logs/mvchain/timer_%d{yyyy-MM-dd}_%i.log"><PatternLayoutpattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /><Policies><TimeBasedTriggeringPolicy interval="1"modulate="true" /><SizeBasedTriggeringPolicy size="100M" /></Policies></RollingRandomAccessFile></appenders><loggers><!-- Root Logger(这个是整个配置文件的入口 --><root level="debug"><!-- 调用定义的log4j配置console:指的是名字;(在这个标签中定义configuration==>appenders这下面标签的name属性 )--><appender-ref ref="Console" /><appender-ref ref="System" /></root><!-- 外部日志,分开日志,name为getLogger()console:指的是名字;(在这个标签中定义configuration==>appenders这下面标签的name属性 )下面定义的多个组合,logger标签中任意组合--><logger name="SystemLog" level="info" additivity="false"><appender-ref ref="Console" /><appender-ref ref="System" /></logger><!-- 外部日志,分开日志,name为getLogger() --><logger name="OuterLog" level="info" additivity="false"><!-- 日志文件要输出到三个地方,控制台,系统文件,外部文件 --><appender-ref ref="Console" /><appender-ref ref="System" /><appender-ref ref="Outer" /></logger><!-- 线程日志,分开日志,name为getLogger() --><logger name="TimerLog" level="info" additivity="false"><!-- 日志文件要输出到三个地方,控制台,系统文件,外部文件 --><appender-ref ref="Console" /><appender-ref ref="System" /><appender-ref ref="Outer" /></logger></loggers> </configuration>
Ⅱ、【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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"default-autowire="byName"><context:property-placeholder location="classpath*:jdbc.properties"/><bean id="dataSource" 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><!-- 如果Spring整合了MyBatis,数据源的配置在MyBatis都不好使,得使用Spring里面的数据源 --><bean id="aa" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis-config.xml"/><property name="mapperLocations" value="classpath*:com/jinghangzz/ssm/*/dao/*Mapper.xml"/></bean><!-- 专门为Dao接口生成Mapper代理类 --><!-- 扫描映射文件,专门为dao接口生成代理类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 告诉Spring,去哪些包里面扫描 --><property name="basePackage" value="com.jinghangzz"/></bean></beans>
Ⅲ、【jdbc.properties】
# MySQL 的数据源 # 驱动 jdbc.driver=com.mysql.cj.jdbc.Driver # URL jdbc.url=jdbc:mysql:///mybatis?serverTimezone=UTC # MySQL的账户名 jdbc.username=root # MySQL的密码 jdbc.password=123456
Ⅳ、【mybatis-config.xml】
Ⅴ、【web.xml】
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>SSM_Demo</display-name><!--···············Spring配置开始位置 ·················--><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring/applicationContext_*.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping><!--···············Spring配置结束位置··················--><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>
Ⅴ、创建工具类
工具类位置
工具类名称 | 作用 |
【ConstatFinalUtil】 | 创建日志记录器 |
【ConstatFinalUtil.java】
package 此处为包名;import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /*** * @Title: ConstatFinalUtil.java * @Package com.jinghangzz.ssm.common.util * @Description: TODO(日志记录器) ** @author 林先生* @date 2019年11月20日 下午4:42:35 * @version*/ public class ConstatFinalUtil {public static final Logger SYS_LOGGER = LogManager.getLogger(); }