This article shares the second article of LibrarySystem Library Management System for your reference. The specific content is as follows
Step 1: Add the database configuration file
jdbc.properties
# Database driver jdbc.driver=com.mysql.jdbc.Driver # Database address jdbc.url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8 # Username jdbc.username=root # Password jdbc.password=root # Initialize connection initialSize=0 # Maximum number of connections maxActive=20 # Maximum idle connection maxIdle=20 # Minimum idle connection minIdle=1 # Timeout waiting time maxWait=60000
Step 2: Add mybatis configuration file
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 primary key --> <setting name="useGeneratedKeys" value="true"/> <!-- Replace column names with an alias, default ture --> <setting name="useColumnLabel" value="true"/> <!-- Turn on camel naming conversion--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
Step 3: Add Spring Configuration File
Create two new files in the resources/spring directory:
│ └── spring
│ ├── spring-mybatis.xml
│ ├── spring-service.xml
│ └── spring-mvc.xml
spring-mvc.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:context="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- Register component scanner --> <context:component-scan base-package="com.ray.controller"/> <!-- Access static resources --> <mvc:default-servlet-handler/> <!-- Enable annotation mode --> <mvc:annotation-driven> <mvc:message-converters> <bean> <property name="supportedMediaTypes"> <list> <!-- Solve Chinese garbled--> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- View parser--> <bean> <!-- Prefix--> <property name="prefix" value="/WEB-INF/views/"/> <!-- Suffix--> <property name="suffix" value=".jsp"/> </bean> </beans>
spring-mybatis.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. Configure database-related parameters --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2. Configure druid data source--> <bean id="dataSource" 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 the initialization size, minimum, maximum value--> <property name="initialSize" value="1"/> <property name="minIdle" value="1"/> <property name="maxActive" value="10"/> <!-- Configure the time to get the connection waiting timeout --> <property name="maxWait" value="10000"/> <!-- Configure how long it takes to perform a detection interval, and the detection requires closing the idle connection in milliseconds--> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- Configure the minimum time for a connection to survive in the pool, in milliseconds--> <property name="minEvictableIdleTimeMillis" value="300000"/> <!-- SQL that verify that the connection is valid, different data configurations are different --> <property name="validationQuery" value="SELECT 1" /> <!-- If the idle time is greater than timeBetweenEvictionRunsMillis, perform validationQuery to detect whether the connection is valid-> <property name="testWhileIdle" value="true"/> <!-- It is recommended to configure it as TRUE here to prevent the retrieved connection from being unavailable --> <property name="testOnBorrow" value="true"/> <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"/> <!-- Configure the submission method here, the default is TRUE, you can do without configuration --> <property name="defaultAutoCommit" value="true" /> <!-- Enable Druid's monitoring and statistics function --> <property name="filters" value="stat"/> </bean> <!-- 3. Configure Mybatis' SqlSessionFactory object --> <bean id="sqlSessionFactory"> <!-- Configure MyBatis global configuration file --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- Inject database connection pool --> <property name="dataSource" ref="dataSource"/> <!-- Scan the configuration file --> <property name="mapperLocations" value="classpath:mapping/*.xml"/> </bean> <!-- 4. Configure the scanned Dao interface package, dynamically implement the Dao interface, and inject it into the spring container --> <bean> <!-- Give the Dao interface package that needs to be scanned --> <property name="basePackage" value="com.ray.dao"/> </bean> </beans>spring-service.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Automatic Scan --> <context:component-scan base-package="com.ray"/> <!-- Transaction Management --> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- Turn on annotation support for transaction control --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
Step 4: Add logback configuration file
The logback configuration is simpler than log4j, and the functions are similar
├── resources
│ ├── logback.xml
Create a new file in the resources folder: logback.xml
<?xml version="1.0" encoding="UTF-8" ?> <configuration debug="true"> <appender name="STDOUT"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!---Enable debug log mode and print logs in the console--> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration>Step 5: Configure web.xml
web.xml
<web-app 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" version="3.1" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name> <!-- Configure DispatcherServlet --> <servlet> <servlet-name>seckill-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configuration file that needs to be loaded for springMVC 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> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name>seckill-dispatcher</servlet-name> <!-- Match all requests by default --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Handle Chinese garbled--> <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> </web-app>
Project structure:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.