javaEEday09-mybatis-入门操作
This commit is contained in:
+13
@@ -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
|
||||
|
||||
+23
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user