2.web.xml配置文件css
<!--配置訪問默認主頁 -->html
<welcome-file-list>java
<welcome-file>/public/index.html</welcome-file>mysql
</welcome-file-list>web
<!-- spring與web的整合讀取applicationContext.xml -->redis
<context-param>spring
<param-name>contextConfigLocation</param-name>sql
<param-value>classpath:applicationContext.xml</param-value>數據庫
</context-param>express
<!-- 監聽器(用於建立ac容器) spring提供ContextLoaderListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 核心控制器配置(springMVC) 讀取springMVC配置文件-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!-- 啓動加載-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 過濾器(解決中文亂碼問題) spring提供CharacterEncodingFilter-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- woff等字體文件一直報404錯誤的解決-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff2</url-pattern>
</servlet-mapping>
<!-- 配置靜態文件不走核心控制器-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>ico</extension>
<mime-type>image/x-icon</mime-type>
</mime-mapping>
<!-- 掃包注入(service層) -->
<!-- <context:component-scanbase-package="com.huapu.dao"></context:component-scan>-->
<context:component-scanbase-package="com.huapufinance.service.impl"></context:component-scan>
<!-- 讀取jdbc.properties文件 -->
<context:property-placeholderlocation="classpath:jdbc.properties"/>
<!-- 使用第三方數據源 -->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"value="${driverClassName}"></property>
<propertyname="url"value="${url}"></property>
<propertyname="password"value="${password}"></property>
<propertyname="username"value="${dbUsername}"></property>
<propertyname="maxActive"value="${maxActive}"></property>
<propertyname="minIdle"value="${minIdle}"></property>
<propertyname="maxWait"value="${maxWait}"></property>
<propertyname="testWhileIdle"value="true"/>
<propertyname="testOnBorrow"value="false"/>
<propertyname="testOnReturn"value="false"/>
<propertyname="validationQuery"value="select 1"/>
<propertyname="timeBetweenEvictionRunsMillis"value="60000"/>
<propertyname="numTestsPerEvictionRun"value="${maxActive}"/>
</bean>
<!-- spring與mybatis整合 -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!--若是使用mybatis核心配置文件 -->
<!-- <property name="configLocation"value="classpath:mybatis-config.xml"></property> -->
<!--數據源 -->
<propertyname="dataSource"ref="dataSource"></property>
<!--實體類(映射文件) -->
<propertyname="mapperLocations"value="classpath:com/huapufinance/mapper/*Mapper.xml"></property>
<!--包別名定義 -->
<propertyname="typeAliasesPackage"value="com.huapufinance.entity"></property>
</bean>
<!-- 建立dao層實例將其放入IOC容器中 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- session -->
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"></property>
<!--接口 -->
<propertyname="basePackage"value="com.huapufinance.dao"></property>
</bean>
<!-- 事物方式三:採用命名空間 -->
<!-- <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource"></property>
</bean>
<tx:adviceid="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcutexpression="execution(* com.huapu.service.impl.*ServiceImpl.*(..))"id="pc" />
<aop:advisoradvice-ref="transactionAdvice" pointcut-ref="pc"/>
</aop:config>-->
<!-- redis配置(與數據庫配置相似) -->
<beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="90"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="1000"/>
<property name="testOnBorrow" value="true"/>
</bean>
<beanid="jedisPool"class="redis.clients.jedis.JedisPool">
<constructor-argref="jedisPoolConfig"/>
<constructor-argvalue="127.0.0.1"/>
<constructor-argvalue="6379"/>
</bean>
<beanid="jedisAPI"class="com.huapufinance.utils.RedisAPI">
<propertyname="jedisPool"ref="jedisPool"></property>
</bean>
<!-- 註解方式配置事物 -->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"></property>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
3、spring整合Hibernateauth="Container"
type="javax.sql.DataSource"
factroy="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/db_book?useUnicode=true&characterEncoding=UTF-8"
username="root"
②須要在web工程中引用該資源(web.xml)password="123456" />
<res-ref-name>hibernate/dataSource</res-ref-name></resource-ref>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<property name="jndiName" value="java:comp/env/hibernate/dataSource"></property></bean>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/db_book?useUnicode=true&characterEncoding=UTF-8</value>
</property></bean>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="dataSource" ref="dataSource"></property></bean>
<property name="driverClassName" value="${driverClassName}"></property></bean>
<property name="url" value="${url}"></property>
<property name="password" value="${password}"></property>
<property name="username" value="${dbUsername}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="minIdle" value="${minIdle}"></property>
<property name="maxWait" value="${maxWait}"></property>
<property name="target" ref="loginServiceImpl"></property> //目標類
<property name="proxyInterfaces"> //目標類的實現接口列表
<list>
<value>com.itany.service.LoginService</value>
</list>
</property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props><prop key="login">PROPAGATION_SUPPORTS</prop><prop key="regist">PROPAGATION_REQUIRED</prop><prop key="select*">PROPAGATION_SUPPORTS</prop><prop key="query*">PROPAGATION_SUPPORTS</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED</prop></props>
</property></bean>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="login">PROPAGATION_SUPPORTS</prop><prop key="regist">PROPAGATION_REQUIRED</prop><prop key="select*">PROPAGATION_SUPPORTS</prop><prop key="query*">PROPAGATION_SUPPORTS</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property></bean>
<property name="beanNames">
<list>
<value>*ServiceImpl</value>
</list></property>
<property name="interceptorNames"><list>
<value>transactionAdvice</value>
</list>
</property></bean>
<tx:attributes></tx:advice><tx:method name="login" propagation="SUPPORTS"/> //代理方法login<tx:method name="regist" propagation="REQUIRED"/> //代理方法regist<tx:method name="*" propagation="REQUIRED"/> //全部方法</tx:attributes>
<aop:pointcut expression="execution(* com.itany.service.impl.*ServiceImpl.*(..))" id="pc"/></aop:config>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pc"/>