一、阿里云服务器安装MySQL
① 阿里云服务器操作系统查看:
② 推荐安装流程?阿里云服务器 linux系统安装mysql8.0.20,超详细步骤_长腿欧巴~的博客-CSDN博客
其中需要注意的是
(1)强力卸载之前安装mysql的命令:
?yum remove mysql mysql-server mysql-libs mysql-server?
?查询mysql相关文件的命令:
find / -name mysql
(2)自己新建编译的my.cnf配置文件,他给出的有点问题,会出错,给一个我改了之后能正确运行的。
他给的主要是两个地方打错了,一个是mysql.sock,还有一个是max_allowed_packet=16M这两个地方有错误。
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/tmp/mysql.sock
[mysqld]
# skip-name-resolve
# 设置3306端口
port=3306
socket=/tmp/mysql.sock
# 是否需要密码登录
# skip-grant-tables
# 设置mysq1的安装目录
basedir=/usr/local/mysql
# 设置mysq1数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的Iatin1字符秀
character-set-server=utf8
# 创建新表时将使用的默认存储引随
default-storage-engine=INNODB
# lover_case_table_name=1
max_allowed_packet=16M
default-authentication-plugin=mysql_native_password
?(3)MySQL服务启动命令:
service mysqld start
二、创建一个spring boot项目
2.1 项目创建
左上角File->New->Project创建一个新项目
就先选个SpringWeb即可
?
?2.2 项目格式
项目格式如下:
① 在src/main/java/com.example.项目名/下新建5个文件夹,分别存放controller【控制层】、dao【数据交互层】、entity【实体类】、service【服务层】、uitl/resp【工具类/统一返回接口】的文件。
② 在resource目录下新建3个配置文件,dev和test是分别为开发环境和线上测试环境做的配置。
?③ pom文件可以根据自己的喜好配置:
<?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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>websiteback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>websiteback</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库相关依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!--Druid的starter-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<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>
</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>
③ 配置文件配置
application.yaml配置文件配置
?
server:
port: xxxxxx
spring:
profiles:
active: dev
mybatis-plus:
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名
# 配置日志 选择了默认的控制台输出 如果选择log4j或slf4j,需要导入包
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
call-setters-on-nulls: true
applicontion-dev.yaml配置文件配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=utf-8
username: root
password: "你的密码"
driver-class-name: com.mysql.cj.jdbc.Driver
?2.3 项目controller、dao、entity、util模板
① controller模板
@CrossOrigin
@RequestMapping("/calendar")
@RestController
public class CalendarController {
@Resource
private CalendarService calendarService;
@GetMapping("/query")
@ResponseBody
public Resp queryAll(
@RequestParam(value = "calendarVal") String calendarVal
){
QueryWrapper<Calendar> wrapper = new QueryWrapper<>();
wrapper.eq("calendar_val",calendarVal);
List<Calendar> res = calendarService.queryAll(wrapper);
if (res == null) {
return Resp.error().setMsg("无数据");
} else {
return Resp.success().setData(res).setMsg("成功");
}
}
}
② service层模板
service层接口
public interface CalendarService {
List<Calendar> queryAll(QueryWrapper<Calendar> wrapper);
}
service层接口的实现
@Service
public class CalendarServiceImpl implements CalendarService {
@Resource
private CalendarDao calendarDao;
@Override
public List<Calendar> queryAll(QueryWrapper<Calendar> wrapper) {
List<Calendar> list = calendarDao.selectList(wrapper);
return list;
}
}
③ entity模板
@Data
@TableName("calendar")
public class Calendar {
@TableId(type = IdType.AUTO)
private Long id;
private String calendarVal;
private String calendarNote;
@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
private String createTime;
@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
private String updateTime;
}
③ Dao层模板
@Mapper
public interface CalendarDao extends BaseMapper<Calendar> {
}
④ 工具类模板
CodeEum
public enum CodeEum {
/**
* 枚举定义
*/
SUCCESS(200, "ok"),
ERROR(1000, "系统错误"),
PARAM_ERROR(1010, "参数错误");
/**
* 常量定义
*/
public static int CODE_SUCCESS = 200;
public static int CODE_ERROR = 1000;
public static int CODE_PARAM_ERROR = 1010;
private int code;
private String msg;
private CodeEum(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Resp返回
public class Resp {
public static Resp PARAM_WRONG;
private int code;
private String msg;
private Object data;
public Resp() {
}
private Resp(CodeEum codeEum) {
this.code = codeEum.getCode();
this.msg = codeEum.getMsg();
}
public static Resp success(CodeEum codeEum) {
return new Resp(codeEum);
}
public static Resp success() {
return new Resp(CodeEum.SUCCESS);
}
public static Resp error(CodeEum codeEum) {
return new Resp(codeEum);
}
public static Resp error() {
return new Resp(CodeEum.ERROR);
}
public static Resp info(CodeEum codeEum) {
return new Resp(codeEum);
}
public boolean respSuccess() {
return this.code == 0;
}
public Resp put(String key, Object value) {
if (key != null && key.length() != 0) {
if (this.data == null) {
this.data = new HashMap();
((Map)this.data).put(key, value);
} else if (this.data instanceof Map) {
((Map)this.data).put(key, value);
}
return this;
} else {
return this;
}
}
public Resp setData(Object data) {
this.data = data;
return this;
}
public static Resp getParamWrong() {
return PARAM_WRONG;
}
public static void setParamWrong(Resp paramWrong) {
PARAM_WRONG = paramWrong;
}
public String getMsg() {
return this.msg;
}
public Resp setMsg(String msg) {
this.msg = msg;
return this;
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return this.data;
}
public String toString() {
return "Resp{errorCode=" + this.code + ", msg='" + this.msg + '\'' + ", data=" + this.data + '}';
}
static {
PARAM_WRONG = new Resp(CodeEum.PARAM_ERROR);
}
}
PageResp分页返回
public class PageResp extends Resp{
// 数据总条目数
private int totalCount;
// 当前页码
private int currentPage;
// 数据总页数
private int totalPage;
public static PageResp success() {
return new PageResp(CodeEum.SUCCESS);
}
public static PageResp error() {
return new PageResp(CodeEum.ERROR);
}
public PageResp setPage(Integer pageCount, Integer currentPage, int totalCount) {
this.totalCount = totalCount;
if (pageCount == null || currentPage == null) {
return this;
}
this.currentPage = currentPage;
this.totalPage = totalCount / pageCount;
if (totalCount % pageCount != 0) {
this.totalPage = this.totalPage + 1;
}
return this;
}
@Override
public PageResp setData(Object data) {
super.setData(data);
return this;
}
@Override
public PageResp setMsg(String msg) {
super.setMsg(msg);
return this;
}
private PageResp(CodeEum codeEum) {
this.setCode(codeEum.getCode());
this.setMsg(codeEum.getMsg());
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
2.4 springboot 项目打包
① 点击右侧Maven->项目名->Lifecycle
② 先点击clean再点击package即可
③ 小蓝色的闪电标志为跳过测试,有时候这个不选中会报错
④? 打包后的文件可以在项目下/target里面找到jar包。
三、打包好后的jar在服务器上部署
3.1 安装jdk
如果你的阿里云服务器上没安装JDK,那么就要按一个,版本和你springboot项目里面的pom配置的一样即可。
?推荐安装教程:
Linux下安装Java环境_穷少年的博客-CSDN博客_linux安装java环境
这个教程有一个地方可以改改,下载只用下JDK就可以了
Java Downloads | Oracle
?解压命令对应换为
tar -zvxf jdk-8u333-linux-x64.tar.gz
其中他的那个路径配置就要换一下:
#set java environment
JAVA_HOME=/usr/java/jdk1.8.0_333
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
放在etc/profile的位置:
?3.2 启动jar包
① 随便找一个位置把你的jar包传上去
② 在这个目录里面启动命令即可,日志文件放在server.log中 。
nohup java -jar 项目名.jar >server.log 2>&1
?
③ 可以通过isof指令,查看一个进程
?(1)isof安装
yum install lsof
(2)isof查看占用端口进程,这个会返回一个PID值,通过这个值可以关闭这个进程
lsof -i:{端口号}
(3)关闭进程?
kill -9 {PID}
四、总结
从早上十点钟弄到晚上七点,其中4个小时都是在配置数据库,真的是服了,找了半天才找到一个能用的方法解决问题,索然无味🐷
|