用IDEA创建Spring项目实现对数据库的增删改查操作

1.创建项目

New-》Project-》Spring Initializr-》next-》next-》

下图设置项目名

2.点击Finish,在pom.xml中添加依赖(数据库版本改成自己的)

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>my
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例,使用 Vue.js 和 Spring Boot 来连接后台 IDEA 进行数据库增删改查操作: 首先,在前端 Vue.js 中,我们需要定义一个可以进行增删改查操作的表格。可以参考以下示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue Spring Boot Example</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <h1>Vue Spring Boot Example</h1> <form> <label> Name: <input type="text" v-model="newItem.name"> </label> <label> Description: <input type="text" v-model="newItem.description"> </label> <button @click.prevent="addItem()">Add Item</button> </form> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Description</th> <th></th> </tr> </thead> <tbody> <tr v-for="item in items"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> <td>{{ item.description }}</td> <td> <button @click.prevent="deleteItem(item)">Delete</button> </td> </tr> </tbody> </table> </div> <script src="app.js"></script> </body> </html> ``` 接下来,在 `app.js` 文件中,我们需要定义 Vue 实例,并在其中定义增删改查的方法: ```javascript var app = new Vue({ el: '#app', data: { items: [], newItem: { name: '', description: '' } }, methods: { getItems: function() { axios.get('/items') .then(response => { this.items = response.data; }) .catch(error => { console.log(error); }); }, addItem: function() { axios.post('/items', this.newItem) .then(response => { this.getItems(); this.newItem.name = ''; this.newItem.description = ''; }) .catch(error => { console.log(error); }); }, deleteItem: function(item) { axios.delete('/items/' + item.id) .then(response => { this.getItems(); }) .catch(error => { console.log(error); }); } }, mounted: function() { this.getItems(); } }); ``` 注意,这里的接口地址为 `/items`,并且我们假设服务器会返回一个数组,每个元素包含 `id`、`name` 和 `description` 三个属性。 然后,在后端 Spring Boot 中,我们需要实现对应的增删改查接口。可以参考以下示例代码: ```java @RestController @RequestMapping("/items") public class ItemController { @Autowired private ItemRepository itemRepository; @GetMapping("") public List<Item> getAllItems() { return itemRepository.findAll(); } @PostMapping("") public Item createItem(@RequestBody Item item) { return itemRepository.save(item); } @DeleteMapping("/{id}") public void deleteItem(@PathVariable Long id) { itemRepository.deleteById(id); } } ``` 注意,这里使用了 Spring Data JPA 来访问数据库,需要先定义一个 `ItemRepository` 接口,继承 `JpaRepository<Item, Long>` 并实现对应的增删改查方法。 最后,启动 Spring Boot 应用程序,打开浏览器,访问前端页面,就可以进行增删改查操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值