SpringBoot-整合Mybatis

  1. 加入依赖
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
  1. 配置数据源信息
#集成mysql数据库的配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/community?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
  1. 编写数据库的实体类com.july.community.model.User
    属性与数据库的字段对齐
public class User{
    private Integer id;
    private String name;
    private String accountId;
    private String token;
    private Long timeCreate;
    private Long timeModified;
    
 public User( String name, String accountId, String token, Long timeCreate, Long timeModified) {
        this.name = name;
        this.accountId = accountId;
        this.token = token;
        this.timeCreate = timeCreate;
        this.timeModified = timeModified;
    }
    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 String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getTimeCreate() {
        return timeCreate;
    }

    public void setTimeCreate(Long timeCreate) {
        this.timeCreate = timeCreate;
    }

    public Long getTimeModified() {
        return timeModified;
    }

    public void setTimeModified(Long timeModified) {
        this.timeModified = timeModified;
    }
}

  1. 写Mapper
    com.july.community.mapper.
package com.july.community.mapper;

import com.july.community.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    @Insert("insert into tbl_user (name,account_id,token,time_create,time_modified) values (#{name},#{accountId},#{token},#{timeCreate},#{timeModified})")
    void insert(User user);
}
  1. 编写controller中的代码
 //存入数据库
            User user = new User(githubUser.getName(),String.valueOf(githubUser.getId()), UUID.randomUUID().toString(),System.currentTimeMillis(),System.currentTimeMillis());
            userMapper.insert(user);
  1. 运行测试
    查看数据库,成功!
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值