css3 transaction display,HTML5+CSS3 本地数据库基本

WebSQLDatabase是一种用于在浏览器中创建和操作本地数据库的技术。虽然已被废弃,但大多数现代浏览器仍支持该技术。本文介绍了WebSQLDatabase的基本概念,包括其三个核心方法:openDatabase,用于创建数据库对象;Transaction,用于控制事务提交或回滚。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML5+CSS3 本地数据库基本

Web SQL Database(本地数据库)是一个已经废弃的规范,但是鉴于除了IE和Firefox,其他浏览器都已经实现了Web SQL Database,并且它还具有一些Storage(存储)所不具有的特性,所以还是值得了解一下的。

Web SQL Database 引入了一套使用 SQL 来操纵客户端数据库(client-side database)的 API。这些API是异步的(asynchronous),所以作者在使用这套API时会发现匿名函数非常有用。规范中所使用的SQL语言为SQLite 3.6.19。

其中,SQLite是一款轻型的数据库,是遵循ACID的关系型数据库管理系统。它的设计目标是嵌入式的,它占用资源非常低,只需要几百K字节的内存就可以了。 它能够支持主流操作系统(Windows、Linux、Unix等操作系统),同时能够跟很多程序语言相结合,如C#,PHP,Java,JavaScript等,还有ODBC接口,比起Mysql,PostgreSQL这两款开源的数据库管理系统来说,它的处理速度更快。 在Web SQL Database规范中定义的三个核心方法:

1.openDatabase方法

这个方法使用现有数据库或新建数据库来创建数据库对象。

1-30-jpg_6_0_______-586-0-0-586.jpg

dbName 数据库实例名称,自定义。 dbVersion 数据库版本,目前为1.0。 dbDisplayName 数据库显示的名称。 dbEstimatedSize 数据库预估的大小。 callbackFun 回调函数。

例如,通过“var db=openDatabase( 'db', '1.0' , 'first database',2*1024*1024);”语句可以打开数据库。

2.Transaction方法

这个方法允许用户根据情况控制事务提交或回滚。

1-30-jpg_6_0_______-586-0-30-586.jpg

语法:

上述语法各参数含义如下:

callbackFun 回调函数。 errorCallbackFun 发生错误时的回调函数。 successCallbackFun 成功执行时的回调函数。

1-30-jpg_6_0_______-586-0-60-586.jpg

