欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单的SSM框架搭建教程

程序员文章站 2022-05-25 19:48:08
简单的ssm框架的搭建和配置文件 ssm框架里边的配置: 1.src路径下直接存放数据库和log4j的properties文件 2.src路径下建个config包,分别放置ssm的xml文件 3.修改WEB-INF路径下的web.xml 4.注意 ......

简单的ssm框架的搭建和配置文件

  • ssm框架里边的配置:

  • 1.src路径下直接存放数据库和log4j的properties文件

  • 2.src路径下建个config包,分别放置ssm的xml文件

  • 3.修改web-inf路径下的web.xml

  • 4.注意放置配置文件的路径问题


1.src路径下的jdbc.propreties和log4j.properties

简单的SSM框架搭建教程

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文件

简单的SSM框架搭建教程

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

简单的SSM框架搭建教程

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.注意放置配置文件的路径问题