需求:查询商品列表
一.配置
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>