01-基于SpringBoot创建第一个mybatis入门程序

目录结构:

01-基于SpringBoot创建第一个mybatis入门程序_第1张图片

1. 创建springboot工程(参考springboot专栏的01创建入门程序)

引入依赖:

pom.xml



        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-test
        
    

在mysql中创建数据库与数据表User

create table user(
    id int unsigned primary key auto_increment comment 'ID',
    name varchar(100) comment '姓名',
    age tinyint unsigned comment '年龄',
    gender tinyint unsigned comment '性别, 1:男, 2:女',
    phone varchar(11) comment '手机号'
) comment '用户表';

insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');

创建实体类User

package demo01.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;
    }

    /**
     * 获取
     * @return id
     */
    public Integer getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public short getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(short age) {
        this.age = age;
    }

    /**
     * 获取
     * @return gender
     */
    public short getGender() {
        return gender;
    }

    /**
     * 设置
     * @param gender
     */
    public void setGender(short gender) {
        this.gender = gender;
    }

    /**
     * 获取
     * @return phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * 设置
     * @param phone
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String toString() {
        return "User{id = " + id + ", name = " + name + ", age = " + age + ", gender = " + gender + ", phone = " + phone + "}";
    }
}

2. 配置Mybatis(数据库连接信息)

创建application.properties文件放到resource目录下

01-基于SpringBoot创建第一个mybatis入门程序_第2张图片

 application.properties

# 配置数据库的链接信息 -四要素
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdemo01?serverTimezone=UTC
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=

3. 创建接口编写SQL语句

UserMapper

package demo01.mapper;

import demo01.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper //表示当前是Mybatis中的一个接口,在运行时,会自动生成该接口的实现类对象,并且将该对象交给IOC容器管理
public interface UserMapper {

//    查询全部用户信息
    @Select("select * from user")
    public List list();
}

4. 创建启动类

mybatisdemo01quickstartapplication

package demo01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//启动类 -- 启动springboot工程
@SpringBootApplication//具有包扫描作用,默认扫描当前包及其子包,即demo01
public class mybatisdemo01quickstartapplication {
    public static void main(String[] args) {
        SpringApplication.run(mybatisdemo01quickstartapplication.class,args);
    }
}

5. 创建整合测试类

 mybatisdemo01quickstartapplicationTest

package demo01;

import demo01.mapper.UserMapper;
import demo01.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整合单元测试的注解
public class mybatisdemo01quickstartapplicationTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testListUser(){
        List userList = userMapper.list();
        userList.stream().forEach(user -> {
            System.out.println(user);
        });

    }
}

6. 运行testListUser()方法

01-基于SpringBoot创建第一个mybatis入门程序_第3张图片

完成! 

你可能感兴趣的:(Mybatis,mybatis,spring,boot,java)