淘淘商城第二天

淘淘商城第二天

1   课程计划

商品列表的查询

1、框架整合springmvc+spring+mybatis

2、创建数据库

3、使用mybatis的逆向工程生成代码

4、商品列表功能实现

 

 

2   创建数据库

使用mysql数据库。

clip_image002

 

在互联网行业的项目中尽可能的减少表的管理查询。使用冗余解决表的关联问题。有利于分库分表。

 

商品表:

clip_image004

Sku:最小库存量单位。就是商品id。就是商品最细力度的划分。每个sku都唯一对应一款商品,商品的颜色、配置都已经唯一确定。

 

3   逆向工程

Mybatis的逆向工程。根据数据库表生成java代码。

clip_image006

注意:如果想再次生成代码,必须先将已经生成的代码删除,否则会在原文件中追加。

 

4   Ssm框架整合

4.1  整合的思路

4.1.1   Dao

使用mybatis框架。创建SqlMapConfig.xml

创建一个applicationContext-dao.xml

1、配置数据源

2、需要让spring容器管理SqlsessionFactory,单例存在。

3、mapper的代理对象放到spring容器中。使用扫描包的方式加载mapper的代理对象。

 

4.1.2   Service

1、事务管理

2、需要把service实现类对象放到spring容器中管理。

 

4.1.3   表现层

1、配置注解驱动

2、配置视图解析器

3、需要扫描controller

 

4.1.4   Web.xml

1、spring容器的配置

2、Springmvc前端控制器的配置

3、Post乱码过滤器

 

4.2  框架整合

需要把配置文件放到taotao-manager-web工程下。因为此工程为war工程,其他的工程只是一个jar包。

4.2.1   Mybatis整合

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 
  3 <!DOCTYPE configuration
  4 
  5          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  6 
  7          "http://mybatis.org/dtd/mybatis-3-config.dtd">
  8 
  9 <configuration>
 10 
 11 
 12 
 13 </configuration>
 14 

 

applicationContext-dao.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <beans xmlns="http://www.springframework.org/schema/beans"
  4 
  5      xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  6 
  7      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  8 
  9      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 10 
 11      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 12 
 13      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 14 
 15      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
 16 
 17      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 18 
 19 
 20 
 21      <!-- 数据库连接池 -->
 22 
 23      <!-- 加载配置文件 -->
 24 
 25      <context:property-placeholder location="classpath:resource/db.properties" />
 26 
 27      <!-- 数据库连接池 -->
 28 
 29      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
 30 
 31          destroy-method="close">
 32 
 33          <property name="url" value="${jdbc.url}" />
 34 
 35          <property name="username" value="${jdbc.username}" />
 36 
 37          <property name="password" value="${jdbc.password}" />
 38 
 39          <property name="driverClassName" value="${jdbc.driver}" />
 40 
 41          <property name="maxActive" value="10" />
 42 
 43          <property name="minIdle" value="5" />
 44 
 45      </bean>
 46 
 47      <!-- 配置sqlsessionFactory -->
 48 
 49      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 50 
 51          <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
 52 
 53          <property name="dataSource" ref="dataSource"></property>
 54 
 55      </bean>
 56 
 57      <!-- 配置扫描包,加载mapper代理对象 -->
 58 
 59      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 60 
 61          <property name="basePackage" value="com.taotao.mapper"></property>
 62 
 63      </bean>
 64 
 65 </beans>
 66 

 

4.2.2   Service

applicationContext-service.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <beans xmlns="http://www.springframework.org/schema/beans"
  4 
  5      xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  6 
  7      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  8 
  9      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 10 
 11      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 12 
 13      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 14 
 15      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
 16 
 17      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 18 
 19 
 20 
 21      <!-- 扫描包加载Service实现类 -->
 22 
 23      <context:component-scan base-package="com.taotao.service"></context:component-scan>
 24 
 25 </beans>
 26 

 

