diff --git a/springboot-mybatis-quickstart/.gitignore b/springboot-mybatis-quickstart/.gitignore new file mode 100644 index 0000000..667aaef --- /dev/null +++ b/springboot-mybatis-quickstart/.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-quickstart/.mvn/wrapper/maven-wrapper.properties b/springboot-mybatis-quickstart/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..bb58b3c --- /dev/null +++ b/springboot-mybatis-quickstart/.mvn/wrapper/maven-wrapper.properties @@ -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 diff --git a/springboot-mybatis-quickstart/pom.xml b/springboot-mybatis-quickstart/pom.xml new file mode 100644 index 0000000..ea5ec2a --- /dev/null +++ b/springboot-mybatis-quickstart/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.16 + + + + com.inmind + springboot-mybatis-quickstart + 0.0.1-SNAPSHOT + springboot-mybatis-quickstart + springboot-mybatis-quickstart + + + + + + + + + + + + + + + + 17 + + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 3.0.5 + + + + + com.mysql + mysql-connector-j + runtime + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter-test + 3.0.5 + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springboot-mybatis-quickstart/src/main/java/com/inmind/SpringbootMybatisQuickstartApplication.java b/springboot-mybatis-quickstart/src/main/java/com/inmind/SpringbootMybatisQuickstartApplication.java new file mode 100644 index 0000000..9e93577 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/SpringbootMybatisQuickstartApplication.java @@ -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); + } + +} 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 new file mode 100644 index 0000000..81d7762 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/mapper/UserMapper.java @@ -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 getUsers(); +} 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 new file mode 100644 index 0000000..1cc19b3 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/pojo/User.java @@ -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 + '\'' + + '}'; + } +} diff --git a/springboot-mybatis-quickstart/src/main/resources/application.properties b/springboot-mybatis-quickstart/src/main/resources/application.properties new file mode 100644 index 0000000..ffe79a9 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/resources/application.properties @@ -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 + diff --git a/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java b/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java new file mode 100644 index 0000000..610579b --- /dev/null +++ b/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java @@ -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 users = userMapper.getUsers(); + users.stream().forEach(user-> System.out.println(user)); + } + +}