轻轻松松学习SpringBoot2:第二十篇: JDBC操作

前面我们讲解如何使用jpa操作数据库   https://blog.csdn.net/stronglyh/article/details/80904531

今天我们来讲解如何使用原生jdbc的方法操作数据库

定义一个controller,一个repository

其中repository文件中

@Autowired
private JdbcTemplate jdbcTemplate;

controller文件中

@Autowired
private JDBCRepository jdbcRepository;

一:访问

controller和repository代码分别如下

    @GetMapping(value="/jdbc_tests/{id}")
    public Test testList(@PathVariable("id") Integer id){
        return jdbcRepository.getTest(id);
    }
    public void createTest(Integer id,Integer age,String name) {
        jdbcTemplate.update("INSERT INTO test(id,age,name) VALUES (?,?,?)",id,age,name);
    }

然后启动服务,用postman进行访问


二:增加

controller和repository代码分别如下

    @PostMapping(value="/jdbc_tests")
    public void testAdd(@RequestParam("id")Integer id,@RequestParam("age")Integer age,@RequestParam("name")String name){
        jdbcRepository.createTest(id,age,name);
    }
    public void createTest(Integer id,Integer age,String name) {
        jdbcTemplate.update("INSERT INTO test(id,age,name) VALUES (?,?,?)",id,age,name);
    }

我们重启服务,用postman看下效果



ok,新增成功


三:修改

controller和repository代码分别如下

   @PutMapping(value="/jdbc_tests/{id}")
    public void testUpdate(@PathVariable("id") Integer id,@RequestParam("age") Integer age,@RequestParam("name")String name){
        jdbcRepository.updateTest(id,age,name);
    }
    public void updateTest(Integer id,Integer age,String name) {
        jdbcTemplate.update("update test set age=?,name=? where id=?",age,name,id);
    }

我们重启服务,用postman看下效果(注意修改body类型)


我们再来看下数据库结果

非常的ok,id为7的数据发生了改变


四:删除

controller和repository代码分别如下

    @DeleteMapping(value="/jdbc_tests/{id}")
    public void testDelete(@PathVariable("id") Integer id){
        jdbcRepository.deleteTest(id);
    }
    public void deleteTest(Integer id){
        jdbcTemplate.update("delete from test where id=?",id);
    }

我们用postman看下结果



ok了

全部代码如下

package com.example.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class JDBCController {

    @Autowired
    private JDBCRepository jdbcRepository;

    @GetMapping(value="/jdbc_tests/{id}")
    public Test testList(@PathVariable("id") Integer id){
        return jdbcRepository.getTest(id);
    }

    @PostMapping(value="/jdbc_tests")
    public void testAdd(@RequestParam("id")Integer id,@RequestParam("age")Integer age,@RequestParam("name")String name){
        jdbcRepository.createTest(id,age,name);
    }

    @PutMapping(value="/jdbc_tests/{id}")
    public void testUpdate(@PathVariable("id") Integer id,@RequestParam("age") Integer age,@RequestParam("name")String name){
        jdbcRepository.updateTest(id,age,name);
    }

    @DeleteMapping(value="/jdbc_tests/{id}")
    public void testDelete(@PathVariable("id") Integer id){
        jdbcRepository.deleteTest(id);
    }

}
package com.example.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class JDBCRepository{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void createTest(Integer id,Integer age,String name) {
        jdbcTemplate.update("INSERT INTO test(id,age,name) VALUES (?,?,?)",id,age,name);
    }

    public void updateTest(Integer id,Integer age,String name) {
        jdbcTemplate.update("update test set age=?,name=? where id=?",age,name,id);
    }

    public void deleteTest(Integer id){
        jdbcTemplate.update("delete from test where id=?",id);
    }

    public Test getTest(Integer id){
        List<Test> list = jdbcTemplate.query("select * from test where id="+id,new BeanPropertyRowMapper(Test.class));
        if(list!=null && list.size()>0){
            Test test = list.get(0);
            return test;
        }
        return null;
    }

}
欢迎鉴赏

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值