applicationContext-trans.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <beans xmlns="http://www.springframework.org/schema/beans"
  4 
  5      xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  6 
  7      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  8 
  9      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 10 
 11      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 12 
 13      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 14 
 15      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
 16 
 17      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 18 
 19 
 20 
 21      <!-- 事务管理器 -->
 22 
 23      <bean id="transactionManager"
 24 
 25          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 26 
 27          <!-- 数据源 -->
 28 
 29          <property name="dataSource" ref="dataSource" />
 30 
 31      </bean>
 32 
 33      <!-- 通知 -->
 34 
 35      <tx:advice id="txAdvice" transaction-manager="transactionManager">
 36 
 37          <tx:attributes>
 38 
 39               <!-- 传播行为 -->
 40 
 41               <tx:method name="save*" propagation="REQUIRED" />
 42 
 43               <tx:method name="insert*" propagation="REQUIRED" />
 44 
 45               <tx:method name="add*" propagation="REQUIRED" />
 46 
 47               <tx:method name="create*" propagation="REQUIRED" />
 48 
 49               <tx:method name="delete*" propagation="REQUIRED" />
 50 
 51               <tx:method name="update*" propagation="REQUIRED" />
 52 
 53               <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
 54 
 55               <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
 56 
 57               <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
 58 
 59          </tx:attributes>
 60 
 61      </tx:advice>
 62 
 63      <!-- 切面 -->
 64 
 65      <aop:config>
 66 
 67          <aop:advisor advice-ref="txAdvice"
 68 
 69               pointcut="execution(* com.taotao.service.*.*(..))" />
 70 
 71      </aop:config>
 72 
 73 </beans>
 74 

 

4.2.3   表现层

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <beans xmlns="http://www.springframework.org/schema/beans"
  4 
  5      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  6 
  7      xmlns:context="http://www.springframework.org/schema/context"
  8 
  9      xmlns:mvc="http://www.springframework.org/schema/mvc"
 10 
 11      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 12 
 13         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 14 
 15         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 16 
 17 
 18 
 19      <context:component-scan base-package="com.taotao.controller" />
 20 
 21      <mvc:annotation-driven />
 22 
 23      <bean
 24 
 25          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 26 
 27          <property name="prefix" value="/WEB-INF/jsp/" />
 28 
 29          <property name="suffix" value=".jsp" />
 30 
 31      </bean>
 32 
 33 </beans>
 34 

 

