后端:Java + Spring Boot
数据库:MySQL
前端:Vue.js
数据库连接:JPA (Java Persistence API)
blog-app/
├── backend/
│ ├── src/main/java/com/example/blogapp/
│ │ ├── BlogApplication.java
│ │ ├── config/
│ │ │ └── DatabaseConfig.java
│ │ ├── controller/
│ │ │ └── BlogController.java
│ │ ├── model/
│ │ │ └── Blog.java
│ │ ├── repository/
│ │ │ └── BlogRepository.java
│ │ └── service/
│ │ └── BlogService.java
│ ├── src/main/resources/
│ │ ├── application.properties
│ │ └── data.sql
│ └── pom.xml
└── frontend/
├── public/
├── src/
│ ├── App.vue
│ ├── main.js
│ └── api.js
├── package.json
└── vue.config.js
// backend/src/main/java/com/example/blogapp/BlogApplication.java
package com.example.blogapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}
// backend/src/main/java/com/example/blogapp/config/DatabaseConfig.java
package com.example.blogapp.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories(basePackages = "com.example.blogapp.repository")
public class DatabaseConfig {
}
// backend/src/main/java/com/example/blogapp/model/Blog.java
package com.example.blogapp.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Blog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
// backend/src/main/java/com/example/blogapp/repository/BlogRepository.java
package com.example.blogapp.repository;
import com.example.blogapp.model.Blog;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BlogRepository extends JpaRepository
}
// backend/src/main/java/com/example/blogapp/service/BlogService.java
package com.example.blogapp.service;
import com.example.blogapp.model.Blog;
import com.example.blogapp.repository.BlogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BlogService {
@Autowired
private BlogRepository blogRepository;
public List
return blogRepository.findAll();
}
public Blog saveBlog(Blog blog) {
return blogRepository.save(blog);
}
}
// backend/src/main/java/com/example/blogapp/controller/BlogController.java
package com.example.blogapp.controller;
import com.example.blogapp.model.Blog;
import com.example.blogapp.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/blogs")
public class BlogController {
@Autowired
private BlogService blogService;
@GetMapping
public List
return blogService.getAllBlogs();
}
@PostMapping
public Blog saveBlog(@RequestBody Blog blog) {
return blogService.saveBlog(blog);
}
}
# backend/src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/blogdb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
npm install -g @vue/cli
vue create frontend
cd frontend
// frontend/src/main.js
import Vue from 'vue';
import App from './App.vue';
Vue.config.productionTip = false;
new Vue({
render: h => h(App),
}).$mount('#app');
{{ blog.content }}
My Blog
{{ blog.title }}