Project directory structure:
configuration file
1. The configuration file spring-web.xml of 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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Add annotation driver--> <mvc:annotation-driven/> <!-- Default scan package path--> <context:component-scan base-package="com.ray.web"/> <!-- Filter static file interception--> <mvc:default-servlet-handler/> <!-- Define the prefix and suffix of the jumped file--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2. Spring integrates mybatis configuration file 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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- Configure the process of integrating mybatis--> <!-- 1. Configure the properties of database-related parameters properties: ${url} --> <!-- Use database configuration file decoupling--> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- The following druid configurations are all basic configurations, the specific optimization settings can be queried online, or you can search for druid directly on github --> <!-- 2. Database connection pool --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- Configure connection pool properties--> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- Configure initialization size, minimum, maximum --> <property name="initialSize" value="1"/> <property name="minIdle" value="1"/> <property name="maxActive" value="10"/> <!-- Configure the time for getting a connection to wait for timeout--> <property name="maxWait" value="10000"/> <!-- How long is the configuration interval to perform detection, and detect idle connections that need to be closed, in milliseconds--> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- Configure the minimum lifetime of a connection in the pool, in milliseconds--> <property name="minEvictableIdleTimeMillis" value="300000"/> <!-- The SQL to verify whether the connection is valid or not, different data configurations are different--> <property name="validationQuery" value="SELECT 1" /> <!-- If the idle time is greater than timeBetweenEvictionRunsMillis, execute validationQuery to check whether the connection is valid --> <property name="testWhileIdle" value="true"/> <!-- It is recommended to configure TRUE here to prevent the obtained connection from being unavailable--> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="false"/> <!-- Open PSCache and specify the size of PSCache on each connection--> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- The submission method is configured here, the default is TRUE, you can configure it without configuration--> <property name="defaultAutoCommit" value="true" /> <!-- Enable Druid's monitoring and statistics function--> <property name="filters" value="stat,log4j2"/> <!-- com.alibaba.druid.filter.Filter --> <property name="proxyFilters"> <list> <ref bean="log-filter"/> </list> </property> </bean> <!-- Druid configuration above --> <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4j2Filter"> <!-- Indicates whether to connect all Connection-related logs --> <property name="connectionLogEnabled" value="false"/> <!-- Indicates whether to connect all Statement-related logs --> <property name="statementLogEnabled" value="false"/> <!-- Indicates whether to display the result set --> <property name="resultSetLogEnabled" value="true"/> <!-- Indicates whether to display the SQL statement --> <property name="statementExecutableSqlLogEnable" value="true"/> </bean> <!-- 3. Configure the Mybatis SqlSessionFactory object --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- Configure MyBaties global configuration file: mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- Inject database connection pool--> <property name="dataSource" ref="dataSource"/> <!-- Scan entity packages, use aliases --> <property name="typeAliasesPackage" value="com.ray.entity"/> <!-- Scan sql configuration file: xml file required by mapper--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 4. Configure and scan the Dao interface package, dynamically implement the Dao interface, and inject it into the spring container --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- Give the Dao interface package that needs to be scanned --> <property name="basePackage" value="com.ray.dao"/> </bean> </beans>
3. Database connection parameter configuration jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
4, mybatis configuration 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> <!-- Configure global properties--> <settings> <!-- Use jdbc's getGeneratedKeys to get the database self-incrementing primary key value --> <setting name="useGeneratedKeys" value="true" /> <!-- Replace column names with column aliases Default: true --> <setting name="useColumnLabel" value="true" /> <!-- Turn on camel case name conversion: Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
5. Log4j2 configuration file log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Log4j 2.x configuration file. Automatically check and apply configuration file updates every 30 seconds; --> <configuration status="warn" monitorInterval="30" strict="true" schema="Log4J-V2.2.xsd"> <Properties> <property name="LOG_HOME">logs</property> <property name="FILE_NAME">test</property> </Properties> <appenders> <!-- output to console --> <console name="Console" target="SYSTEM_OUT"> <!-- The level to be logged --> <!-- <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" /> --> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level [%C{36}.%M] - %msg%n"/> </console> <!-- Circular file log output --> <RollingRandomAccessFile name="running-log" fileName="${LOG_HOME}/${FILE_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="[%-5p] %d %c - %m%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="60 MB" /> </Policies> <DefaultRolloverStrategy max="20" /> </RollingRandomAccessFile> </appenders> <loggers> <!-- Global configuration--> <root level="info"> <appenderRef ref="Console"/> <appenderRef ref="running-log"/> </root> <logger name="org.springframework.web" level="debug" additivity="false"> <appenderRef ref="Console"/> </logger> <logger name="com.mc.core.service" level="debug" additivity="false"> <appender-ref ref="Console"/> </logger> <!-- druid configuration--> <logger name="druid.sql.Statement" level="debug" additivity="false"> <appender-ref ref="Console"/> <appender-ref ref="running-log"/> </logger> <logger name="druid.sql.ResultSet" level="debug" additivity="false"> <appender-ref ref="Console"/> <appender-ref ref="running-log"/> </logger> </loggers> </configuration>
6. The final web.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Alibaba data source BENGIN --> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!-- Ali data source END --> <!-- spring4 mvc BEGIN --> <servlet> <servlet-name>springMVC</servlet-name> <!-- mvc调度器 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure the configuration file that springMVC needs to load spring-dao.xml, spring-service.xml, spring-web.xml Mybatis - > spring -> springmvc --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- Matches all static resources by default, if the configuration is wrong here, error 500 will be generated --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- spring4 mvc END --> <!-- Log4j2 BEGIN --> <listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener> <filter> <filter-name>log4jServletFilter</filter-name> <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class> </filter> <filter-mapping> <filter-name>log4jServletFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping> <!-- Log4j2 END --> <!-- Spring character set filter BENGIN--> <filter> <filter-name>SpringEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring character set filterEND--> <!-- System Home--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
operation result
After starting the project, enter: http://localhost:8080/project name/druid/index.html in the web browser to check whether druid is configured successfully.