更新時間:2022-05-18 09:55:21 來源:動力節點 瀏覽1879次
學習框架的時候,一開始最煩的就是一些配置文件,有很多東西需要配置。
Web.xml 文件
<?xml 版本="1.0" 編碼="UTF-8"?>
<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"
版本="3.1">
<!-- 配置加載Spring文件的監聽器 -->
<上下文參數>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<聽眾>
<listener- class >org.springframework.web.context.ContextLoaderListener</listener- class >
</listener>
<!-- 編碼過濾器-->
<過濾器>
<filter-name>編碼</filter-name>
<filter- class >org.springframework.web.filter.CharacterEncodingFilter</filter- class >
<初始化參數>
<param-name>編碼</param-name>
<param-value>UTF-8</param-value>
</init-param>
</過濾器>
<過濾器映射>
<filter-name>編碼</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端核心控制器-->
<小服務程序>
<servlet 名稱>crm</servlet 名稱>
<servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
<初始化參數>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 配置服務器啟動后立即加載Spring MVC配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet 映射>
<servlet 名稱>crm</servlet 名稱>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<歡迎文件列表>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Springmvc的配置文件springmvc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc ="http://www .springframework.org/schema/mvc"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
網址:www.springframework.org/schema/beans/spring-beans-4.3.xsd
http:www.springframework.org/schema/mvc
http:www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http:www.springframework.org/schema/context
http:www.springframework.org/schema/context/spring-context-4.3.xsd
http:www.springframework.org/schema/aop
http:www.springframework.org/schema/aop/spring-aop-4.3.xsd
http:www.springframework.org/schema/tx
http:www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 加載屬性文件 -->
<context:property-placeholder location="classpath:resource.properties"/>
<!-- 配置掃描儀-->
<context:component-scan base- package ="com.ma.core.web.controller"/>
<!-- 注解驅動:配置處理器映射器和適配器 -->
<mvc:annotation-driven/>
<!--配置靜態資源的訪問映射,此配置中的文件不會被前端控制器攔截-->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/fonts/**" location="/fonts/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<!-- 配置視圖解釋器ViewResolver -->
<bean id="jspViewResolver"類="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</豆>
<!--配置攔截器-->
<mvc:攔截器>
<mvc:攔截器>
<mvc:mapping path="/**"/>
<bean class ="com.ma.core.interceptor.LoginInterceptor"/>
</mvc:攔截器>
</mvc:攔截器>
</beans>
ApplicationContext.xml 文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc ="http://www .springframework.org/schema/mvc"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
網址:www.springframework.org/schema/beans/spring-beans-4.3.xsd
http:www.springframework.org/schema/mvc
http:www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http:www.springframework.org/schema/context
http:www.springframework.org/schema/context/spring-context-4.3.xsd
http:www.springframework.org/schema/aop
http:www.springframework.org/schema/aop/spring-aop-4.3.xsd
http:www.springframework.org/schema/tx
http:www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--讀取 db.properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置數據源-->
<bean id="dataSource"類="org.apache.commons.dbcp2.BasicDataSource">
<!--數據庫驅動器-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--連接數據庫的URL-->
<property name="url" value="${jdbc.url}"/>
<!--連接數據庫的用戶名-->
<property name="username" value="${jdbc.username}"/>
<!--連接數據庫的密碼-->
<property name="password" value="${jdbc.password}"/>
<!--最大連接數-->
<property name="maxTotal" value="${jdbc.maxTotal}"/>
<!--最大空閑數-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--初始連接號-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</豆>
<!-- 事務管理器,依賴于數據源 -->
<bean id="transactionManager"類="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</豆>
<!--注意事項-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:屬性>
<!--通信行為-->
<tx:method name="save*" 傳播="REQUIRED"/>
<tx:method name="插入*" 傳播="REQUIRED"/>
<tx:method name="add*" 傳播="REQUIRED"/>
<tx:method name="create*" 傳播="REQUIRED"/>
<tx:method name="delete*" 傳播="REQUIRED"/>
<tx:method name="update*" 傳播="REQUIRED"/>
<tx:method name="find*" 傳播="SUPPORTS" 只讀="true"/>
<tx:method name="select*" 傳播="SUPPORTS" 只讀="true"/>
<tx:method name="get*" 傳播="SUPPORTS" 只讀="true"/>
</tx:屬性>
</tx: 建議>
<!--切面條-->
<aop:配置>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ma.core.service.*.*(..))"/>
</aop:config>
<!--配置MyBatis工廠SqlSessionFactory-->
<bean id="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入數據源-->
<property name="dataSource" ref="dataSource"/>
<!--指定mapper文件的位置-->
<property name="mapperLocations" value="classpath:mybatis/sqlmap/*.xml"/>
<!--指定MyBatis的核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</豆>
<!-- 接口開發,掃描com.itheima.core.dao包,可以掃描到這個包里寫的接口 -->
<bean類="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ma.core.dao"/>
</豆>
<!-- 配置掃描@Service注解-->
<context:component-scan base- package ="com.ma.core.service"/>
</beans>
Mybatis 配置文件 mybatis-config.xml
<?xml 版本="1.0" 編碼="UTF-8" ?>
<!DOCTYPE 配置 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<配置>
<!-- 別名定義-->
<類型別名>
<包名="com.ma.core.po"/>
</typeAliases>
</配置>
Db.properties 文件
jdbc.driver= com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql:localhost:3306/test
jdbc.username= root
jdbc.password =
jdbc.maxTotal =30
jdbc.maxIdle =10
jdbc.initialSize =5
Log4j.properties 文件
# 全局日志配置
log4j.rootLogger =錯誤,標準輸出
# MyBatis 日志配置...
log4j.logger.com.ma.core =調試
# 控制臺輸出...
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n
以上是SSM框架的基本配置文件。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習