javaEEday09-mybatis-入门操作

This commit is contained in:
xx
2026-10-18 11:16:57 +08:00
parent cd108ec973
commit 1afd72d60c
8 changed files with 241 additions and 0 deletions
+33
View File
@@ -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/
@@ -0,0 +1,3 @@
wrapperVersion=3.3.4
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.10.0/apache-maven-3.10.0-bin.zip
+73
View File
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--maven的坐标-->
<groupId>com.inmind</groupId>
<artifactId>springboot-mybatis-quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis-quickstart</name>
<description>springboot-mybatis-quickstart</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<!--配置通用版本号-->
<properties>
<java.version>17</java.version>
</properties>
<!--依赖管理-->
<dependencies>
<!--mybatis的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mysql的驱动依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!--springboot的测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis测试起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,13 @@
package com.inmind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMybatisQuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisQuickstartApplication.class, args);
}
}
@@ -0,0 +1,14 @@
package com.inmind.mapper;
import com.inmind.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将该对象交给IOC容器(spring容器)管理
public interface UserMapper {
//查询所有用户信息
@Select("select * from user")
public List<User> getUsers();
}
@@ -0,0 +1,75 @@
package com.inmind.pojo;
public class User {
private Integer id;
private String name;
private Short age;
private Short gender;
private String phone;
//构造方法
public User() {
}
public User(Integer id, String name, Short age, Short gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
//set/get
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 Short getAge() {
return age;
}
public void setAge(Short 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
}
@@ -0,0 +1,7 @@
spring.application.name=springboot-mybatis-quickstart
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=1234
@@ -0,0 +1,23 @@
package com.inmind;
import com.inmind.mapper.UserMapper;
import com.inmind.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest//springboot整合单元测试框架的注解 junit4
class SpringbootMybatisQuickstartApplicationTests {
@Autowired //spring中DI依赖反转,从IOC容器中获取UserMapper类型的对象
private UserMapper userMapper;//接口的多态
@Test//要测试执行的功能代码
void contextLoads() {
List<User> users = userMapper.getUsers();
users.stream().forEach(user-> System.out.println(user));
}
}