使用idea工具开发,用maven进行管理。
常用配置文件:
resources
spring
--spring-dao.xml : spring容器的配置文件,实现框架整合
--spring-web.xml : springMVC的配置文件。
--jdbc.properties : 数据库常量配置
--logback.xml : 日志信息的存放(开发时为debug,上线改为info)
--mybatis-config.xml : mybats的配置文件,整合后 几乎只剩下 “别名”
WEBINF -- web.xml :让配置文件随着项目的启动而加载
1、web.xml 配置说明
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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_3_1.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 1、启动springmvc的前端控制器 ,拦截所有请求 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 启动spring容器 需要加载的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</init-param>
</servlet>
<!-- 配置mvc转发器拦截地址-->
<!-- 关于web.xml的url映射的小知识:-->
<!-- < url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url-->
<!-- < url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、spring-dao.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1、添加约束条件 -->
<!-- 2、开启注解扫描(除了控制器,其他都扫描)-->
<!-- 扫描方式有两种:任选一个-->
<!-- 1、注解扫描是针对已经在Spring容器里注册过的Bean -->
<context:annotation-config>
<!-- 2、不仅具备<context:annotation-config>的所有功能,还可以在指定的package下面扫描对应的bean -->
<context:component-scan base-package="com.sun">
<!--除了描控制器不扫描外 其它全部扫描-->
<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 3、加载jdbc.properties文件,并且配置数据源 -->
<!-- 加载文件两种方式:任选一个-->
<!-- 1、-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2、-->
<!--
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 连接池的属性-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="filters" value="${jdbc.filters}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${jdbc.validationQuery}"/>
<property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
<property name="testOnBorrow" value="${jdbc.testOnBorrow}"/>
<property name="testOnReturn" value="${jdbc.testOnReturn}"/>
<property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}"/>
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${jdbc.maxPoolPreparedStatementPerConnectionSize}"/>
</bean>
<!-- 4、配置sqlSessionFactory (Spring和Mybatis整合) -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBatis全局配置文件:mybatis-config.XML -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.sun.seckill.entity"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置扫描dao接口包,动态实现dao接口,并注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出扫描dao接口包 -->
<property name="basePackage" value="com.sun.seckill.dao"/>
</bean>
<!-- 5、事务控制的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" ></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
3、jdbc.properties 数据库常量配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/text?serverTimezone=CST
jdbc.username=root
jdbc.password=123456
#别名方式,扩展插件,监控统计用的filter:stat,日志用的filter:log4j,防御sql注入的filter:wall
jdbc.filters=stat
#最大连接数
jdbc.maxActive=300
#初始化连接数
jdbc.initialSize=2
#获取连接最大等待时间
jdbc.maxWait=60000
#最小连接数
jdbc.minIdle=1
#检测连接有效性的时间间隔
jdbc.timeBetweenEvictionRunsMillis=60000
#连接保持空闲而不被驱逐的最长时间
jdbc.minEvictableIdleTimeMillis=300000
#连接有效性,检测sql
jdbc.validationQuery=SELECT 'x'
#定时检测空闲连接有效性
jdbc.testWhileIdle=true
#检测获取的连接的有效性
jdbc.testOnBorrow=false
#检测要归还的连接的有效性
jdbc.testOnReturn=false
#是否缓存preparedStatement,即PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
jdbc.poolPreparedStatements=false
jdbc.maxPoolPreparedStatementPerConnectionSize=50
4、mybatis-config.xml 配置说明
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局属性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys 获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名 -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
5、spring-web.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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 1、配置springMvc -->
<!--注解驱动注册-->
<!-- 简化配置:
1.自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
2.默认提供一系列功能:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat,xml,json默认读写支持.
-->
<mvc:annotation-driven />
<!--静态资源可以交由默认的Servlet处理-->
<!-- servlet-mapping 映射的路径:"/" -->
<!-- 静态资源配置
1.加入对静态资源的处理:js,gif,png
2.允许使用"/"做整体映射
-->
<mvc:default-servlet-handler />
<!--2、配置视图解析器-->
<bean 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"/>
</bean>
<!-- 3、扫描controller-->
<context:component-scan base-package="com.sun.seckill" />
</beans>
6、logback.xml 配置文件说明
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<!-- 控制台日志输出格式-->
<!--详细介绍logback配置链接-->
<!--https://blog.csdn.net/wt520it/article/details/85633188-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="chapters.configuration" level="INFO"/>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>