淘淘商城第二天

Posted ✧*꧁一品堂.技术学习笔记꧂*✧.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了淘淘商城第二天相关的知识,希望对你有一定的参考价值。

淘淘商城第二天

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="SUP

以上是关于淘淘商城第二天的主要内容,如果未能解决你的问题,请参考以下文章

淘淘商城的第一天

淘淘商城_0100_前言

传智播客大型分布式电商项目-淘淘商城

传智播客大型分布式电商项目-淘淘商城

淘淘商场第二天

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结第五天

(c)2006-2024 SYSTEM All Rights Reserved IT常识