🌕博客x主页:己不由心王道长🌕!
🌎文章说明:SpringBoot关于程序的打包和运行🌎
✅系列专栏:spring
🌴本篇内容:基于Windows对程序进行打包和运行🌴
☕️每日一语:世上有很多不可能,不过不要在你未尽全力之前下结论。☕️
🕤作者详情:作者是一名双非大三在校生,喜欢Java,欢迎大家探讨学习,喜欢的话请给博主一个三连鼓励。🕤
🚩 交流社区:己不由心王道长(优质编程社区)
我个人认为,学习一个知识点或者做一个项目,我们得有一个清晰的思路。知道自己要做什么。这个时候你就得把思路理清,而理清思路又不至于丢失的方法就是画出流程图。这里推荐一些好的软件,如:Xmind、processOn等都值得入手,上手快速。
本次讲解整体流程:

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

一、建表:
create table user(
id int(2),
name varchar(15),
age int(3),
email varchar(30),
password varchar(20)
);
二、插入数据:
insert into user(id,name,age,email,password) values(1,"Jone",18,"test1@qq.com","775033");
三、验证




1、添加IDEA没有提供选择的技术或者其他需要的技术
整体依赖坐标
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.5 com.example SpringBoot-development 0.0.1-SNAPSHOT SpringBoot-development SpringBoot-development 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 com.mysql mysql-connector-j runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.baomidou mybatisplus-spring-boot-starter 1.0.5 com.alibaba druid 1.2.8 javax.servlet.jsp javax.servlet.jsp-api 2.2.1 provided javax.servlet.jsp.jstl jstl-api 1.2 org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
2、把Application启动类提到上一个目录
3、把application.properties文件改为application.yml


package com.example.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author 不止于梦想* @date 2022/10/29 21:53*/@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;private String Username;private Integer age;private String email;
}
一、开发接口
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*/
@Mapper
public interface UserMapper {//通过用户名和密码查询User selectByNameAndPwd(@Param("username") String username,@Param("password") String password);//添加用户int insertByUser(@Param("user") User user);
}
四、测试

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);
}
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*/
@Service
public class ImplUserService implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User selectByNameAndPwd(String name, String password) {return userMapper.selectByNameAndPwd(name,password);}@Overridepublic int insertUser(User user) {return userMapper.insertByUser(user);}
}
测试:

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 {@AutowiredUserService 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";}
}
测试:当账号密码存在,跳转成功页面

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

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

小结:

还有一个需要注意的点就是,打包我们的项目,需要导入一个插件,不过这个插件在我们创建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处都取消勾选,这样就万无一失了。
大家又掌握一个小方法。
