IDEA2023 SpringBoot整合MyBatis(三)

打印 上一主题 下一主题

主题 1812|帖子 1812|积分 5438

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、数据库表

  1. CREATE TABLE students (
  2.     id INT AUTO_INCREMENT PRIMARY KEY,
  3.     name VARCHAR(100) NOT NULL,
  4.     age INT,
  5.     gender ENUM('Male', 'Female', 'Other'),
  6.     email VARCHAR(100) UNIQUE,
  7.     phone_number VARCHAR(20),
  8.     address VARCHAR(255),
  9.     date_of_birth DATE,
  10.     enrollment_date DATE,
  11.     course VARCHAR(100)
  12. );
  13. -- 插入10条学生数据
  14. INSERT INTO students (name, age, gender, email, phone_number, address, date_of_birth, enrollment_date, course) VALUES
  15. ('John Doe', 20, 'Male', 'john.doe@example.com', '1234567890', '123 Main St, City', '2003-01-01', '2023-09-01', 'Computer Science'),
  16. ('Jane Smith', 21, 'Female', 'jane.smith@example.com', '0987654321', '456 Elm Rd, Town', '2002-02-02', '2023-09-01', 'Business Administration'),
  17. ('Michael Johnson', 19, 'Male', 'michael.johnson@example.com', '1122334455', '789 Oak Ave, Village', '2004-03-03', '2023-09-01', 'Electrical Engineering'),
  18. ('Emily Davis', 22, 'Female', 'emily.davis@example.com', '5544332211', '321 Pine Blvd, County', '2001-04-04', '2023-09-01', 'Mechanical Engineering'),
  19. ('William Brown', 20, 'Male', 'william.brown@example.com', '9988776655', '654 Cedar Ln, District', '2003-05-05', '2023-09-01', 'Civil Engineering'),
  20. ('Olivia Wilson', 21, 'Female', 'olivia.wilson@example.com', '4433221100', '987 Walnut St, Borough', '2002-06-06', '2023-09-01', 'Chemistry'),
  21. ('Benjamin Taylor', 19, 'Male', 'benjamin.taylor@example.com', '7766554433', '246 Maple Dr, Neighborhood', '2004-07-07', '2023-09-01', 'Physics'),
  22. ('Grace Anderson', 22, 'Female', 'grace.anderson@example.com', '3322110099', '852 Birch Rd, Hamlet', '2001-08-08', '2023-09-01', 'Mathematics'),
  23. ('Henry Thompson', 20, 'Male', 'henry.thompson@example.com', '6655443322', '587 Aspen St, Province', '2003-09-09', '2023-09-01', 'Biology'),
  24. ('Chloe Robinson', 21, 'Female', 'chloe.robinson@example.com', '9977665544', '101 Poplar Ave, Region', '2002-10-10', '2023-09-01', 'Psychology');
复制代码
二、在pom文件中添加MyBatis相干依赖包,Mysql驱动依赖

  1. <dependencies>
  2.         <dependency>
  3.             <groupId>org.springframework.boot</groupId>
  4.             <artifactId>spring-boot-starter-jdbc</artifactId>
  5.         </dependency>
  6.       
  7.         <dependency>
  8.             <groupId>org.springframework.boot</groupId>
  9.             <artifactId>spring-boot-starter-web</artifactId>
  10.         </dependency>
  11.          <!-- mybatis依赖 -->
  12.         <dependency>
  13.             <groupId>org.mybatis.spring.boot</groupId>
  14.             <artifactId>mybatis-spring-boot-starter</artifactId>
  15.             <version>3.0.3</version>
  16.         </dependency>
  17.          <!-- mysql依赖 -->
  18.         <dependency>
  19.             <groupId>com.mysql</groupId>
  20.             <artifactId>mysql-connector-j</artifactId>
  21.             <scope>runtime</scope>
  22.         </dependency>
  23.         <dependency>
  24.             <groupId>org.springframework.boot</groupId>
  25.             <artifactId>spring-boot-starter-test</artifactId>
  26.             <scope>test</scope>
  27.         </dependency>
  28.         <!-- mybatis测试依赖 -->
  29.         <dependency>
  30.             <groupId>org.mybatis.spring.boot</groupId>
  31.             <artifactId>mybatis-spring-boot-starter-test</artifactId>
  32.             <version>3.0.3</version>
  33.             <scope>test</scope>
  34.         </dependency>
