栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于SpringBoot构建电商秒杀项目代码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于SpringBoot构建电商秒杀项目代码实例

一、项目功能概述

电商秒杀需要完成的3个功能:

1.展示一个商品列表页面,我们可以从中看到可秒杀的商品列表

2.点击进入商品详情页,获取该商品的详细信息

3.秒杀时间开始后,点击进入下单确认页面,并支付成功

二、基于SpringBoot进行项目环境搭建

步骤1:创建一个maven工程,使用quickStart骨架。

步骤2:在pom.xml导入SpringBoot相关依赖。

 
 
 
  4.0.0 
  org.example
  Spike
  1.0-SNAPSHOT
 
  Spike
  
  http://www.example.com 
 
  org.springframework.boot
  spring-boot-starter-parent
  2.0.5.RELEASE
  
  
   UTF-8
   1.8   1.8
   
  
   
    org.springframework.boot
    spring-boot-starter-web
   
   
    junit
    junit
    4.11
    test
   
   
  
   
    
     
     
     maven-clean-plugin
      3.1.0
     
     
     
      maven-resources-plugin
      3.0.2
     
     
      maven-compiler-plugin
      3.8.0
     
     
      maven-surefire-plugin
      2.22.1
     
     
      maven-jar-plugin
      3.0.2
     
     
      maven-install-plugin
      2.5.2
    
     
      maven-deploy-plugin
      2.8.2
     
     
     
      maven-site-plugin
      3.7.1
     
     
      maven-project-info-reports-plugin
      3.0.0
     
    
   
  
 

步骤3:在main/java/app中,我们对SpringBoot和SpringMVC进行简单的配置工作。掌握这几个注解的作用。

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//SpringBoot会帮我们启动tomcat,并加载默认配置
@EnableAutoConfiguration
//SpringMVC相关配置
@RestController
public class App {
  @RequestMapping("/")
  public String home(){
    //网页中输出
    return "Hello World!";
  }
  public static void main( String[] args ){
    //控制台输出
    System.out.println( "Hello World!" );
    SpringApplication.run(App.class,args);
  }
}

运行结果:

用浏览器打开http://localhost:8080/,我们可以看到页面上输出:Hello World!

同时,控制台也输出了Hello World!,以及一些Spring相关的信息。

SpringBoot小技巧:可以在resource目录下创建一个application.propeties配置文件,在其中写:server.port = 端口号来设置端口号。

步骤4:接入mybatis,首先在pom.xml添加需要的依赖(mysql,druid连接池,mybatis)

写一个plugin标签,引入对应的mybatis自动生成文件的插件 {

添加对应的依赖:mybatis generator的core(第一次使用要单独在前面导入依赖,不可直接放在plugin中),mysql数据库的解析

写一个excution标签:设置允许移动生成的文件,允许自动覆盖文件(实际工作中不可以)

写一个configuration标签:指定mybatis generator 配置文件的路径 }

1 
 2 
 3 
 5  4.0.0
 6 
 7  org.example
 8  Spike
 9  1.0-SNAPSHOT
 10 
 11  Spike
 12  
 13  http://www.example.com
 14 
 15 
 16  org.springframework.boot
 17  spring-boot-starter-parent
 18  2.0.5.RELEASE
 19 
 20 
 21  
 22   UTF-8
 23   1.8
 24   1.8
 25  
 26 
 27  
 28   
 29    org.springframework.boot
 30    spring-boot-starter-web
 31   
 32   
 33    mysql
 34    mysql-connector-java
 35    5.1.6
 36   
 37   
 38    com.alibaba
 39    druid
 40    1.1.3
 41   
 42   
 43    org.mybatis.spring.boot
 44    mybatis-spring-boot-starter
 45    1.3.1
 46   
 47   
 48    junit
 49    junit
 50    4.11
 51    test
 52   
 53   
 54    org.mybatis.generator
 55    mybatis-generator-core
 56    1.3.5
 57   
 58  
 59 
 60  
 61   
 62    
 63     
 64     
 65      maven-clean-plugin
 66      3.1.0
 67     
 68     
 69     
 70      maven-resources-plugin
 71      3.0.2
 72     
 73     
 74      maven-compiler-plugin
 75      3.8.0
 76     
 77     
 78      maven-surefire-plugin
 79      2.22.1
 80     
 81     
 82      maven-jar-plugin
 83      3.0.2
 84     
 85     
 86      maven-install-plugin
 87      2.5.2
 88     
 89     
 90      maven-deploy-plugin
 91      2.8.2
 92     
 93 
 94     
 95     
 96      maven-site-plugin
 97      3.7.1
 98     
 99     
100      maven-project-info-reports-plugin
101      3.0.0
102     
103 
104     
105      org.mybatis.generator
106      mybatis-generator-maven-plugin
107      1.3.5
108      
109
110 org.mybatis.generator
111 mybatis-generator-core
112 1.3.5
113
114
115 mysql
116 mysql-connector-java
117 5.1.6
118
119      
120      
121
122 mybatis generator
123 package
124 
125  generate
126 
127
128      
129      
130
131true
132
133true
134
135
136 src/main/resource/mybatis-generator.xml
137
138      
139     
140 
141    
142   
143  
144 

步骤5:创建mysql底层的数据库与相关表格

1.创建数据库spike

2.创建一个user_info表格

3.创建一个user_password表格,并设置user_id为外键关联user_info的id