Web.xml

 

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 
  5      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6 
  7      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  8 
  9      id="taotao" version="2.5">
 10 
 11      <display-name>taotao-manager</display-name>
 12 
 13      <welcome-file-list>
 14 
 15           <welcome-file>index.html</welcome-file>
 16 
 17          <welcome-file>index.htm</welcome-file>
 18 
 19          <welcome-file>index.jsp</welcome-file>
 20 
 21          <welcome-file>default.html</welcome-file>
 22 
 23          <welcome-file>default.htm</welcome-file>
 24 
 25          <welcome-file>default.jsp</welcome-file>
 26 
 27      </welcome-file-list>
 28 
 29      <!-- 加载spring容器 -->
 30 
 31      <context-param>
 32 
 33          <param-name>contextConfigLocation</param-name>
 34 
 35          <param-value>classpath:spring/applicationContext-*.xml</param-value>
 36 
 37      </context-param>
 38 
 39      <listener>
 40 
 41          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 42 
 43      </listener>
 44 
 45      <!-- 解决post乱码 -->
 46 
 47      <filter>
 48 
 49          <filter-name>CharacterEncodingFilter</filter-name>
 50 
 51          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 52 
 53          <init-param>
 54 
 55               <param-name>encoding</param-name>
 56 
 57               <param-value>utf-8</param-value>
 58 
 59          </init-param>
 60 
 61      </filter>
 62 
 63      <filter-mapping>
 64 
 65          <filter-name>CharacterEncodingFilter</filter-name>
 66 
 67          <url-pattern>/*</url-pattern>
 68 
 69      </filter-mapping>
 70 
 71      <!-- springmvc的前端控制器 -->
 72 
 73      <servlet>
 74 
 75          <servlet-name>taotao-manager</servlet-name>
 76 
 77          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 78 
 79          <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
 80 
 81          <init-param>
 82 
 83               <param-name>contextConfigLocation</param-name>
 84 
 85               <param-value>classpath:spring/springmvc.xml</param-value>
 86 
 87          </init-param>
 88 
 89          <load-on-startup>1</load-on-startup>
 90 
 91      </servlet>
 92 
 93      <servlet-mapping>
 94 
 95          <servlet-name>taotao-manager</servlet-name>
 96 
 97          <url-pattern>/</url-pattern>
 98 
 99      </servlet-mapping>
100 
101 </web-app>
102 

 

/:会拦截所有请求包括静态资源。需要在springmvc.xml中添加静态资源的映射。

  1 <!-- 资源映射 -->
  2 
  3      <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
  4 
  5      <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
  6 

 

4.2.4   添加静态资源

clip_image007

clip_image009


5   Springmvcspring的父子容器关系

image


applicationContext-service中配置:

  1 <!-- 扫描包加载Service实现类 -->
  2 
  3 <context:component-scan base-package="com.taotao"></context:component-scan>
  4 
  5 会扫描@Controller、@Service、@Repository、@Compnent
  6 
  7 
  8 

SpringmvcXml中不扫描。

结论:springmvc。不能提供服务,因为springmvc子容器中没有controller对象。

 

6   测试整合结果

6.1  需求

跟据商品id查询商品信息。

6.2  Sql语句

SELECT * from tb_item WHERE id=536563

 

6.3  Dao

可以使用逆向工程生成的mapper文件。

 

6.4  Service

接收商品id调用dao查询商品信息。返回商品pojo对象。

  1 /**
  2 
  3  * 商品管理Service
  4 
  5  * <p>Title: ItemServiceImpl</p>
  6 
  7  * <p>Description: </p>
  8 
  9  * <p>Company: www.itcast.com</p>
 10 
 11  * @author    入云龙
 12 
 13  * @date  2015年9月2日上午10:47:14
 14 
 15  * @version 1.0
 16 
 17  */
 18 
 19 @Service
 20 
 21 public class ItemServiceImpl implements ItemService {
 22 
 23 
 24 
 25      @Autowired
 26 
 27      private TbItemMapper itemMapper;
 28 
 29 
 30 
 31      @Override
 32 
 33      public TbItem getItemById(long itemId) {
 34 
 35 
 36 
 37          //TbItem item = itemMapper.selectByPrimaryKey(itemId);
 38 
 39          //添加查询条件
 40 
 41          TbItemExample example = new TbItemExample();
 42 
 43          Criteria criteria = example.createCriteria();
 44 
 45          criteria.andIdEqualTo(itemId);
 46 
 47          List<TbItem> list = itemMapper.selectByExample(example);
 48 
 49          if (list != null && list.size() > 0) {
 50 
 51               TbItem item = list.get(0);
 52 
 53               return item;
 54 
 55          }
 56 
 57          return null;
 58 
 59      }
 60 
 61 
 62 
 63 }
 64 

 

6.5  Controller

接收页面请求商品id,调用service查询商品信息。直接返回一个json数据。需要使用@ResponseBody注解。

  1 @Controller
  2 
  3 public class ItemController {
  4 
  5 
  6 
  7      @Autowired
  8 
  9      private ItemService itemService;
 10 
 11 
 12 
 13      @RequestMapping("/item/{itemId}")
 14 
 15      @ResponseBody
 16 
 17      public TbItem getItemById(@PathVariable Long itemId) {
 18 
 19          TbItem tbItem = itemService.getItemById(itemId);
 20 
 21          return tbItem;
 22 
 23      }
 24 
 25 }
 26 

 

 

