mybatis的简单教程

整体就是mysql里存了一张表,然后在java程序里用mybatis把数据读出来的一个简单示例。

库 blog里有一张表 article

mybatis的简单教程_第1张图片

整个项目就是增加了这3个文件

mybatis的简单教程_第2张图片

首先是mybatis-config.xml文件




    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

然后是ArticleMapper.XML




    

然后是和数据库存储模型对应的对象Article类

package org.example.po;

import java.util.Date;

public class Article {
    private Integer id;

    private String title;

    private String desc;

    private Integer cate;

    private Date createdAt;

    private Date updatedAt;

    private String content;

    public Integer getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc == null ? null : desc.trim();
    }

    public Integer getCate() {
        return cate;
    }

    public void setCate(Integer cate) {
        this.cate = cate;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }
}

然后就是运行类,main方法

package org.example;

import com.google.gson.Gson;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.po.Article;

import java.io.InputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        String resource="mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        Article article=sqlSession.selectOne("selectArticle",1);
        Gson gson=new Gson();
        System.out.println("Hello world!");

        System.out.println(gson.toJson(article));
    }
}

最后把pom.XML也贴一下



    4.0.0

    org.example
    mybatis-study
    1.0-SNAPSHOT

    
        14
        14
        UTF-8
    

    
        
            org.mybatis
            mybatis
            3.5.9
        
        
            mysql
            mysql-connector-java
            8.0.27
        

        
            com.google.code.gson
            gson
            2.10
        

        
            org.projectlombok
            lombok
            1.18.30
        
    

其他问题可以参这个官方教程

mybatis – MyBatis 3 | XML 映射器

你可能感兴趣的:(数据库,java)