From 4040b06ab80e905847294f502f12a51553d16830 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 25 Sep 2025 14:34:50 +0800 Subject: [PATCH] =?UTF-8?q?1.lombok=E7=9A=84=E4=BD=BF=E7=94=A8=202.Spring-?= =?UTF-8?q?mybatis-=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E5=87=86=E5=A4=87?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-mybatis-crud/.gitignore | 33 ++++++++ springboot-mybatis-crud/pom.xml | 83 +++++++++++++++++++ .../SpringbootMybatisCrudApplication.java | 13 +++ .../java/com/inmind/mapper/EmpMapper.java | 8 ++ .../src/main/java/com/inmind/pojo/Emp.java | 26 ++++++ .../src/main/resources/application.properties | 14 ++++ ...SpringbootMybatisCrudApplicationTests.java | 13 +++ springboot-mybatis-quickstart/pom.xml | 12 +++ .../java/com/inmind/mapper/UserMapper.java | 3 +- .../src/main/java/com/inmind/pojo/User.java | 70 ++-------------- ...bootMybatisQuickstartApplicationTests.java | 44 +++++++++- 11 files changed, 255 insertions(+), 64 deletions(-) create mode 100644 springboot-mybatis-crud/.gitignore create mode 100644 springboot-mybatis-crud/pom.xml create mode 100644 springboot-mybatis-crud/src/main/java/com/inmind/SpringbootMybatisCrudApplication.java create mode 100644 springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java create mode 100644 springboot-mybatis-crud/src/main/java/com/inmind/pojo/Emp.java create mode 100644 springboot-mybatis-crud/src/main/resources/application.properties create mode 100644 springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java diff --git a/springboot-mybatis-crud/.gitignore b/springboot-mybatis-crud/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/springboot-mybatis-crud/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/springboot-mybatis-crud/pom.xml b/springboot-mybatis-crud/pom.xml new file mode 100644 index 0000000..2a0c3b8 --- /dev/null +++ b/springboot-mybatis-crud/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + com.inmind + springboot-mybatis-crud + 0.0.1-SNAPSHOT + springboot-mybatis-crud + springboot-mybatis-crud + + 11 + UTF-8 + UTF-8 + 2.7.6 + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.3.0 + + + + com.mysql + mysql-connector-j + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + 11 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + com.inmind.SpringbootMybatisCrudApplication + true + + + + repackage + + repackage + + + + + + + + diff --git a/springboot-mybatis-crud/src/main/java/com/inmind/SpringbootMybatisCrudApplication.java b/springboot-mybatis-crud/src/main/java/com/inmind/SpringbootMybatisCrudApplication.java new file mode 100644 index 0000000..65b715c --- /dev/null +++ b/springboot-mybatis-crud/src/main/java/com/inmind/SpringbootMybatisCrudApplication.java @@ -0,0 +1,13 @@ +package com.inmind; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringbootMybatisCrudApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootMybatisCrudApplication.class, args); + } + +} diff --git a/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java b/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java new file mode 100644 index 0000000..c5dcde4 --- /dev/null +++ b/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java @@ -0,0 +1,8 @@ +package com.inmind.mapper; + +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface EmpMapper { + +} diff --git a/springboot-mybatis-crud/src/main/java/com/inmind/pojo/Emp.java b/springboot-mybatis-crud/src/main/java/com/inmind/pojo/Emp.java new file mode 100644 index 0000000..84e9c0d --- /dev/null +++ b/springboot-mybatis-crud/src/main/java/com/inmind/pojo/Emp.java @@ -0,0 +1,26 @@ +package com.inmind.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Emp { + private Integer id;//ID + private String username;//用户名 + private String password;//密码 + private String name;//姓名 + private Short gender;//性别 1:男 2:女 + private String image; //头像URL + private String job; // 职位 1:班主任 2:讲师 3:学工主管 4:教研主管 5:咨询师 + private LocalDate entrydate; // 入职日期 + private Integer deptId; // 部门ID + private LocalDateTime createTime; + private LocalDateTime updateTime; + +} diff --git a/springboot-mybatis-crud/src/main/resources/application.properties b/springboot-mybatis-crud/src/main/resources/application.properties new file mode 100644 index 0000000..ced36a2 --- /dev/null +++ b/springboot-mybatis-crud/src/main/resources/application.properties @@ -0,0 +1,14 @@ +#下面这些内容是为了让MyBatis映射 +#指定Mybatis的Mapper文件 +mybatis.mapper-locations=classpath:mappers/*xml +#指定Mybatis的实体目录 +mybatis.type-aliases-package=com.inmind.mybatis.entity + +#驱动类名称 +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +#数据库连接的url +spring.datasource.url=jdbc:mysql://localhost:3306/mybatis +#连接数据库的用户名 +spring.datasource.username=root +#连接数据库的密码 +spring.datasource.password=1234 diff --git a/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java b/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java new file mode 100644 index 0000000..f8cf9fe --- /dev/null +++ b/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java @@ -0,0 +1,13 @@ +package com.inmind; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringbootMybatisCrudApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/springboot-mybatis-quickstart/pom.xml b/springboot-mybatis-quickstart/pom.xml index 80c7a1f..7d89638 100644 --- a/springboot-mybatis-quickstart/pom.xml +++ b/springboot-mybatis-quickstart/pom.xml @@ -44,6 +44,18 @@ spring-boot-starter-test test + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + org.projectlombok + lombok + diff --git a/springboot-mybatis-quickstart/src/main/java/com/inmind/mapper/UserMapper.java b/springboot-mybatis-quickstart/src/main/java/com/inmind/mapper/UserMapper.java index 3357f10..22ef6c0 100644 --- a/springboot-mybatis-quickstart/src/main/java/com/inmind/mapper/UserMapper.java +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/mapper/UserMapper.java @@ -14,6 +14,7 @@ public interface UserMapper { /* 查询所有用户数据 */ - @Select("select * from user;") +// @Select("select * from user;") + @Select("select * from user") public List list(); } diff --git a/springboot-mybatis-quickstart/src/main/java/com/inmind/pojo/User.java b/springboot-mybatis-quickstart/src/main/java/com/inmind/pojo/User.java index 01fd5e1..e0f3a4a 100644 --- a/springboot-mybatis-quickstart/src/main/java/com/inmind/pojo/User.java +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/pojo/User.java @@ -1,5 +1,14 @@ package com.inmind.pojo; +import lombok.*; + +/*@Getter +@Setter +@ToString +@EqualsAndHashCode*/ +@Data +@NoArgsConstructor //生成无参构造 +@AllArgsConstructor//生成满参构造方法 public class User { private Integer id; private String name; @@ -7,65 +16,4 @@ public class User { private Short gender; private String phone; - public User() { - } - - public User(Integer id, String name, Integer age, Short gender, String phone) { - this.id = id; - this.name = name; - this.age = age; - this.gender = gender; - this.phone = phone; - } - - @Override - public String toString() { - return "User{" + - "id=" + id + - ", name='" + name + '\'' + - ", age=" + age + - ", gender=" + gender + - ", phone='" + phone + '\'' + - '}'; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Integer getAge() { - return age; - } - - public void setAge(Integer age) { - this.age = age; - } - - public Short getGender() { - return gender; - } - - public void setGender(Short gender) { - this.gender = gender; - } - - public String getPhone() { - return phone; - } - - public void setPhone(String phone) { - this.phone = phone; - } } diff --git a/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java b/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java index 41df903..3ae4c0f 100644 --- a/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java +++ b/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java @@ -6,21 +6,61 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.sql.*; +import java.util.ArrayList; import java.util.List; -@SpringBootTest +@SpringBootTest //该注解的作用,启动spring环境,所以也就有了IOC容器 class SpringbootMybatisQuickstartApplicationTests { @Autowired//直接从IOC容器中获取UserMapper类型的对象 - private UserMapper userMapper; + private UserMapper userMapper;//接口开发,多态 @Test void contextLoads() { //使用mybatis框架查询所有用户的数据 List list = userMapper.list(); list.stream().forEach(user -> { + System.out.println(user.getId()); System.out.println(user); }); } + @Test + void testJDBC() throws ClassNotFoundException, SQLException { + //1. 注册驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + + //2. 获取连接对象 + String url = "jdbc:mysql://localhost:3306/mybatis"; + String username = "root"; + String password = "1234"; + Connection connection = DriverManager.getConnection(url, username, password); + + //3. 获取执行SQL的对象Statement,执行SQL,返回结果 + String sql = "select * from user"; + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql); + + //4. 封装结果数据 + List userList = new ArrayList<>(); + while (resultSet.next()){ + int id = resultSet.getInt("id"); + String name = resultSet.getString("name"); + int age = resultSet.getInt("age"); + short gender = resultSet.getShort("gender"); + String phone = resultSet.getString("phone"); + + User user = new User(id,name,age,gender,phone); + userList.add(user); + } + + //5. 释放资源 + statement.close(); + connection.close(); + + + userList.forEach(System.out::println); + } + }