spring boot实现@mapper对数据库表的增删改查

1.连接数据库,springboot为数据库名称

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2.创建Article实体类

    private Integer id;
    private String title;
    private String content;

3.创建ArticleMapper接口类

(1)增(根据实体类article属性进行操作)

public int insertComment(Article article);

(2)删(根据实体类id操作)

public int deleteComment(Integer id);

(3)改(根据实体类id和content两个属性执行操作)

public int updateComment(Integer id, String content);

(4)查(根据id查找)

public Article findById(Integer id);

4.创建 badGuy 类,并在实体方法外写上@RestController注解

(1)引入ArticleMapper接口

@Autowired
private ArticleMapper articleMapper;

(2)完成对接口类中的增操作

@GetMapping("/add")
 public String badGuy1() {
      Article article = new Article();
     article.setContent("简介:一个程序员的成长史!");
     article.setTitle("一本好书");
     articleMapper.insertComment(article);
     return "添加成功";

}

(3)完成对接口类中的删操作

@GetMapping("/delete")

  public String badGuy2() {
      articleMapper.deleteComment(12);
      return "删除成功";
   }

(4)完成对接口类中的改操作

@GetMapping("/update")
   public String badGuy3() {
       articleMapper.updateComment(13,"修改成功");
       return "更新成功";
     }

(5)完成对接口类中的查操作

@GetMapping("/select")
    public String badGuy4() {
       Article article = articleMapper.findById(11);
        System.out.println(article);
        return "查找成功";
        }

加油!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值