复制代码
     当我们没有热部署的时候,我们必须在代码修改完后再重起程序,程序才会同步你修改的信息。那么我们可以手动启动热部署-》》》
  1.      <!-- 热部署工具 -->
  2.         <dependency>
  3.             <groupId>org.springframework.boot</groupId>
  4.             <artifactId>spring-boot-devtools</artifactId>
  5.             <optional>true</optional>
  6.         </dependency>
复制代码
 
三、编写实体类

  1. public class Student {
  2.     // 定义属性
  3.     private String name; // 姓名
  4.     private int age; // 年龄
  5.     private String gender; // 性别
  6.     private String email; // 邮箱
  7.     private String phoneNumber; // 电话号码
  8.     private String address; // 地址
  9.     private java.util.Date dateOfBirth; // 出生日期
  10.     private java.util.Date enrollmentDate; // 入学日期
  11.     private String course; // 课程
  12.    ...setXXX and getXXX and constructor and toString...
复制代码
四、 编写映射类StudentMapper

  1. */
  2. @Mapper
  3. public interface StudentMapper  {
  4.    // @Select("select * from student where id = #{id}")
  5.     public List<Student> findById(int id);
  6.    //  @Select("select * from student")
  7.     public List<Student> findAll();
  8. }
复制代码
  注意:假如不写@Select注解,则就必要写Mapper映射文件
  五、编写Mapper映射文件

  mybatis – MyBatis 3 | 配置
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.hlx.springbootdemo1.mapper.StudentMapper">
  4.     <select id="findById" parameterType="int" resultType="Student">
  5.         SELECT * FROM student WHERE id = #{id}
  6.     </select>
  7.     <select id="findAll" resultType="Student">
  8.       select * from student
  9.     </select>
  10. </mapper>
复制代码
六、编写配置文件

  1. #mysql数据源配置
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
  4. spring.datasource.username=root
  5. spring.datasource.password=123456
  6. #mybatis
  7. mybatis.mapper-locations=classpath*:mapper/*.xml
  8. #实体类的别名
  9. mybatis.type-aliases-package=com.hlx.springbootdemo1.entity
  10. #日志
  11. logging.pattern.console='%d{HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'
复制代码
 七、编写业务类

  1. @Service
  2. public class StudentService {
  3.     @Autowired
  4.     private StudentMapper studentMapper;
  5.     public List<Student> findAll(){
  6.         return (studentMapper.findAll());
  7.     }
  8.     public Student findById(int id){
  9.         return (studentMapper.findById(id));
  10.     }
  11. }
复制代码
 八、编写控制器类

  1. @RestController
  2. public class StudentsController {
  3.     @Autowired
  4.     private StudentService studentService;
  5.     @GetMapping("/user/{id}")
  6.     public Student findById(@PathVariable int id) {
  7.         return studentService.findById(id);
  8.     }
  9.     @GetMapping("/user/all")
  10.     public List<Student> findAll() {
  11.         return studentService.findAll();
  12.     }
  13. }
复制代码
九、启动类

  1. @SpringBootApplication
  2. @MapperScan("com.hlx.springbootdemo1.mapper")
  3. public class SpringbootDemo1Application {
  4.     public static void main(String[] args) {
  5.         SpringApplication.run(SpringbootDemo1Application.class, args);
  6.     }
  7. }
复制代码
十、项目布局


十一、启动运行



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表