基于H2数据库使用Springboot+MyBatis的CounterWebAppV6
一、项目简介
本项目为一个刷新计数器的Web应用,采用了H2文件型数据库作为计数器的基础,以能够让Web应用重启之后,计数不会重置,而是继续计数。本项目采用Spring Boot+MyBatis+Thymeleaf实现。
二、开发软件和技术
开发软件
本项目所使用的开发软件为IDEA。IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境。IntelliJ在业界被公认为最好的java开发工具,尤其在智能代码助手、代码自动提示、重构、JavaEE支持、各类版本工具(git、svn等)、JUnit、CVS整合、代码分析、 创新的GUI设计等方面的功能可以说是超常的。它的旗舰版本还支持HTML,CSS,PHP,MySQL,Python等。免费版只支持Java,Kotlin等少数语言。
技术
1.Spring Boot
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
2.MyBatis
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。
3.Thymeleaf
Thymeleaf是一个现代服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
Thymeleaf的主要目标是提供一种优雅且高度可维护的模板创建方式。为实现这一目标,它以自然模板的概念为基础,将其逻辑注入模板文件,其方式不会影响模板被用作设计原型。这改善了设计沟通,缩小了设计和开发团队之间的差距。
Thymeleaf也从一开始就设计了Web标准 - 特别是HTML5 - 允许您创建完全验证的模板,如果您需要的话。
4.H2数据库
H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式,可以兼容一些主流的数据库,因此采用H2作为开发期的数据库非常方便。H2是纯java编写的,源码大小只有1M左右。
三、项目开发流程
3.1创建项目
- 打开IDEA
- 创建一个SpringBoot的项目
(1)选中File中的New下面的Project…选项
(2)选中Spring Initializr,确定Project SDK为JDK1.8
(3)设置项目,注意Packaging类型和项目名
(4)选择依赖
(5)设置工程名和工程存放目录
(6)完成项目创建
(7)创建文件夹,分别用作不同的功能
3.2项目的开发
3.2.1 配置文件pom.xml
<?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.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CounterWebAppV6</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- thymeleaf的引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<!-- h2数据库的引入 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybitis整合H2-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--H2数据库-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!--tkmybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<!-- 测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2.2 配置文件application.yml
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema.sql #,每次启动程序,程序都会运行schema.sql文件,对数据库的数据操作
data: classpath:db/data.sql #,每次启动程序,程序都会运行data.sql文件,对数据库的数据操作
url: jdbc:h2:mem:test #配置h2数据库的连接地址
username: sa
password: sa
h2:
console:
enabled: true #开启web console功能
3.2.3 index.html
(1)使用Thymeleaf时,需要将html中的命名空间代码修改为以下代码
<html xmlns:th="http://www.thymeleaf.org">
(2)为了实现此Web应用重启后,计数器还能继续计数,因此index中的内容为
<!DOCTYPE html>
<!-- 使用thymeleaf必须引入的命名空间 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>CounterWebAppV6</title>
</head>
<body>
<h1>Maven + Spring MVC Web Project Example</h1>
<h2 th:text="${message}">默认Message</h2>
<h2 th:text="|Counter:${counter}|">默认Counter</h2>
I add this line of text just to test Jenkins continuous deployment process.
</body>
</html>
**注意:**为使的页面显示效果和原来保持一致,需要在页面中进行数据和字符串的拼接
<!--thymeleaf的语法就是使用"||"将所需的部分进行拼接,以此来达到页面效果-->
<!--代码实现如下-->
<h2 th:text="|Counter:${counter}|">默认Counter</h2>
3.2.4 CounterMapper(接口)
用于查找和更改value
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface CounterMaaper {
@Select("select counter_value from counters where counter_id=#{id}") //根据id去查找value
Integer findValueByID(Integer id);
@Update("update counters set counter_value=#{value} where counter_id=1;")//更改id为1的value
void Updatevalue(Integer value);
}
3.2.5 DaoServer
进行返回数据和计数器加一操作
package com.example.demo.model;
import com.example.demo.mapper.CounterMaaper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class DaoServer {
@Resource
private CounterMaaper counterMaaper;//依赖注入mapper
public int retuervalue(){
return counterMaaper.findValueByID(1);
}//返回id为1的value
public void increse(){
counterMaaper.Updatevalue(retuervalue()+1);
}//进行技术使id为1的value+1。
}
3.2.6 ServletInitializer
通过ServletInitializer启动Spring Boot,
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CounterWebAppV6Application.class);
}
}
3.3 测试
3.3.1 CounterWebAppV6Application
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class CounterWebAppV6ApplicationTests {
@Test
void contextLoads() {
}
}
3.3.2 MainTest
测试DaoServer
package com.example.demo;
import com.example.demo.model.DaoServer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MainTest {
@Resource
DaoServer daoServer;
@Test
public void test1() {
System.out.println(daoServer.retuervalue());
daoServer.increse();
System.out.println(daoServer.retuervalue());
}
}
四、项目的部署
- 因为项目采用".jar"的方式进行部署,所以pom.xml中需要进行配置,将标签下的内容写为jar
- 使用Maven对项目进行打包
找到IDEA中Maven的打包界面,开始打包
项目打包成功
- 找到打包好的jar包,jar包的位置在工程目录的target下
- 打开XShell连接阿里云服务器
- 打开Xftp连接阿里云服务器
- 将打包好的项目jar包通过Xftp传到阿里云上的usr下
- 在阿里云上运行项目
使用以下命令以进程的形式运行当前目录下的jar包
nohup java -jar demo-0.0.1-SNAPSHOT.jar &
linux常用命令
# 杀死 进程的pid ,关闭程序
kill -9 22899
# 查看java 运行的几个进程 pid
ps -ef | grep java
部署时出现错误,h2数据库中的表不存在,需要访问云服务器上的h2数据库
通过http://47.106.229.153:8099/h2-console来进行访问,访问时创建表格,即解决错误
# 注意:默认情况下不允许其他远程web访问云服务器上的h2数据库,需要在项目的配置文件中配置如下几行
h2:
console:
enabled: true #开启web console功能
#使用http://localhost:8099/h2-console/ 进入控制台
# settings中设置web-allow-others为true,即可使用非本地web访问云服务器上的h2数据库
settings:
web-allow-others: true
trace: true
部署成功
**注意:**部署后无法访问页面,说明阿里云的安全组没有设置,因为该应用的端口号为8099,所以需要在安全中设置
(1)首先进入阿里云的网站,登录后点击右上角控制台进入页面
(2)找到阿里云服务器下的安全组
(3)找到相应的安全组,点击配置规则
(4)点击右上角的添加安全组
(5)添加安全组规则
五、参考资料
55252)]
(3)找到相应的安全组,点击配置规则
[外链图片转存中…(img-AiBQdJbp-1623901255253)]
(4)点击右上角的添加安全组
[外链图片转存中…(img-dlLTw9DW-1623901255254)]
(5)添加安全组规则
[外链图片转存中…(img-3a6l7mpv-1623901255255)]