mybatis--快速入门
This commit is contained in:
@@ -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,16 @@
|
||||
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接口
|
||||
*/
|
||||
@Mapper//mybatis在运行时,会自动生成该接口的实现类(代理对象),并且将该对象交给Spring容器管理(Bean对象)
|
||||
public interface UserMapper {
|
||||
@Select("select * from user")
|
||||
public List<User> list();
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.inmind.pojo;
|
||||
//对应User表的实体类
|
||||
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;
|
||||
}
|
||||
|
||||
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,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/mybatis2
|
||||
#连接数据库的用户名
|
||||
spring.datasource.username=root
|
||||
#连接数据库的密码
|
||||
spring.datasource.password=1234
|
||||
@@ -0,0 +1,31 @@
|
||||
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//在进行单元测试时,启动spring程序,启动IOC,将程序中Bean对象加载到容器
|
||||
class SpringbootMybatisQuickstartApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;//注入的是mybatis自动生成的UserMapper接口类型的实现类对象
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testList(){
|
||||
List<User> userList = userMapper.list();
|
||||
//通过集合获取Stream流,进行遍历消费,user就是集合中的每个元素,拿到每个元素进行打印消费
|
||||
// userList.stream().forEach(user -> System.out.println(user));
|
||||
for (User user : userList) {
|
||||
System.out.println(user);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user