当前位置:   article > 正文

springboot整合mybatis对数据库交互

springboot整合mybatis对数据库交互

首先要配置好MySQL数据库,并且建立af-school数据库。

思路流程是pom文件添加mybatis及mysql数据库驱动依赖--创建application.yml配置文件,并配置数据库参数---建立数据库具体类---建立mybatis接口类--建立数据库操作接口--将数据库操作接口实例化--controller调用数据库实例化类,完成数据库读写。

1、配置pom.xml依赖

  1. <!--mybatis起步依赖-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>3.0.3</version>
  6. </dependency>
  7. <!--mysql驱动依赖-->
  8. <dependency>
  9. <groupId>com.mysql</groupId>
  10. <artifactId>mysql-connector-j</artifactId>
  11. </dependency>

2、创建application.yml配置文件,配置数据库驱动、url,账号及密码

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://192.168.1.101:3306/af_school
  5. username: root
  6. password: a1b2c3

3、在dao包里创建mybatis@Mapper注解接口

  1. @Mapper
  2. public interface StudentMapper {
  3. @Select("select * from af_school.student")
  4. public List<Student> findAll();
  5. @Select("select * from af_school.student where id=#{id}")
  6. public Student findById(Integer id);
  7. }

4、在entity包里创建af_school数据库里的Student类

  1. package com.zw.springboot.springbootmybatis.entity;
  2. /** 本类由 POJO生成器 自动生成于 2024-04-26 22:45:56
  3. 作者:阿发你好 官网: http://afanihao.cn
  4. */
  5. /** INSERT语句 ( 预处理方式 )
  6. INSERT INTO `student`
  7. (`id`, `name`, `sex`, `phone`, `birthday`)
  8. VALUES(?, ?, ?, ?, ?)
  9. */
  10. import java.util.Date;
  11. /** INSERT语句 ( MyBatis方式 )
  12. INSERT INTO `student`
  13. (`id`, `name`, `sex`, `phone`, `birthday`)
  14. VALUES(#{id}, #{name}, #{sex}, #{phone}, #{birthday})
  15. 自增主键: 无
  16. */
  17. public class Student
  18. {
  19. public Integer id ;
  20. public String name ;
  21. public Boolean sex ;
  22. public String phone ;
  23. public Date birthday ;
  24. public Student(){
  25. }
  26. public Student(Integer id,String name,Boolean sex,String phone,Date birthday){
  27. this.id=id;
  28. this.name=name;
  29. this.sex=sex;
  30. this.phone=phone;
  31. this.birthday=birthday;
  32. }
  33. public void setId(Integer id)
  34. {
  35. this.id=id;
  36. }
  37. public Integer getId()
  38. {
  39. return this.id;
  40. }
  41. public void setName(String name)
  42. {
  43. this.name=name;
  44. }
  45. public String getName()
  46. {
  47. return this.name;
  48. }
  49. public void setSex(Boolean sex)
  50. {
  51. this.sex=sex;
  52. }
  53. public Boolean getSex()
  54. {
  55. return this.sex;
  56. }
  57. public void setPhone(String phone)
  58. {
  59. this.phone=phone;
  60. }
  61. public String getPhone()
  62. {
  63. return this.phone;
  64. }
  65. public void setBirthday(Date birthday)
  66. {
  67. this.birthday=birthday;
  68. }
  69. public Date getBirthday()
  70. {
  71. return this.birthday;
  72. }
  73. }

5、在service包里创建StudentService接口

  1. public interface StudentService {
  2. public List<Student> findAll();
  3. public Student findById(Integer id);
  4. }

6、在service的子包impl里创建StudentService接口的实现类StudentServiceImpl

  1. @Service
  2. public class StudentServiceImpl implements StudentService {
  3. @Autowired
  4. private StudentMapper studentMapper;
  5. @Override
  6. public List<Student> findAll() {
  7. return studentMapper.findAll();
  8. }
  9. @Override
  10. public Student findById(Integer id) {
  11. return studentMapper.findById(id);
  12. }
  13. }

7、在controller包里创建类StudentController,与数据库交互将数据传到web

  1. package com.zw.springboot.springbootmybatis.controller;
  2. import com.zw.springboot.springbootmybatis.entity.Student;
  3. import com.zw.springboot.springbootmybatis.service.StudentService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. /**
  10. * ClassName: StudentController
  11. * Package: com.zw.springboot.springbootmybatis.controller
  12. * Description:
  13. *
  14. * @Author 唱片
  15. * @Create 2024/5/4 下午10:44
  16. * @Version 1.0
  17. */
  18. @RestController
  19. @ResponseBody
  20. public class StudentController {
  21. @Autowired
  22. private StudentService studentService;
  23. @RequestMapping("/findById")
  24. public Student findById(Integer id) {
  25. return studentService.findById(id);
  26. }
  27. @RequestMapping("/findAll")
  28. public List<Student> findAll() {
  29. return studentService.findAll();
  30. }
  31. @RequestMapping("/testStudent")
  32. public Student testStudent() {
  33. Student student = new Student();
  34. student.setId(1001);
  35. student.setName("dddd");
  36. student.setSex(true);
  37. student.setPhone("123456789");
  38. return student;
  39. }
  40. @RequestMapping("/hello")
  41. public String hello() {
  42. return "hello";
  43. }
  44. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/644641
推荐阅读
相关标签
  

闽ICP备14008679号