电商商城定制开发【SpringBoot】一文掌握程序的打包与运行

文章目录

🌕博客x主页:🌕!
🌎文章说明:SpringBoot电商商城定制开发关于程序的打包和运行🌎
✅系列专栏:
🌴本篇内容:基于Windows电商商城定制开发对程序进行打包和运行🌴
☕️每日一语:电商商城定制开发世上有很多不可能,电商商城定制开发不过不要在你未尽全力电商商城定制开发之前下结论。☕️
🕤作者详情:电商商城定制开发作者是一名双非大三在校生,喜欢Java,电商商城定制开发欢迎大家探讨学习,电商商城定制开发喜欢的话请给博主一个三连鼓励。🕤
🚩 交流社区:(电商商城定制开发优质编程社区)

前言

电商商城定制开发我个人认为,电商商城定制开发学习一个知识点或者做一个项目,我们得有一个清晰的思路。知道自己要做什么。这个时候你就得把思路理清,而理清思路又不至于丢失的方法就是画出流程图。这里推荐一些好的软件,如:Xmind、processOn等都值得入手,上手快速。
本次讲解整体流程:

分析、剧透

本次是通过后端的测试,进行程序的打包和运行,并不牵扯到前端,但是可以基于postman进行测试

程序准备

数据库

一、建表:

create table user(id int(2),name varchar(15),age int(3),email varchar(30),password varchar(20));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、插入数据:

insert into user(id,name,age,email,password) values(1,"Jone",18,"test1@qq.com","775033");
  • 1
  • 2

三、验证

环境搭建

  • 环境搭建是从后端、从底层往上层开发的。就是先数据持久层、然后业务层、然后是控制层。
  • 整体流程图:
创建SpringBoot项目、选择用到的依赖。



1、添加IDEA没有提供选择的技术或者其他需要的技术

  • 我们在选择技术的时候,可能不会一下就完全想到用什么技术来实现我们的功能。而项目已经创建,删除再创建实属不聪明之举。
  • 所以我们需要在创建项目之后再对项目进行一次技术的整合,因为用的是SpringBoot,绝大部分开发用到的技术其实SpringBoot已经帮我们管理好了。
  • 这里就添加一些SpringBoot没有提供的

整体依赖坐标

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <parent>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-parent</artifactId>       <version>2.7.5</version>       <relativePath/> <!-- lookup parent from repository -->   </parent>   <groupId>com.example</groupId>   <artifactId>SpringBoot-development</artifactId>   <version>0.0.1-SNAPSHOT</version>   <name>SpringBoot-development</name>   <description>SpringBoot-development</description>   <properties>       <java.version>1.8</java.version>   </properties>   <!--thymeleaf模板-->   <dependencies>       <dependency>           <groupId>org.springframework.boot</groupId>           <artifactId>spring-boot-starter-thymeleaf</artifactId>       </dependency>       <!--web启动-->       <dependency>           <groupId>org.springframework.boot</groupId>           <artifactId>spring-boot-starter-web</artifactId>       </dependency>       <!--Mybatis整合Spring依赖-->       <dependency>           <groupId>org.mybatis.spring.boot</groupId>           <artifactId>mybatis-spring-boot-starter</artifactId>           <version>2.2.2</version>       </dependency>       <!--数据库依赖-->       <dependency>           <groupId>com.mysql</groupId>           <artifactId>mysql-connector-j</artifactId>           <scope>runtime</scope>       </dependency>       <!--Lombok依赖、简化实体类开发-->       <dependency>           <groupId>org.projectlombok</groupId>           <artifactId>lombok</artifactId>           <optional>true</optional>       </dependency>       <!--测试类依赖-->       <dependency>           <groupId>org.springframework.boot</groupId>           <artifactId>spring-boot-starter-test</artifactId>           <scope>test</scope>       </dependency>       <!--Mybatisplus依赖-->       <dependency>           <groupId>com.baomidou</groupId>           <artifactId>mybatisplus-spring-boot-starter</artifactId>           <version>1.0.5</version>       </dependency>       <!-- 连接池依赖 -->       <dependency>           <groupId>com.alibaba</groupId>           <artifactId>druid</artifactId>           <version>1.2.8</version>       </dependency>       <!-- jsp依赖 -->       <dependency>           <groupId>javax.servlet.jsp</groupId>           <artifactId>javax.servlet.jsp-api</artifactId>           <version>2.2.1</version>           <scope>provided</scope>       </dependency>       <!-- jstl标签库依赖 -->       <dependency>           <groupId>javax.servlet.jsp.jstl</groupId>           <artifactId>jstl-api</artifactId>           <version>1.2</version>       </dependency>   </dependencies>   <!--打包插件、这里不要删除,这个项目的主要目的就是打包-->   <build>       <plugins>           <plugin>               <groupId>org.springframework.boot</groupId>               <artifactId>spring-boot-maven-plugin</artifactId>               <configuration>                   <excludes>                       <exclude>                           <groupId>org.projectlombok</groupId>                           <artifactId>lombok</artifactId>                       </exclude>                   </excludes>               </configuration>           </plugin>       </plugins>   </build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

2、把Application启动类提到上一个目录

  • 这里为什么这么做是有原因的
  • SpringBootApplication类默认扫描本层包及其子包的文件,所以启动类必须在所有类的外面或者在同一层。
    这里启动类在三层架构的同级目录之中。

3、把application.properties文件改为application.yml

  • 这里我们用yml语法,yml语法有很多好处,这里就不再赘述。
完善三层架构基本框架
  • 三层架构:dao/mapper、service、controller。
  • 这里直接添加包即可,本不止于作为一个小结点。
