diff --git a/springboot-mybatis-quickstart/.gitignore b/springboot-mybatis-quickstart/.gitignore new file mode 100644 index 0000000..549e00a --- /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/pom.xml b/springboot-mybatis-quickstart/pom.xml new file mode 100644 index 0000000..80c7a1f --- /dev/null +++ b/springboot-mybatis-quickstart/pom.xml @@ -0,0 +1,93 @@ + + + 4.0.0 + com.inmind + springboot-mybatis-quickstart + 0.0.1-SNAPSHOT + springboot-mybatis-quickstart + springboot-mybatis-quickstart + + 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.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.SpringbootMybatisQuickstartApplication + true + + + + repackage + + repackage + + + + + + + + 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..3357f10 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/mapper/UserMapper.java @@ -0,0 +1,19 @@ +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; + +//让mybatis进行识别是Mapper接口 +//注意:在运行时,@Mapper会自动生成该接口UserMapper的实现类对象(代理对象),并且将该代理对象交给spring的IOC容器管理 +@Mapper +public interface UserMapper { + + /* + 查询所有用户数据 + */ + @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 new file mode 100644 index 0000000..01fd5e1 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/java/com/inmind/pojo/User.java @@ -0,0 +1,71 @@ +package com.inmind.pojo; + +public class User { + private Integer id; + private String name; + private Integer age; + 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/main/resources/application.properties b/springboot-mybatis-quickstart/src/main/resources/application.properties new file mode 100644 index 0000000..14d7486 --- /dev/null +++ b/springboot-mybatis-quickstart/src/main/resources/application.properties @@ -0,0 +1,15 @@ +#下面这些内容是为了让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-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..41df903 --- /dev/null +++ b/springboot-mybatis-quickstart/src/test/java/com/inmind/SpringbootMybatisQuickstartApplicationTests.java @@ -0,0 +1,26 @@ +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 +class SpringbootMybatisQuickstartApplicationTests { + + @Autowired//直接从IOC容器中获取UserMapper类型的对象 + private UserMapper userMapper; + + @Test + void contextLoads() { + //使用mybatis框架查询所有用户的数据 + List list = userMapper.list(); + list.stream().forEach(user -> { + System.out.println(user); + }); + } + +}