好的,下面是一个简单的实现过程。 前端部分: 1. 使用 Vue CLI 创建一个新项目,然后安装 Element UI、Axios 和 Vue Router。 ``` vue create my-project cd my-project npm install element-ui axios vue-router --save ``` 2. 在 `src` 目录下创建一个 `views` 文件夹,里面放置一个 `Comment.vue` 组件。 ```vue <template> <div> <div class="comment-header"> <h3>评论</h3> <el-button type="primary" @click="showForm = true">添加评论</el-button> </div> <div class="comment-body"> <el-card v-for="comment in comments" :key="comment.id"> <div class="comment-info"> <span class="comment-author">{{ comment.author }}</span> <span class="comment-date">{{ comment.date }}</span> </div> <div class="comment-content">{{ comment.content }}</div> <div class="comment-actions"> <el-button size="small" type="text" @click="editComment(comment)">编辑</el-button> <el-button size="small" type="text" @click="deleteComment(comment)">删除</el-button> <el-button size="small" type="text" @click="likeComment(comment)"> {{ comment.likes }} <i class="el-icon-thumb-up"></i> </el-button> </div> </el-card> </div> <el-dialog title="添加评论" :visible.sync="showForm" width="50%" center> <el-form :model="newComment" label-width="80px"> <el-form-item label="用户名"> <el-input v-model="newComment.author"></el-input> </el-form-item> <el-form-item label="评论内容"> <el-input type="textarea" v-model="newComment.content"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="showForm = false">取消</el-button> <el-button type="primary" @click="addComment">确定</el-button> </div> </el-dialog> </div> </template> <script> import axios from &#39;axios&#39;; export default { name: &#39;Comment&#39;, data() { return { comments: [], showForm: false, newComment: { author: &#39;&#39;, content: &#39;&#39; } }; }, methods: { fetchComments() { axios.get(&#39;/api/comments&#39;).then(response => { this.comments = response.data; }).catch(error => { console.log(error); }); }, addComment() { axios.post(&#39;/api/comments&#39;, this.newComment).then(response => { this.comments.push(response.data); this.newComment.author = &#39;&#39;; this.newComment.content = &#39;&#39;; this.showForm = false; }).catch(error => { console.log(error); }); }, editComment(comment) { // TODO }, deleteComment(comment) { axios.delete(`/api/comments/${comment.id}`).then(response => { const index = this.comments.indexOf(comment); this.comments.splice(index, 1); }).catch(error => { console.log(error); }); }, likeComment(comment) { axios.post(`/api/comments/${comment.id}/like`).then(response => { comment.likes = response.data.likes; }).catch(error => { console.log(error); }); } }, mounted() { this.fetchComments(); } }; </script> <style scoped> .comment-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; } .comment-info { display: flex; justify-content: space-between; margin-bottom: 8px; } .comment-author { font-weight: bold; } .comment-date { color: #999; } .comment-content { margin-bottom: 8px; } .comment-actions { display: flex; justify-content: flex-end; } </style> ``` 3. 在 `src` 目录下创建一个 `router` 文件夹,里面放置一个 `index.js` 路由配置文件。 ```js import Vue from &#39;vue&#39;; import VueRouter from &#39;vue-router&#39;; import Comment from &#39;@/views/Comment.vue&#39;; Vue.use(VueRouter); const routes = [ { path: &#39;/&#39;, name: &#39;Comment&#39;, component: Comment } ]; const router = new VueRouter({ mode: &#39;history&#39;, base: process.env.BASE_URL, routes }); export default router; ``` 4. 在 `src` 目录下创建一个 `main.js` 入口文件,里面配置 Axios 和 Element UI。 ```js import Vue from &#39;vue&#39;; import App from &#39;./App.vue&#39;; import router from &#39;./router&#39;; import axios from &#39;axios&#39;; import ElementUI from &#39;element-ui&#39;; import &#39;element-ui/lib/theme-chalk/index.css&#39;; Vue.config.productionTip = false; axios.defaults.baseURL = &#39;http://localhost:8080&#39;; Vue.prototype.$http = axios; Vue.use(ElementUI); new Vue({ router, render: h => h(App) }).$mount(&#39;#app&#39;); ``` 5. 创建一个 `public` 文件夹,里面放置一个 `index.html` 文件,用于渲染 Vue 应用。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Project</title> </head> <body> <div id="app"></div> <script src="/dist/js/chunk-vendors.js"></script> <script src="/dist/js/app.js"></script> </body> </html> ``` 后端部分: 1. 使用 Spring Initializr 创建一个新项目,然后添加 Spring Web、Spring Data JPA 和 H2 Database 的依赖。 2. 在 `src/main/java` 目录下创建一个 `com.example.myproject` 包,里面放置一个 `MyProjectApplication.java` 入口文件。 ```java package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyProjectApplication { public static void main(String[] args) { SpringApplication.run(MyProjectApplication.class, args); } } ``` 3. 在 `src/main/java` 目录下创建一个 `com.example.myproject.controller` 包,里面放置一个 `CommentController.java` 控制器,用于处理评论相关的 API 请求。 ```java package com.example.myproject.controller; import com.example.myproject.entity.Comment; import com.example.myproject.repository.CommentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.transaction.Transactional; import java.util.List; @RestController @RequestMapping("/api/comments") public class CommentController { @Autowired private CommentRepository commentRepository; @GetMapping public List<Comment> getAllComments() { return commentRepository.findAll(); } @PostMapping public Comment createComment(@RequestBody Comment comment) { return commentRepository.save(comment); } @PutMapping("/{id}") public ResponseEntity<Comment> updateComment(@PathVariable Long id, @RequestBody Comment comment) { Comment existingComment = commentRepository.findById(id).orElse(null); if (existingComment == null) { return ResponseEntity.notFound().build(); } existingComment.setAuthor(comment.getAuthor()); existingComment.setContent(comment.getContent()); Comment updatedComment = commentRepository.save(existingComment); return ResponseEntity.ok(updatedComment); } @DeleteMapping("/{id}") public ResponseEntity<Comment> deleteComment(@PathVariable Long id) { Comment comment = commentRepository.findById(id).orElse(null); if (comment == null) { return ResponseEntity.notFound().build(); } commentRepository.delete(comment); return ResponseEntity.ok(comment); } @Transactional @PostMapping("/{id}/like") public ResponseEntity<Comment> likeComment(@PathVariable Long id) { Comment comment = commentRepository.findById(id).orElse(null); if (comment == null) { return ResponseEntity.notFound().build(); } comment.setLikes(comment.getLikes() + 1); Comment updatedComment = commentRepository.save(comment); return ResponseEntity.ok(updatedComment); } } ``` 4. 在 `src/main/java` 目录下创建一个 `com.example.myproject.entity` 包,里面放置一个 `Comment.java` 实体类,用于表示评论。 ```java package com.example.myproject.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.util.Date; @Entity public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String author; private String content; private Integer likes = 0; private Date date = new Date(); public Comment() {} public Comment(String author, String content) { this.author = author; this.content = content; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getLikes() { return likes; } public void setLikes(Integer likes) { this.likes = likes; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } ``` 5. 在 `src/main/java` 目录下创建一个 `com.example.myproject.repository` 包,里面放置一个 `CommentRepository.java` 接口,用于与数据库交互。 ```java package com.example.myproject.repository; import com.example.myproject.entity.Comment; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface CommentRepository extends JpaRepository<Comment, Long> { } ``` 6. 在 `src/main/resources` 目录下创建一个 `application.properties` 配置文件,用于配置 H2 Database。 ``` spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.show-sql=true spring.h2.console.enabled=true spring.h2.console.path=/h2-console ``` 7. 运行项目,访问 `http://localhost:8080/h2-console` 可以进入 H2 Database 控制台,查看评论表的数据。 8. 运行项目,访问 `http://localhost:8080/` 可以进入 Vue 应用,查看评论区的界面效果。 以上就是一个简单的基于 Vue 和 Spring Boot 的评论区模块的实现过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值