3.ssm整合(傳智播客)
阿新 • • 發佈:2018-11-19
需求:查詢商品列表
一.配置
1.web.xml
<!-- 啟動Web容器時,載入spring容器,自動裝配ApplicationContext.xml的配置資訊 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 載入springmvc配置檔案 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <!-- 配置將所有請求交給springmvc來處理 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
2.springmvc.xml
<!-- 1.配置處理器,可以掃描controller、service、... 這裡讓掃描controller,指定controller的包 --> <context:component-scan base-package="com.steven.ssm.controller"></context:component-scan> <!-- 2.配置處理器對映起和處理器介面卡 --> <!-- 使用mvc:annotation-driven配置註解對映器和註解介面卡,其預設載入很多的引數繫結方法,比如json轉換解析器等等 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 3.配置檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路徑的字首 --> <property name="prefix" value="/WEB-INF/views/"/> <!-- 配置jsp路徑的字尾 --> <property name="suffix" value=".jsp"/> </bean>
3.applicationContext-dao.xml
<!-- 載入db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 1.配置資料來源(c3p0資料庫連線池) --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 基礎配置 --> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="driverClass" value="${jdbc.driver}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 2.配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 載入資料來源 --> <property name="dataSource" ref="dataSource" /> <!-- 載入mybatis的全域性配置檔案 --> <property name="configLocation" value="classpath:mybatis/mybatis.xml" /> <!--設定對映檔案位置--> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!-- 3.配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描包路徑(如果需要掃描多個包,中間使用半形逗號隔開) --> <property name="basePackage" value="com.steven.ssm.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
4.applicationContext-service.xml
<bean id="itemsService" class="com.steven.ssm.service.impl.ItemsServiceImpl"/>
二.開發步驟
1.處理器(控制器)的開發
@Controller
@RequestMapping("/items")
public class ItemsController {
private static final Logger log = Logger.getLogger(Logger.class);
@Autowired
private ItemsServiceImpl itemsService;
//商品查詢列表
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
List<Items> itemsList = itemsService.getItemsList();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList",itemsList);
modelAndView.setViewName("items/queryItems");
log.info("itemList:"+itemsList);
return modelAndView;
}
}
2.service開發
public interface ItemsService {
//查詢商品列表
List<Items> getItemsList() throws Exception;
}
@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapperCustom itemsMapperCustom;
@Override
public List<Items> getItemsList() throws Exception {
return itemsMapperCustom.getItemsList();
}
}
3.dao開發
public interface ItemsMapperCustom {
//查詢商品列表
List<Items> getItemsList()throws Exception;
}
<!--查詢商品列表-->
<select id="getItemsList" resultMap="queryItems">
SELECT * FROM item
</select>
<resultMap id="queryItems" type="Items">
<id column="item_id" property="itemId"/>
<result column="item_id" property="itemId"/>
<result column="item_name" property="itemName"/>
<result column="item_price" property="itemPrice"/>
<result column="item_detail" property="itemDetail"/>
<result column="item_createDate" property="itemCreateDate"/>
</resultMap>
4.檢視的開發
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查詢商品列表</title>
</head>
<body>
<form action="" method="post">
商品列表:
<table width="100%" border=1>
<tr>
<td>商品編號</td>
<td>商品名稱</td>
<td>商品價格</td>
<td>商品描述</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.price }</td>
<td>${item.detail }</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>