ssm框架配置⽂件详解,(分模块配置)
通过创建⼀个web⼯程,则经过修改之后会是如下的⽬录结构:
杨戬电影为什么杀申公豹每个⽬录的含义我就不⼀⼀介绍了,这不是本次讨论的重点,创建好之后,我们在/main/resources资源⽬录下加⼊以下配置⽂件:
⼀共6个,最后加上⼀个web.的配置,就OK了,现在来详解这些配置⽂件的内容:
空⽂件即可:
1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN"
3"/dtd/mybatis-3-config.dtd">
4<configuration>
5
6</configuration>
需要配置:
加载properties⽂件,数据源,SqlSessionFactory,Mapper扫描:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="/schema/beans"
3 xmlns:context="/schema/context" xmlns:p="/schema/p"
4 xmlns:aop="/schema/aop" xmlns:tx="/schema/tx"
5 xmlns:xsi="/2001/XMLSchema-instance"
6 xsi:schemaLocation="/schema/beans www.springframework.
org/schema/beans/spring-beans-4.0.xsd
7 /schema/context /schema/context/spring-context-4.0.xsd
java编程学习入门教材8 /schema/aop /schema/aop/spring-aop-4.0.xsd /schema/tx ht
9 /schema/util /schema/util/spring-util-4.0.xsd">
10
11 <!-- 配置读取properties⽂件 jdbc.properties -->
12 <context:property-placeholder location="classpath:jdbc.properties" />
13
14 <!-- 配置数据源 -->
15 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16 <property name="driverClassName" value="${jdbc.driver}" />
17 <property name="url" value="${jdbc.url}" />
18 <property name="username" value="${jdbc.username}" />
19 <property name="password" value="${jdbc.password}" />
20 </bean>
21
22 <!-- 配置SqlSessionFactory -->
23 <bean class="batis.spring.SqlSessionFactoryBean">
24 <!-- 设置MyBatis核⼼配置⽂件 -->
25 <property name="configLocation" value="classpath:l" />
26 <!-- 设置数据源 -->
27 <property name="dataSource" ref="dataSource" />
28 </bean>
29
30 <!-- 配置Mapper扫描 -->
31 <bean class="batis.spring.mapper.MapperScannerConfigurer">
32 <!-- 设置Mapper扫描包 -->
33 <property name="basePackage" value="apper" />
34 </bean>
35</beans>
jdbc.properties:
配置数据库信息:
1jdbc.sql.jdbc.Driver
2jdbc.url=jdbc:mysql://localhost:3306/mybase?characterEncoding=utf-8
3jdbc.username=root
4jdbc.password=123456
log4j.properties:
配置⽇志信息:
1# Global logging configuration
3#
4log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
配置service的扫描:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="/schema/beans"
3 xmlns:context="/schema/context" xmlns:p="/schema/p"
4 xmlns:aop="/schema/aop" xmlns:tx="/schema/tx"
5 xmlns:xsi="/2001/XMLSchema-instance"
6 xsi:schemaLocation="/schema/beans www.springframework.
org/schema/beans/spring-beans-4.0.xsd
7 /schema/context /schema/context/spring-context-4.0.xsd
8 /schema/aop /schema/aop/spring-aop-4.0.xsd /schema/tx ht
9 /schema/util /schema/util/spring-util-4.0.xsd">
10
11 <!-- 配置Service扫描 -->
12 <context:component-scan base-package="service" />
13</beans>
配置事务管理:事务管理器、通知、切⾯:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="/schema/beans"
3 xmlns:context="/schema/context" xmlns:p="/schema/p"
4 xmlns:aop="/schema/aop" xmlns:tx="/schema/tx"
5 xmlns:xsi="/2001/XMLSchema-instance"
6 xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-4.0.xsd
7 /schema/context /schema/context/spring-context-4.0.xsd
8 /schema/aop /schema/aop/spring-aop-4.0.xsd /schema/tx ht
9 /schema/util /schema/util/spring-util-4.0.xsd">
10
11 <!-- 事务管理器 -->
12 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
13 <!-- 数据源 -->
14 <property name="dataSource" ref="dataSource" />
15 </bean>
16
17 <!-- 通知 -->
18 <tx:advice id="txAdvice" transaction-manager="transactionManager">
19 <tx:attributes>
20 <!-- 传播⾏为 -->
21 <tx:method name="save*" propagation="REQUIRED" />
22 <tx:method name="insert*" propagation="REQUIRED" />
23 <tx:method name="add*" propagation="REQUIRED" />
24 <tx:method name="create*" propagation="REQUIRED" />
25 <tx:method name="delete*" propagation="REQUIRED" />
26 <tx:method name="update*" propagation="REQUIRED" />
27 <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
28 <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
29 <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
30 <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
31 </tx:attributes>
32 </tx:advice>
33
34 <!-- 切⾯ -->
35 <aop:config>
36 <aop:advisor advice-ref="txAdvice"
37 pointcut="execution(* service.*.*(..))" />
38 </aop:config>
39
40</beans>
配置SpringMVC表现层:Controller扫描、注解驱动、视图解析器:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="/schema/beans"
3 xmlns:xsi="/2001/XMLSchema-instance" xmlns:p="/schema/p"
4 xmlns:context="/schema/context"
5 xmlns:mvc="/schema/mvc"
6 xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-4.0.xsd
7 /schema/mvc /schema/mvc/spring-mvc-4.0.xsd
数据库设计说明书 物理结构设计8 /schema/context /schema/context/spring-context-4.0.xsd">
9 <!-- 配置Controller扫描 -->
10 <context:component-scan base-package="ntroller" />
11
12 <!-- 配置注解驱动 -->
13 <mvc:annotation-driven />
14
15 <!-- 配置视图解析器 -->
discuz简洁模板16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17 <!-- 前缀 -->
18 <property name="prefix" value="/WEB-INF/jsp/" />
19 <!-- 后缀 -->
20 <property name="suffix" value=".jsp" />
21 </bean>
22</beans>
配置Spring、SpringMVC、解决post乱码问题:
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app xmlns:xsi="/2001/XMLSchema-instance"
3 xmlns="java.sun/xml/ns/javaee"
4 xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-app_2_5.xsd"
5 id="WebApp_ID" version="2.5">
6 <display-name>boot-crm</display-name>
7 <welcome-file-list>
8 <welcome-file>index.jsp</welcome-file>
9 </welcome-file-list>
10
11 <!-- 配置spring -->
12 <context-param>
13 <param-name>contextConfigLocation</param-name>
14 <param-value>classpath:spring/applicationContext-*.xml</param-value>
15 </context-param>
16
17 <!-- 配置加载spring -->
18 <listener>
19 <listener-class>org.t.ContextLoaderListener</listener-class>
20 </listener>
21
22 <!-- 配置过滤器,解决post的乱码问题 -->
23 <filter>
24 <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
25 </filter>
properties是什么文件26 <filter-mapping>
27 <filter-name>encoding</filter-name>
28 <url-pattern>/*</url-pattern>
29 </filter-mapping>
30
31 <!-- 配置SpringMVC -->
32 <servlet>
33 <servlet-name>boot-crm</servlet-name>
34 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
35 <init-param>
36 <param-name>contextConfigLocation</param-name>
37 <param-value>classpath:l</param-value>
38 </init-param>
39 <!-- 配置springmvc什么时候启动,参数必须为整数 -->
40 <!-- 如果为0或者⼤于0,则springMVC随着容器启动⽽启动 -->
41 <!-- 如果⼩于0,则在第⼀次请求进来的时候启动 -->
42 <load-on-startup>1</load-on-startup>
43 </servlet>
44 <servlet-mapping>
45 <servlet-name>boot-crm</servlet-name>
benchmark软件46 <!-- 所有的请求都进⼊springMVC -->
47 <url-pattern>/</url-pattern>
48 </servlet-mapping>
49</web-app>
⾄此,分模块配置⽂件都ok了,但是你会发现,我的DispatcherServlet的拦⽅式是/,即所有的都拦截,也就是说静态资源也会被拦截,但是我们并不希望拦截我们的静态资源,于是乎:有下⾯两种解决⽅案:
原因:l配置时,是设置所有的请求都进⼊SpringMVC。但是SpringMVC ⽆法处理css、js等静态资源,所以⽆法正常显⽰
解决⽅案:
1. 在l中配置
<!-- 解决静态资源⽆法被springMVC处理的问题 -->
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论