本文實例為大家分享了LibrarySystem圖書管理系統第二篇,供大家參考,具體內容如下
第一步:添加數據庫配置文件
jdbc.properties
# 數據庫驅動jdbc.driver=com.mysql.jdbc.Driver # 數據庫地址jdbc.url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8 # 用戶名jdbc.username=root # 密碼jdbc.password=root # 初始化連接initialSize=0 # 最大連接數量maxActive=20 # 最大空閒連接maxIdle=20 # 最小空閒連接minIdle=1 # 超時等待時間maxWait=60000
第二步:添加mybatis配置文件
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"/> <!-- 使用別名替換列名, 默認ture --> <setting name="useColumnLabel" value="true"/> <!-- 開啟駝峰命名轉換--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
第三步:添加Spring配置文件
在resources/spring目錄下新建二個文件:
│ └── 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/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-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"> <!-- 註冊組件掃描器--> <context:component-scan base-package="com.ray.controller"/> <!-- 訪問靜態資源--> <mvc:default-servlet-handler/> <!-- 開啟註解模式--> <mvc:annotation-driven> <mvc:message-converters> <bean> <property name="supportedMediaTypes"> <list> <!-- 解決中文亂碼--> <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> <!-- 視圖解析器--> <bean> <!-- 前綴--> <property name="prefix" value="/WEB-INF/views/"/> <!-- 後綴--> <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.配置數據庫相關參數--> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.配置druid數據源--> <bean id="dataSource" init-method="init" destroy-method="close"> <!-- 配置連接池屬性--> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 配置初始化大小,最小,最大值--> <property name="initialSize" value="1"/> <property name="minIdle" value="1"/> <property name="maxActive" value="10"/> <!-- 配置獲取連接等待超時的時間--> <property name="maxWait" value="10000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉空閒連接,單位毫秒--> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒--> <property name="minEvictableIdleTimeMillis" value="300000"/> <!-- 驗證連接有效與否的SQL,不同的數據配置不同--> <property name="validationQuery" value="SELECT 1" /> <!-- 如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效--> <property name="testWhileIdle" value="true"/> <!-- 這裡建議配置為TRUE,防止取到的連接不可用--> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小--> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- 這裡配置提交方式,默認就是TRUE,可以不用配置--> <property name="defaultAutoCommit" value="true" /> <!-- 開啟Druid的監控統計功能--> <property name="filters" value="stat"/> </bean> <!-- 3.配置Mybatis的SqlSessionFactory對象--> <bean id="sqlSessionFactory"> <!-- 配置MyBatis全局配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 注入數據庫連接池--> <property name="dataSource" ref="dataSource"/> <!-- 掃描配置文件--> <property name="mapperLocations" value="classpath:mapping/*.xml"/> </bean> <!-- 4.配置掃描Dao接口包,動態實現Dao接口,注入到spring容器中--> <bean> <!-- 給出需要掃描Dao接口包--> <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"> <!-- 自動掃描--> <context:component-scan base-package="com.ray"/> <!-- 事務管理--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 開啟事務控制的註解支持--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
第四步:添加logback配置文件
logback配置比log4j要簡單點,功能類似
├── resources
│ ├── logback.xml
在resources文件夾下新建文件: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> <!--開啟debug日誌模式,在控制台打印日誌--> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration>第五步:配置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> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>seckill-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置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-name>seckill-dispatcher</servlet-name> <!-- 默認匹配所有的請求--> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 處理中文亂碼--> <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>
項目結構:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。