之前就听老师说过Spring框架全家桶,一直不知道是什么,带着神秘感看了很多也不是很了解,借着实训的机会简单的学习了一下SSM框架的配置,作为小白刚入门Spring真的是需要直到一步一步到底怎么么做才行,直接po上代码会不知道放到哪个文件夹里,所以小白本人要详细记录一下配置的流程。
(基于eclipse的)
创建一个 普通的web项目(下面这个一定要勾选上它会生成web的核心文件web.xml)
在webcontext-->web-inf-->lib需要导入相关的jar包
看了看有42个,小白有好多jar包还不知道使用来干什么的所以干脆一股脑全导入了(不要喷我)
接下来就是配置了:
所谓的配置其实就是写好.xml文件。当你开始去弄得时候会发现有好多xml文件,SSM三个对应是哪个xml文件
- SpringMVC------------springmvc.xml
- mybatis-------------mybatis.xml
- spring-------------applicationConnContext.xml
这些配置文件里面的基本内容是不需要我们自己写的直接复制粘贴过去就行(有前人的模板,稍后我会粘出代码,自己创建文件复制代码过去就好了)
粘贴到src下就行,我这里一共导入5个文件,还有一个数据库的文件配置和日志配置
可以看到我创建了很多包,这个稍后再说
配置文件的代码
1. applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 配置读取db.properties配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置扫描Service -->
<context:component-scan base-package="com.situ.ssm.service"/>
<!-- ******************数据库相关配置************************ -->
<!-- 数据库相关配置 -->
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSessionFactory 单独的MyBaits需要自己去new这个SqlSessionFactory,
现在和Spring结合后交给Spring容器帮我们创建好,默认bean的单例的。
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置Mapper扫描 -->
<!-- MapperScannerConfigurer:mapper扫描器,将包下面的mapper接口
自动创建代理对象,自动放到Spring容器中, bean
的id就是mapper的类名(首字母小写)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper扫描包 -->
<property name="basePackage" value="com.situ.ssm.dao"/>
</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="*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.situ.ssm.service.impl.*.*(..))"/>
</aop:config>
</beans>
2. mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 定义类型别名 -->
<typeAliases>
<!-- 扫描包里面的类,批量起别名,别名即为类名,别名不区分大小写 -->
<package name="com.situ.ssm.entity"/>
<!-- 包名的书写规范在此不再详解 -->
</typeAliases>
</configuration>
3.springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描@Controller 是要扫描注解的包-->
<context:component-scan base-package="com.situ.ssm.controller"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 视图解析器
如果Controller中书写的是视图的逻辑名,这个视图解析器必须要配置。
前缀+视图逻辑名+后缀=真实路径
如果Controller中书写的是视图的真实名称,这个视图解析器就不需要配置
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 路径前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property><!-- 参考controller类 -->
<!-- 路径后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
剩下的两个顺便也粘贴出来吧
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
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/**(数据库名)?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=**(数据库密码)
这些只是框架的文件,最终起作用的是web.xml的配置,所以要修改web.xml文件的配置(项目创建时自动生成的),这是整个项目的核心
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SSMtest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置监听器加载Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载Spring核心配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 解决POST乱码问题 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置DispatcherServlet(前端控制器(调度器)) -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- classpath:代表src这个路径 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 所有以action结尾的请求都交给DispatcherServlet处理 -->
<!-- /student.action /user.action -->
<url-pattern>*.action</url-pattern>
<!-- 拦截所有以action结尾的 -->
</servlet-mapping>
</web-app>
基本工作差不多完成了,接下来就是建包
entity(放实体类)、controller(相当于servlet,在这里以及去掉servlet的概念了)、service、dao
细节说明 :
想要真正理解先看一张图