clip_image012

解决方法:

修改taotao-manager-mapperpom文件

pom文件中添加如下内容:

  1 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
  2 
  3      <build>
  4 
  5          <resources>
  6 
  7             <resource>
  8 
  9                 <directory>src/main/java</directory>
 10 
 11                 <includes>
 12 
 13                     <include>**/*.properties</include>
 14 
 15                     <include>**/*.xml</include>
 16 
 17                 </includes>
 18 
 19                 <filtering>false</filtering>
 20 
 21             </resource>
 22 
 23         </resources>
 24 
 25      </build>
 26 

测试OK

clip_image014

7   使用maventomcat插件时debug

clip_image016

clip_image017

clip_image019

下次启动生效。

 

第二种方法:

clip_image021

 

8   商品列表的实现

8.1  打开后台管理工程的首页

分析:先写一个controller进行页面跳转展示首页。

首页是使用easyUI开发。

 

  1 /**
  2 
  3  * 页面跳转controller
  4 
  5  * <p>Title: PageController</p>
  6 
  7  * <p>Description: </p>
  8 
  9  * <p>Company: www.itcast.com</p>
 10 
 11  * @author    入云龙
 12 
 13  * @date  2015年9月2日上午11:11:41
 14 
 15  * @version 1.0
 16 
 17  */
 18 
 19 @Controller
 20 
 21 public class PageController {
 22 
 23 
 24 
 25      /**
 26 
 27       * 打开首页
 28 
 29       */
 30 
 31      @RequestMapping("/")
 32 
 33      public String showIndex() {
 34 
 35          return "index";
 36 
 37      }
 38 
 39      /**
 40 
 41       * 展示其他页面
 42 
 43       * <p>Title: showpage</p>
 44 
 45       * <p>Description: </p>
 46 
 47       * @param page
 48 
 49       * @return
 50 
 51       */
 52 
 53      @RequestMapping("/{page}")
 54 
 55      public String showpage(@PathVariable String page) {
 56 
 57          return page;
 58 
 59      }
 60 
 61 }
 62 
 63 
 64 

 

8.2  商品列表查询

8.2.1   需求分析

1、请求的url/item/list

clip_image023

2、请求的参数:http://localhost:8080/item/list?page=1&rows=30    分页信息。(需要看官方的手册)

3、返回值。Json数据。数据格式:

Easyuidatagrid控件要求的数据格式为:

  1 {total:”2”,rows:[{“id”:”1”,”name”,”张三”},{“id”:”2”,”name”,”李四”}]}

 

8.2.2   Dao

Sql语句:SELECT * from tb_item LIMIT 0,30

 

8.2.2.1         分页插件PageHelper

8.2.2.1.1       官方网站:

https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper

clip_image025


8.2.2.1.2       实现原理
image
clip_image029

 

8.2.2.1.3       使用方法

第一步:引入pageHelperjar包。

第二步:需要在SqlMapConfig.xml中配置插件。

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 
  3 <!DOCTYPE configuration
  4 
  5          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  6 
  7          "http://mybatis.org/dtd/mybatis-3-config.dtd">
  8 
  9 <configuration>
 10 
 11      <!-- 配置分页插件 -->
 12 
 13      <plugins>
 14 
 15          <plugin interceptor="com.github.pagehelper.PageHelper">
 16 
 17               <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
 18 
 19            <property name="dialect" value="mysql"/>
 20 
 21          </plugin>
 22 
 23      </plugins>
 24 
 25 </configuration>
 26 

 

第三步:在查询的sql语句执行之前,添加一行代码:

PageHelper.startPage(1, 10);

第一个参数是page,要显示第几页。

第二个参数是rows,没页显示的记录数。

第四步:取查询结果的总数量。

创建一个PageInfo类的对象,从对象中取分页信息。

 

