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 "查找成功";
}
加油!!!