Springboot整合Mybatis框架

Springboot学习有段时间,由于最近忙着找实习,学习网之后很久么有实操过,有点忘了今天复盘一下,记录此笔记。

1、新建springboot项目,这里使用阿里云镜像,官方的网络太卡了
Springboot整合Mybatis框架_第1张图片
2、选择好版本,填入项目名字
Springboot整合Mybatis框架_第2张图片
3、选择这三个依赖包
Springboot整合Mybatis框架_第3张图片
4、yml文件配置数据源
Springboot整合Mybatis框架_第4张图片
5、在com.example包下创建pojo包,并编写pojo实体类User

package com.example.pojo;

public class User {
    private int id;
    private String username;
    private String city;

    public User(int id, String username, String city) {
        this.id = id;
        this.username = username;
        this.city = city;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

6、在example包下创建UserMapper接口

package com.example.mapper;

import com.example.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    public List<User> getlist();
}

7、接下来写的的xml配置文件实现,也可以用纯注解,不过我个人喜欢前者。
在resources包建立mapper文件,专门存放xml映射文件的。


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <select id="getlist" resultType="user">
        select * from users;
    select>
mapper>

8、然后在配置文件中写入mybatis的配置文件
第一行的作用是让spring找到配置文件
第二行的作用是让为了别名实体类
在这里插入图片描述
9、最后在test类中编写测试代码输出。
Springboot整合Mybatis框架_第5张图片
10、打印输出
Springboot整合Mybatis框架_第6张图片
整合完毕!;

你可能感兴趣的:(Springboot,spring,boot,java,intellij-idea)