8.2.2.1.4       分页测试
  1 public class TestPageHelper {
  2 
  3 
  4 
  5      @Test
  6 
  7      public void testPageHelper() {
  8 
  9          //创建一个spring容器
 10 
 11          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
 12 
 13          //从spring容器中获得Mapper的代理对象
 14 
 15          TbItemMapper mapper = applicationContext.getBean(TbItemMapper.class);
 16 
 17          //执行查询,并分页
 18 
 19          TbItemExample example = new TbItemExample();
 20 
 21          //分页处理
 22 
 23          PageHelper.startPage(2, 10);
 24 
 25          List<TbItem> list = mapper.selectByExample(example);
 26 
 27          //取商品列表
 28 
 29          for (TbItem tbItem : list) {
 30 
 31               System.out.println(tbItem.getTitle());
 32 
 33          }
 34 
 35          //取分页信息
 36 
 37          PageInfo<TbItem> pageInfo = new PageInfo<>(list);
 38 
 39          long total = pageInfo.getTotal();
 40 
 41          System.out.println("共有商品:"+ total);
 42 
 43 
 44 
 45      }
 46 
 47 }
 48 

 

注意:分页插件对逆向工程生成的代码支持不好,不能对有查询条件的查询分页。会抛异常。

使用我修改过的版本就可以了。

clip_image031

 

Dao可以实现逆向工程生成的mapper文件+PageHelper实现。

 

8.2.3   Service

接收分页参数,一个是page一个是rows。调用dao查询商品列表。并分页。返回商品列表。

返回一个EasyUIDateGrid支持的数据格式。需要创建一个Pojo。此pojo应该放到taotao-common工程中。

  1 public class EUDataGridResult {
  2 
  3 
  4 
  5      private long total;
  6 
  7      private List<?> rows;
  8 
  9      public long getTotal() {
 10 
 11          return total;
 12 
 13      }
 14 
 15      public void setTotal(long total) {
 16 
 17          this.total = total;
 18 
 19      }
 20 
 21      public List<?> getRows() {
 22 
 23          return rows;
 24 
 25      }
 26 
 27      public void setRows(List<?> rows) {
 28 
 29          this.rows = rows;
 30 
 31      }
 32 
 33 
 34 
 35 
 36 
 37 }
 38 

 

代码实现

  1 /**
  2 
  3       * 商品列表查询
  4 
  5       * <p>Title: getItemList</p>
  6 
  7       * <p>Description: </p>
  8 
  9       * @param page
 10 
 11       * @param rows
 12 
 13       * @return
 14 
 15       * @see com.taotao.service.ItemService#getItemList(long, long)
 16 
 17       */
 18 
 19      @Override
 20 
 21      public EUDataGridResult getItemList(int page, int rows) {
 22 
 23          //查询商品列表
 24 
 25          TbItemExample example = new TbItemExample();
 26 
 27          //分页处理
 28 
 29          PageHelper.startPage(page, rows);
 30 
 31          List<TbItem> list = itemMapper.selectByExample(example);
 32 
 33          //创建一个返回值对象
 34 
 35          EUDataGridResult result = new EUDataGridResult();
 36 
 37          result.setRows(list);
 38 
 39          //取记录总条数
 40 
 41          PageInfo<TbItem> pageInfo = new PageInfo<>(list);
 42 
 43          result.setTotal(pageInfo.getTotal());
 44 
 45          return result;
 46 
 47      }
 48 

 

8.2.4   Controller

接收页面传递过来的参数pagerows。返回json格式的数据。EUDataGridResult

需要使用到@ResponseBody注解。

  1 @RequestMapping("/item/list")
  2 
  3      @ResponseBody
  4 
  5      public EUDataGridResult getItemList(Integer page, Integer rows) {
  6 
  7          EUDataGridResult result = itemService.getItemList(page, rows);
  8 
  9          return result;
 10 
 11      }
 12 

 

截图留念:

clip_image033

原文地址:https://www.cnblogs.com/ios9/p/7466788.html