总结了一下搭建SSM框架流程,在以后用到的时候方便回头使用。
使用工具:MyEclipse 2015;Tomcat 8版本;jdk1.8版本。
首先:
1:创建一个WebProject项目,jdk1.8 Tomcat8 最后勾选web.xml配置文件。
然后:
2.将相应的Jar包导入lib文件下。总共35个Jar包,将OJBDBC也导入进去。
3.配置web.xml文件。
配置2个内容。一个是Spring,一个是Spring MVC的配置。
Spring配置信息
1:通过全局上下文参数来加载Spring配置文件
2:配置监听器。
在web.xml中继续配置Spring MVC;
Spring MVC的配置信息。
然后还需要配置一下中文乱码解决问题。继续在web.xml中配置相关信息。
然后,进行下一步。
4:加入3个配置文件。Spring,Spring MVC,Mybatis 这三个配置文件需要加入。
将配置文件放在src根目录下即可。
Spring的扫描包:配置了事物。(applicationContext.xml);
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.sys.dao" />
<context:component-scan base-package="com.sys.service" />
<context:component-scan base-package="com.sys.entity"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:initialPoolSize="${jdbc.initialSize}"
p:maxPoolSize="${jdbc.maxActive}"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml">property>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sys.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.sys.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
aop:config>
beans>
Spring MVC:(servlet-mvc.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.sys.controller" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
mvc:message-converters>
mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp">property>
bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="5242880"/>
bean>
<mvc:default-servlet-handler/>
beans>
MyBatis 配置文件(mybatis-config.xml)放置在src目录下的mabatis文件夹内。
<configuration>
<typeAliases>
<package name="com.sys.entity"/>
typeAliases>
configuration>
JDBC文件与Log4j配置文件。(日志文件,将错误信息保存在日志文件,前台不能显示错误文件,也可保存用户访问信息,以及数据库操作的信息)。
log4j.properties配置源码:
log4j.rootLogger=info,appender1,appender2
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=D:/logs/news/logFile.txt
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
在jdbc配置文件中修改相关信息。(需要自己修改;#代表注释)。
jdbc.properties配置源码
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver //Oracle数据库
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:jredu //数据库名
jdbc.username=OnlineTest //数据库表
jdbc.password=Jredu12345 //数据库密码
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=60000
至此,关于SSM框架搭建已经成功,在下一个博客之中,会实现一个SSM搭建框架,实现一个简单的登录功能。
如有疑问或需要或指教的朋友:
希望朋友多多指导,小白萌新渴望学习。
QQ:1090239782