ssm的配置檔案

XiangdxDu發表於2024-06-30

1.jdbc.properties檔案

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/sms2021
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.passwd=root

2.applicationContext.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:aop="http://www.springframework.org/schema/aop" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--Spring配置檔案, 這裡主要配置和業務邏輯有關的-->
<!--資料來源、事務控制、... -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<!--1.資料來源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.passwd}"/>
</bean>

<!--2.掃描業務邏輯元件-->
<context:component-scan base-package="com.wxl.sms">
<!--除了控制器其它的都要-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--3.配置Spring整合MyBatis-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--MyBatis全域性配置檔案位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<!--指定MyBatis的mapper檔案位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

<!--配置一個可以執行批次的sqlSession-->
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
<!-- <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>-->
<!-- <constructor-arg name="executorType" value="BATCH"/>-->
<!-- </bean>-->

<!--4.配置掃描器, 將MyBatis介面的實現加入到IOC容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--掃描所有的dao介面的實現加入到IOC容器中-->
<property name="basePackage" value="com.wxl.sms.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>

<!--5.事務控制-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制住資料來源-->
<property name="dataSource" ref="dataSource"/>
</bean>

<!--6.開啟基於註解的事務/XML配置的事務(重要的都是使用配置)-->
<aop:config>
<!--切入點表示式-->
<aop:pointcut id="txPoint" expression="execution(* com.wxl.sms.service..*(..))"/>
<!--配置事務增強-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

<!--配置事務增強(事務如何切入)-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事務方法-->
<tx:method name="*"/>
<!--以get開始的所有方法(認為是查詢, 可以調優)-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>

<tx:annotation-driven/>

<!--Spring配置檔案的核心
1.資料來源
2.整合MyBatis
3.事務控制
-->

<!-- <bean class="org.springframework.web.filter.CharacterEncodingFilter">-->
<!-- </bean>-->
</beans>

3.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>
<!--設定MyBatis輸出日誌-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--開啟駝峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>

4.springmvc.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: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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--SpringMVC的配置檔案, 包含網站跳轉邏輯的控制, 配置-->
<context:component-scan base-package="com.wxl.sms" use-default-filters="false">
<!--只掃描控制器-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--配置檢視解析器, 方便頁面返回解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page/"/>
<property name="suffix" value=".jsp"/>
</bean>

<!--兩個標準配置-->
<!-- 將SpringMVC不能處理的請求交給Tomcat, 實現動態、靜態資源都能訪問成功 -->
<mvc:default-servlet-handler/>
<!--能支援SpringMVC更高階的功能, 比如JSR303校驗, 快捷的AJAX請求... 對映動態請求-->
<mvc:annotation-driven/>

</beans>

相關文章