简单的ssm框架的搭建和配置文件
- ssm框架里边的配置:
- 1.src路径下直接存放数据库和log4j的properties文件
- 2.src路径下建个config包,分别放置ssm的xml文件
- 3.修改WEB-INF路径下的web.xml
- 4.注意放置配置文件的路径问题
1.src路径下的jdbc.propreties和log4j.properties
log4j.properties
log4j.properties里边的代码,例如:
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
jdbc.properties
jdbc.properties里边的代码,例如:
1 # Global logging configuration 2 jdbc.driverClassName=com.mysql.jdbc.Driver 3 jdbc.url=jdbc:mysql:///ssm_my?useUnicode=true&characterEncoding=utf8 4 jdbc.username=root 5 jdbc.password=root
2.src路径下建个config包,分别放置ssm的xml文件
applicationContext.xml
spring框架里边applicationContext.xml配置文件的代码,例如:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.3.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx.xsd"> 14 <!-- 扫描包 --> 15 <context:component-scan base-package="com.offcn"> 16 <context:exclude-filter type="annotation" 17 expression="org.springframework.stereotype.Controller" /> 18 </context:component-scan> 19 20 <!-- 加载配置文件 --> 21 <context:property-placeholder location="classpath:jdbc.properties" /> 22 <!-- 数据库的配置文件 --> 23 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 24 <property name="driverClassName" value="${jdbc.driverClassName}"></property> 25 <property name="url" value="${jdbc.url}"></property> 26 <property name="username" value="${jdbc.username}"></property> 27 <property name="password" value="${jdbc.password}"></property> 28 </bean> 29 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <property name="dataSource" ref="dataSource"></property> 32 <property name="configLocation" value="classpath:config/SqlMapConfig.xml"></property> 33 </bean> 34 35 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 36 <property name="basePackage" value="com.offcn.mapper"></property> 37 </bean> 38 39 40 <bean id="transactionManager" 41 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 42 <property name="dataSource" ref="dataSource"></property> 43 </bean> 44 45 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 46 <tx:attributes> 47 <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" 48 read-only="true" /> 49 <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" 50 read-only="false" /> 51 </tx:attributes> 52 </tx:advice> 53 <!-- aop配置 --> 54 <aop:config> 55 <aop:pointcut expression="execution(* com.offcn.service.*.*(..))" 56 id="poit" /> 57 <aop:advisor advice-ref="txAdvice" pointcut-ref="poit" /> 58 </aop:config> 59 </beans>
springmvc-servlet.xml
springmvc框架里边springmvc-servlet.xml配置文件的代码,例如:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-4.3.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd 15 http://www.springframework.org/schema/mvc 16 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> 17 18 <bean 19 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <property name="viewClass" 21 value="org.springframework.web.servlet.view.JstlView"></property> 22 <property name="prefix" value="/views/"></property> 23 <property name="suffix" value=".jsp"></property> 24 </bean> 25 26 <context:component-scan base-package="com.offcn.controller"></context:component-scan> 27 <mvc:annotation-driven> 28 </mvc:annotation-driven> 29 <mvc:default-servlet-handler /> 30 31 <mvc:interceptors> 32 <mvc:interceptor> 33 <mvc:mapping path="/**" /> 34 <bean class="com.offcn.interceptor.LoginInterceptor"> 35 <property name="excuteMappingURL"> 36 <list> 37 <value>.js</value> 38 <value>.css</value> 39 <value>.png</value> 40 <value>.gif</value> 41 <value>.jsp</value> 42 </list> 43 </property> 44 </bean> 45 </mvc:interceptor> 46 </mvc:interceptors> 47 48 </beans>
SqlMapConfig.xml
mybatis框架里边SqlMapConfig.xml配置文件的代码,例如:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <settings> 8 <setting name="lazyLoadingEnabled" value="true" /> 9 <setting name="aggressiveLazyLoading" value="false" /> 10 <setting name="cacheEnabled" value="true" /> 11 </settings> 12 13 <typeAliases> 14 <package name="com.offcn.pojo" /> 15 </typeAliases> 16 17 </configuration>
3.修改WEB-INF路径下的web.xml
WEB-INF路径下的web.xml
WEB-INF路径下的web.xml的代码,例如:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <display-name>ssm</display-name> 8 9 <welcome-file-list> 10 <welcome-file>views/login.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 解决字符乱码问题 --> 14 <filter> 15 <filter-name>CharacterEncodingFilter</filter-name> 16 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 17 <init-param> 18 <param-name>encoding</param-name> 19 <param-value>utf-8</param-value> 20 </init-param> 21 </filter> 22 <filter-mapping> 23 <filter-name>CharacterEncodingFilter</filter-name> 24 <url-pattern>/*</url-pattern> 25 </filter-mapping> 26 27 <!--springmvc的配置 --> 28 <servlet> 29 <servlet-name>springmvc</servlet-name> 30 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 31 <init-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>classpath:config/springmvc-servlet.xml</param-value> 34 </init-param> 35 </servlet> 36 <servlet-mapping> 37 <servlet-name>springmvc</servlet-name> 38 <url-pattern>/</url-pattern> 39 </servlet-mapping> 40 41 <!-- spring的配置 --> 42 <listener> 43 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 44 </listener> 45 46 <context-param> 47 <param-name>contextConfigLocation</param-name> 48 <param-value>classpath:config/applicationContext.xml</param-value> 49 </context-param> 50 51 </web-app>
4.注意放置配置文件的路径问题
原文地址:https://www.cnblogs.com/aishangJava/p/10359184.html
时间: 2024-10-08 13:46:25