参考资料:
https://blog.csdn.net/shaofengzong/article/details/121191018
http://t.zoukankan.com/yuboon-p-11919329.html
需求背景:SpringBoot打包后,可以直接运行,但维护部署不方便,体积庞大,不利于传输
做法:将依赖库与项目的class进行分离打包(一定保证 执行命令 的目录 与项目jar、lib、config都在同一目录下)
1、数据库操作 1.1 建立单表create database tasks; use tasks; drop table if EXISTS user; create table user( userId BigInt unsigned not null primary key auto_increment, username varchar(100), userage int );1.2 建立数据库权限用户
create user george@localhost IDENTIFIED by '123456'; use tasks; grant all PRIVILEGES on tasks.* to george@localhost; show GRANTs for george@localhost;2、建立springboot项目 2.1 搭建基本的springboot框架项目(只有一个user表)
# 应用名称 spring.application.name=Demo # 应用服务 WEB 访问端口 server.port=8080 # 数据库驱动: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 数据源名称 spring.datasource.name=defaultDataSource # 数据库连接地址 spring.datasource.url=jdbc:mysql://localhost:3306/tasks?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8 # 数据库用户名&密码: spring.datasource.username=george spring.datasource.password=123456
项目架构:
4.0.0 com.example Demo 0.0.1-SNAPSHOT Demo Demo 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.mybatis mybatis 3.5.7 org.mybatis mybatis-spring 2.0.6 com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-starter-freemarker io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0 org.projectlombok lombok true com.baomidou mybatis-plus-generator 3.5.1 org.apache.velocity velocity 1.7 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.example.demo.DemoApplication repackage repackage
mvn clean package -Dmaven.test.skip=true
结果:
3.2 主包和依赖包分离 3.2.1 修改pom.xml文件,实现目的4.0.0 com.example Demo 0.0.1-SNAPSHOT Demo Demo 1.8 UTF-8 UTF-8 2.3.7.RELEASE lib/ bin/ config/ F:IntellijIdea_workplacespringbootValidatetarget org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.mybatis mybatis 3.5.7 org.mybatis mybatis-spring 2.0.6 com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-starter-freemarker io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0 org.projectlombok lombok true com.baomidou mybatis-plus-generator 3.5.1 org.apache.velocity velocity 1.7 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.apache.maven.plugins maven-jar-plugin 2.4 ${env.LEARN_HOME}/${output.jar.file.path} *.properties *.yml *.xml *.txt true false true ../${output.dependence.file.path} com.example.demo.DemoApplication ../${output.resource.file.path} org.apache.maven.plugins maven-dependency-plugin copy-dependencies package copy-dependencies ${env.LEARN_HOME}/${output.dependence.file.path} false false true true true org.apache.maven.plugins maven-resources-plugin 2.6 copy-resources package copy-resources src/main/resources *.properties *.yml *.xml *.txt ${env.LEARN_HOME}/${output.resource.file.path}
mvn clean package -Dmaven.test.skip=true
结果:
备注:bin/ ,config/,lib/, 文件夹需要同时存在
3.2.2 修改数据库用户密码和config内配置文件- 数据库改密码
ALTER USER 'george'@'localhost' IDENTIFIED BY '123' PASSWORD EXPIRE NEVER; ALTER USER 'george'@'localhost' IDENTIFIED BY '123';
- 配置文件
# 应用名称 spring.application.name=Demo # 应用服务 WEB 访问端口 server.port=8080 # 数据库驱动: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 数据源名称 spring.datasource.name=defaultDataSource # 数据库连接地址 spring.datasource.url=jdbc:mysql://localhost:3306/tasks?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8 # 数据库用户名&密码: spring.datasource.username=george # 修改密码,仍然可以!!! spring.datasource.password=123