步骤6:在步骤4中,我们最后指定了mybatis generator 配置文件的路径,于是我们在指定路径(resource目录下)创建一个mybatis generator.xml,并进行如下配置:




  
    
    
    
    
    
      
      
    
    
    
      
    
    
    
      
    
    
    

步骤7:根据步骤6中指定的位置,我们在org.example目录下新建一个dataobject的包,一个dao包。并测试是否能够成功生成相应的文件:

run——edit configurations——+maven——command line:mybatis-generator:generate——apply

然后我们运行这个新建的命令,可以看到resources/mapping下多了两个文件:

dataobject包与dao包下生成了如下文件:

手动删除两个Example文件。

步骤8:为了接入mybatis对应mysql的数据源,我们继续编写application.properties文件

 server.port = 8090
 mybatis.mapperLocations = classpath:mapping/*.xml
 
 spring.datasource.name = Spike
 spring.datasource.url = jdbc:mysql://127.0.0.1:3306/Spike
 spring.datasource.username = root
 spring.datasource.password = 0322
 
 #使用druid数据源
 spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
 spring.datasource.driverClassName = com.mysql.jdbc.Driver

步骤9:回到app.java

将@EnableAutoConfiguration注解改为@SpringBootApplication(scanbasePackages = "org.example"),作用是将app交给spring托管,并且指定为主启动类。

添加注解@MapperScan("org.example.dao"),把dao存放的地方设置在对应注解下面。

最后,写一个方法来测试我们的搭建工作是否完成,(事先在表格中添加一条数据)

 package org.example; 
 import org.example.dao.UserDoMapper;
 import org.example.dataobject.UserDo;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 //SpringBoot会帮我们启动tomcat,并加载默认配置
 @SpringBootApplication(scanbasePackages = {"org.example"})
 
 //SpringMVC相关配置
 @RestController
 @MapperScan("org.example.dao")
 public class App {
   @Autowired
   private UserDoMapper userDoMapper;
 
   @RequestMapping("/")
   public String home(){
     UserDo userDo = userDoMapper.selectByPrimaryKey(1);
     if(userDo == null){
      return "用户对象不存在";
     }else{
return userDo.getName();
     } 
   }
   public static void main( String[] args ){
     //控制台输出
     System.out.println( "Hello World!" );
     SpringApplication.run(App.class,args);
   }
 }
app.java

打开http://localhost:8090/,我们可以看到页面上显示了我们添加的数据中name字段的内容。

三、用户模块开发

1.使用SpingMVC模式开发用户信息

步骤1:补全框架结构:

步骤2:service层的编写:

UserService接口:

package org.example.service;
import org.example.service.model.UserModel;
public interface UserService {
  UserModel getUserById(Integer id);
}

UserService实现类:

@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserDoMapper userDoMapper;

  @Autowired
  private UserPasswordDOMapper userPasswordDOMapper;

  @Override
  public UserModel getUserById(Integer id) {
    UserDo userDo = userDoMapper.selectByPrimaryKey(id);
    if(userDo == null){
      return null;
    }
    //通过用户id获取对应的用户加密密码信息
    UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDo.getId());
    return convertFromDataObject(userDo,userPasswordDO);
  }

  public UserModel convertFromDataObject(UserDo userDo, UserPasswordDO userPasswordDO) {
    if(userDo == null){
      return null;
    }
    UserModel userModel = new UserModel();
    BeanUtils.copyProperties(userDo,userModel);
    if(userPasswordDO != null){
      userModel.setEncriptPassword(userPasswordDO.getEncriptPassword());
    }
    return userModel;
  }
}

UserModel类:存放数据库的所有对应字段与getters&setters,用于service层与数据库数据的解耦,使service层无法直接接触数据库

1 package org.example.service.model;
 2 
 3 public class UserModel {
 4   private Integer id;
 5   private String name;
 6   private Byte gender;
 7   private Integer age;
 8   private String telephone;
 9   private String registerMode;
10   private String thirdPartyId;
11   private String encriptPassword;
12 
13   public Integer getId() {
14     return id;
15   }
16 
17   public void setId(Integer id) {
18     this.id = id;
19   }
20 
21   public String getName() {
22     return name;
23   }
24 
25   public void setName(String name) {
26     this.name = name;
27   }
28 
29   public Byte getGender() {
30     return gender;
31   }
32 
33   public void setGender(Byte gender) {
34     this.gender = gender;
35   }
36 
37   public Integer getAge() {
38     return age;
39   }
40 
41   public void setAge(Integer age) {
42     this.age = age;
43   }
44 
45   public String getTelephone() {
46     return telephone;
47   }
48 
49   public void setTelephone(String telephone) {
50     this.telephone = telephone;
51   }
52 
53   public String getRegisterMode() {
54     return registerMode;
55   }
56 
57   public void setRegisterMode(String registerMode) {
58     this.registerMode = registerMode;
59   }
60 
61   public String getThirdPartyId() {
62     return thirdPartyId;
63   }
64 
65   public void setThirdPartyId(String thirdPartyId) {
66     this.thirdPartyId = thirdPartyId;
67   }
68 
69   public String getEncriptPassword() {
70     return encriptPassword;
71   }
72 
73   public void setEncriptPassword(String encriptPassword) {
74     this.encriptPassword = encriptPassword;
75   }
76 }

步骤3:修改UserPasswordDOMapper.xml,添加一个selectByUserId操作的配置