构建实体类
  • 实体类与数据库的字段要一一对应
  • 这里添加了lombok依赖,我们可以看看lombok的强大之处。
package com.example.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * @author 不止于梦想 * @date 2022/10/29 21:53 */@Data@NoArgsConstructor@AllArgsConstructorpublic class User {    private Integer id;    private String Username;    private Integer age;    private String email;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
开发持久层
  • 回想一下我们当时开发两天半时长项目时,持久层用到了两个方法,一个是验证用户名和密码是否存在。
  • 另一个是添加用户,一共两个方法。

一、开发接口

package com.example.mapper;import com.example.pojo.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;/** * @author 不止于梦想 * @date 2022/11/11 14:34 */@Mapperpublic interface UserMapper {    //通过用户名和密码查询    User selectByNameAndPwd(@Param("username") String username,@Param("password") String password);    //添加用户    int insertByUser(@Param("user") User user);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

四、测试

  • 从底层往上开发,一个好处就是可以方便测试数据是否正确。
开发业务层

UserService:

package com.example.service;import com.example.pojo.User;/** * @author 不止于梦想 * @date 2022/11/11 14:51 */public interface UserService {    User selectByNameAndPwd(String name, String password);    int insertUser(User user);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

ImplUserService:

package com.example.service.impl;import com.example.mapper.UserMapper;import com.example.pojo.User;import com.example.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * @author 不止于梦想 * @date 2022/11/11 14:53 */@Servicepublic class ImplUserService implements UserService {    @Autowired    private UserMapper userMapper;    @Override    public User selectByNameAndPwd(String name, String password) {       return userMapper.selectByNameAndPwd(name,password);    }    @Override    public int insertUser(User user) {        return userMapper.insertByUser(user);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

测试:

开发控制层

UserController:

package com.example.controller;import com.example.pojo.User;import com.example.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;/** * @author 不止于梦想 * @date 2022/11/11 15:12 */@Controller@RequestMapping("/user")public class UserController {    @Autowired    UserService userService;    //登录成功跳转到登录页面    @RequestMapping("/login")    public void login(String name, String password) {        ModelAndView mv = new ModelAndView();        User user = userService.selectByNameAndPwd(name, password);        if(user!=null){            //如果用户存在,跳转到登录成功页面            mv.setViewName("succeed");            mv.addObject("user",user);            System.out.println("登录成功");            System.out.println(mv);        }else {            mv.setViewName("login");            System.out.println("登录失败");            System.out.println(mv);        }    }    //注册成功跳转登录页面,注册失败留在注册页面    @RequestMapping("/register")    public void register(User user){//这里没有视图,所有设置为void        //如果影响行数不等于0,则说明加入成功,返回登录页面        int i = userService.insertUser(user);        if(i!=0){            //return "login";        }        //否则注册失败,留在注册界面        //return "register";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

测试:当账号密码存在,跳转成功页面

当密码不正确:跳转登录页面

程序打包和运行

第一,为什么我们需要打包?因为当我们把项目在我们的电脑上写好以后,需要发布项目供客户使用,不能你一写好,关机回家,客户也跟着下线。所以一般会把程序打包,然后在专门的服务器上运行。
程序打包:找到我们的项目的生命周期,先clean,然后执行package。


注意以下输出:


看到BUILD SUCCESS说明我们已经打包成功了。
运行程序:
找到打包好的target目录,右键——>open in——>Explorer


进入到存储的项目target目录中:

在地址栏里输入cmd,敲入Java -jar s+tab键自动补全,运行项目

然后看下图:您猜怎么着?我们刚才的项目运行的时候已经把端口占用了,有没有解决办法?有

教你一招

解决端口占用问题:
一、以临时端口号解决

java -jar 项目 --server.port=临时端口号
启动:可以看到启动是没问题的

而且端口已经变成了8085,我们在上面的时候设置的端口号是8081

可是设置临时端口号也有弊端,弊端就是很混乱,今天这个被占用,明天那个被占用。有没有一劳永逸的办法?没有,但是有一个你掌握了就能解决此类问题的办法。

二:杀死端口占用

#查询端口netstat -ano#查询指定端口netstat -ano|findstr "端口号"#根据进程PID查询进程名称tasklist | findstr "进程PID号"#根据PID杀死任务taskkill /F /PID "进程PID号"# 根据进程名称杀死任务taskkill -f -t -im "进程名称"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

举例:

小结:

还有一个需要注意的点就是,打包我们的项目,需要导入一个插件,不过这个插件在我们创建springboot项目之初就已经导入了。

另外还有一些常见的小问题,如打包之后多出来很多东西,还记得我们在上面打包过程中的test过程吗?不记得了?上去看看,我已经截图放在文章中了,在我们执行打包的时候,一般需要跳过这个test过程,以保证数据的准确性。

找到maven生命周期,选择test,点击闪电符号即可跳过测试。

结尾小结

我这里在调试debug的时候,出现了一些小问题,如下图所示:

问题:Skipped breakpoint at com.sun.proxy.$Proxy74.toString()+0 because it happened inside debugger evaluatio

翻译:跳过com.sun.proxy.$Proxy74.toString()+0处的断点,因为它发生在调试器评估中

看不懂,查看了资料说:IDEA的debugger是默认会在内部将方法执行一次,然后回显提示数据,本意是很好,但有时候会干扰影响结果。

解决方案:file——>settings——>build——>Debugger——>Data Views——>java

把上图中的1取消勾选即可,一般取消这个即可,如果还是不行就把1、2处都取消勾选,这样就万无一失了。
大家又掌握一个小方法。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发