第二天:商品列表的查询
1. 计划
商品列表的查询
1、创建数据库
2、框架整合springmvc+spring+mybatis
3、商品列表功能实现
第一节:创建数据库
使用mysql数据库。在互联网行业的项目中尽可能的减少表的管理查询。使用冗余解决表的关联问题。有利于分库分表。
1.1 商品表:

Sku:最小库存量单位。就是商品id。就是商品最细力度的划分。每个sku都唯一对应一款商品,商品的颜色、配置都已经唯一确定。
第二节:整合springmvc+spring+mybatis
2.1 整合的思路
2.1.1 Dao层
使用mybatis框架, 创建SqlMapConfig.xml。
创建一个applicationContext-dao.xml, applicationContext-dao.xml包含如下内容:
1、配置数据源。
2、需要让spring容器管理SqlsessionFactory,单例存在。
3、把mapper的代理对象放到spring容器中,使用扫描包的方式加载mapper的代理对象。
2.1.2 Service层
1、事务管理
2、需要把service实现类对象放到spring容器中管理。
2.1.3 表现层
1、配置注解驱动。
2、配置视图解析器。
3、需要扫描controller。
4、 web.xml的配置。
2.2 框架整合
需要把上面的所有的配置文件放到pfnie-manager-web工程下。因为此工程为war工程,其他的工程只是一个jar包。
2.2.1 Mybatis整合
mybatisConfig.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>
</configuration>
applicationContext-dao.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:mongo="http://www.springframework.org/schema/data/mongo" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db/db.properties" /> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 配置sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/mybatisConfig.xml"/> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置扫描包,加载mapper代理对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.npf.manager.mapper"></property> </bean> </beans>
2.2.2 Service层
applicationContext-service.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:mongo="http://www.springframework.org/schema/data/mongo" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 扫描包加载Service实现类 --> <context:component-scan base-package="com.npf.manager.service"/> </beans>
applicationContext-transaction.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:mongo="http://www.springframework.org/schema/data/mongo" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> </beans>
2.2.3 表现层
springmvc.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:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.npf.manager.web"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="pfnie" version="2.5"> <display-name>pfnie-manager-web</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> <!-- 解决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> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>pfnie-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>pfnie-manager</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
/:会拦截所有请求包括静态资源。需要在springmvc.xml中添加静态资源的映射。
<!-- 资源映射 --> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
2.3 添加静态资源

2.4 Spring和Springmvc的父子容器关系
2.5 测试整合结果
使用maven命令:
clean tomcat7:run
tomcat7指定使用tomcat7的插件。
访问: http://localhost:8080/langjitianya/index
截图如下:

2.6 测试获取单个商品
2.6.1 需求
跟据商品id查询商品信息。
2.6.2 sql语句
SELECT * from tb_item WHERE id=536563
2.6.3 Dao层
mapper接口以及mapper.xml文件。
2.6.4 Service层
接收商品id调用dao查询商品信息。返回商品entity对象。
package com.npf.manager.service.item; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.npf.manager.entity.Item; /** * * @author Jack * */ public interface ItemService { @Transactional(propagation=Propagation.SUPPORTS,readOnly=true) public Item getItemById(long itemId); }
2.6.5 Controller层
接收页面请求商品id,调用service查询商品信息。直接返回一个json数据。
package com.npf.manager.web.item; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.npf.manager.entity.Item; import com.npf.manager.service.item.ItemService; /** * * @author Jack * */ @RestController @RequestMapping("/item") public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/{itemId}") public Item getItemById(@PathVariable Long itemId) { Item tbItem = itemService.getItemById(itemId); return tbItem; } }
访问: http://localhost:8080/langjitianya/item/536563 出现下面异常:

解决方法:修改pfnie-manager-mapper的pom文件,在pom文件中添加如下内容:
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
测试OK:
2.7 使用maven的tomcat插件时debug
第一种方法(下次启动生效):

第二种方法:

3 商品列表的实现
3.1 打开后台管理工程的首页
分析:先写一个controller进行页面跳转展示首页。首页是使用easyUI开发。
package com.npf.manager.web.index; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * * @author Jack * */ @Controller public class IndexController { @RequestMapping("/index") public String goIndex(){ return "index"; } @RequestMapping("/item-list") public String goItemList(){ return "item-list"; } @RequestMapping("/item-add") public String goItemAdd(){ return "item-add"; } }
3.2 商品列表查询
3.2.1 需求分析
1、请求的url:/item/list

2、请求的参数:http://localhost:8080/langjitianya/item/list?page=1&rows=30 分页信息。
3、返回值。Json数据。数据格式:
Easyui中datagrid控件要求的数据格式为:
{total:”2”,rows:[{“id”:”1”,”name”,”张三”},{“id”:”2”,”name”,”李四”}]}
3.2.2 Dao层
Sql语句:SELECT * from tb_item LIMIT 0,10
3.2.3 分页插件PageHelper
https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper
3.2.4 实现原理

3.2.5 分页插件使用方法
第一步:引入pageHelper的jar包。
第二步:需要在MybatisConfig.xml中配置插件。

第三步:在查询的sql语句执行之前,添加一行代码:
PageHelper.startPage(1, 10);
第一个参数是page,要显示第几页。
第二个参数是rows,每页显示的记录数。
第四步:取查询结果的总数量。
创建一个PageInfo类的对象,从对象中取分页信息。
3.2.6 Service层
接收分页参数,一个是page一个是rows。调用dao查询商品列表。并分页。返回商品列表。
返回一个EasyUIDateGrid支持的数据格式。需要创建一个Pojo。此pojo应该放到pfnie-common工程中。
package com.npf.pfniecommon.ui; import java.util.List; public class EUDataGridResult { private long total; private List<?> rows; public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public List<?> getRows() { return rows; } public void setRows(List<?> rows) { this.rows = rows; } }
代码实现:
@Override public EUDataGridResult getItemList(int page, int rows) { PageHelper.startPage(page, rows); List<Item> list = itemMapper.getItemList(page, rows); EUDataGridResult result = new EUDataGridResult(); result.setRows(list); PageInfo<Item> pageInfo = new PageInfo<>(list); long total = pageInfo.getTotal(); result.setTotal(total); return result; }
3.2.7 Controller层
接收页面传递过来的参数page、rows。返回json格式的数据EUDataGridResult.
package com.npf.manager.web.item; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.npf.manager.entity.Item; import com.npf.manager.service.item.ItemService; import com.npf.pfniecommon.ui.EUDataGridResult; /** * * @author Jack * */ @RestController @RequestMapping("/item") public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/{itemId}") public Item getItemById(@PathVariable Long itemId) { Item tbItem = itemService.getItemById(itemId); return tbItem; } @RequestMapping("/list") public EUDataGridResult getItemList(Integer page, Integer rows) { EUDataGridResult result = itemService.getItemList(page, rows); return result; } }
当点击”查询商品”选项时,后台会调用/item-list跳转到查询商品页面,在该页面的背后会有一个ajax的call调用/item/list, 获取第一页的数据,截图留念:


浙公网安备 33010602011771号