需求:查询商品列表

一.整合原理

1.ssm系统架构
3.ssm整合(传智播客)
2.项目工程结构(参考前一小节建立)
3.整合过程
(1).整合dao (需要的数据库文件从github的项目代码中获取)

  1. 配置mybatis.xml。
  2. 配置applicationContext-dao.xml(数据源,SqlSessionFactory,mapper扫描器)。
  3. ****生成po类及mapper(ItemsMapperCustom.xml和ItemsMapperCustom.java)。

(2).整合service

  1. 定义service接口。
  2. 在spring容器配置事务控制(applicationContext-transaction.xml)。

(3).整合springmvc

  1. 创建springmvc.xml文件,配置处理器、处理器映射器、处理器适配器、视图解析器。
  2. 编写controller。
  3. 编写jsp。
  4. 加载spring容器(web.xml中)。

二.配置

1.web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<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">
	<!-- 启动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>
</web-app>

2.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: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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!-- 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>
</beans>

3.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.steven.ssm.po"/>
	    </typeAliases>
	</configuration>

4.applicationContext-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
	<!-- 加载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>
</beans>

5.applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    <!-- 商品管理的service -->
    <bean id="itemsService" class="com.steven.ssm.service.impl.ItemsServiceImpl"/>
</beans>

三.开发步骤

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层的开发

//ItemsService
public interface ItemsService {
    //查询商品列表
    List<Items> getItemsList() throws Exception;
}
//ItemsServiceImpl 
@Service
public class ItemsServiceImpl implements ItemsService {
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    public List<Items> getItemsList() throws Exception {
        return itemsMapperCustom.getItemsList();
    }
}

3.dao层的开发

//ItemsMapperCustom
public interface ItemsMapperCustom {
    //查询商品列表
    List<Items> getItemsList()throws Exception;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.steven.ssm.mapper.ItemsMapperCustom" >
	<!--查询商品列表-->
	<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>
</mapper>

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>

5.测试
3.ssm整合(传智播客)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2022-01-29
  • 2021-04-20
猜你喜欢
  • 2022-02-10
  • 2021-07-08
  • 2021-05-28
  • 2021-11-10
  • 2021-06-09
  • 2021-05-19
相关资源
相似解决方案