Spring系列文章
Spring框架-1(基礎(chǔ))
Spring框架-2(IOC上)
Spring框架-3(IOC下)
Spring框架-4(AOP)
Spring框架-5(JDBC模板&Spring事務(wù)管理)
Spring框架-6(SpringMvc)
Spring框架-7(搭建SSM)
Spring框架-8(SpringMVC2)
前面學(xué)習(xí)了spring和SpringMvc缰盏,掌握了一些基礎(chǔ)知識意推,那么現(xiàn)在我們來整合一下ssm钦无。使用一個簡單的小項目來使用一下绕德。下面是整合ssm的一些基礎(chǔ):
搭建SSM
寫好后的目錄結(jié)構(gòu):
這里的所有的配置文件我單獨創(chuàng)建了一個config。所有的配置文件doub放在這里怀各,當(dāng)然我在web.xml中這樣是無法訪問的。我們需要通過idea吧這個目錄設(shè)置為resource目錄。點擊config目錄右鍵戒祠,如圖:
這樣在web.xml中就能訪問到我們的配置文件了
創(chuàng)建項目導(dǎo)入jar包
使用IDEA創(chuàng)建一個Web項目。
導(dǎo)入jar包
Dao層配置文件
數(shù)據(jù)庫文件速种,使用mybatis逆向工程生成mapper
數(shù)據(jù)庫表結(jié)構(gòu)
逆向工程生成所用的東西
創(chuàng)建SqlMapConfig.xml
mybatis核心配置文件
<?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>
</configuration>
創(chuàng)建ApplicationContext-dao.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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 數(shù)據(jù)庫連接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- mapper配置 -->
<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數(shù)據(jù)庫連接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!-- 配置Mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhong.dao"/>
</bean>
</beans>
通配符配置的db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.108.184.253:3306/zTest
jdbc.username=root
jdbc.password=123456
Service層配置文件
ApplicationContext-trans.xml 事務(wù)
<?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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事務(wù)管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" 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="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.zhong.service.*.*(..))"/>
</aop:config>
</beans>
ApplicationContext-service.xml @Service注解掃描
<?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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- @Service掃描 -->
<context:component-scan base-package="com.zhong.service"></context:component-scan>
</beans>
controller層配置文件
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.zhong.controller"></context:component-scan>
<!-- 注解驅(qū)動:
替我們顯示的配置了最新版的注解的處理器映射器和處理器適配器 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 配置視圖解析器
作用:在controller中指定頁面路徑的時候就不用寫頁面的完整路徑名稱了,可以直接寫頁面去掉擴展名的名稱
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的頁面路徑 = 前綴 + 去掉后綴名的頁面名稱 + 后綴 -->
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomcat啟動的時候就加載這個servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置Post請求亂碼 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
流程
- tomcat啟動時首先加載web.xml文件
- web.xml中加載spring的所有的ApplicationContext開頭的配置文件
- 在所有的ApplicationContext中再加載所有的db.properties姜盈,SqlMapConfig.xml文件
- web.xml中加載SpringMvc.xml
加載完成,我們的ssm框架就能跑起來了配阵。
寫一個測試
需求
請求http://localhost:8080/list.action查詢數(shù)據(jù)庫數(shù)據(jù)馏颂,返回jsp頁面顯示出來
1.編寫jsp
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td>商品價格</td>
<td>生產(chǎn)日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
2.編寫service
使用mybatis查詢數(shù)據(jù)庫
@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapper itemsMapper;
@Override
public List<Items> list() throws Exception {
//如果不需要任何查詢條件,直接將example對象new出來即可
ItemsExample example = new ItemsExample();
List<Items> list = itemsMapper.selectByExampleWithBLOBs(example);
return list;
}
}
3.編寫controller
springMvc中默認(rèn)支持的參數(shù)類型:也就是說在controller方法中可以加入這些參數(shù)如下的Model示血,也可以不加, 加不加看自己需不需要,都行.HttpServletRequest,HttpServletResponse,HttpSession,Model
@Controller
public class ItemsController {
@Autowired
private ItemsService itmesService;
@RequestMapping("/list")
public String itemsList(Model model) throws Exception {
List<Items> list = itmesService.list();
//Model模型:模型中放入了返回給頁面的數(shù)據(jù)
//model底層其實就是用的request域來傳遞數(shù)據(jù),但是對request域進行了擴展.
model.addAttribute("itemList", list);
//如果springMvc方法返回一個簡單的string字符串,那么springMvc就會認(rèn)為這個字符串就是頁面的名稱
return "itemList";
}
}
4.測試
配置tomcat訪問http://localhost:8080/list.action,注意在前面配置了.action所以必須在后面加上.action救拉。
結(jié)果:成功