美文网首页
Mybatis 快速入门

Mybatis 快速入门

作者: 小小爱笑 | 来源:发表于2018-08-13 11:31 被阅读0次

前言

介绍mybatis框架的基本使用方法。
更详细的源码学习 参考另外一篇文章: mybatis mybatis-spring 源码流程分析

官网


创建maven工程

pom.xml

设置java版本

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>

添加mybatis, mysql

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
</dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
</dependency>

创建db.properties (resource/)

username=gen
password=123
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://192.168.199.217:3306/awesome?useUnicode=true&characterEncoding=utf8&useSSL=true

创建 mybatis-config.xml (resource/)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="db.properties">
    </properties>
    
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/BlogMapper.xml"/>
    </mappers>
</configuration>

创建实体类 BlogEntity.java

package com.gen.mybatis.entity;

public class BlogEntity {

    private String id;
    private String userId;
    private String userName;
    private String name;
    private String summary;
    private String content;
    private String createAt;

    @Override
    public String toString() {
        return "BlogEntity{" +
                "id='" + id + '\'' +
                ", userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                ", name='" + name + '\'' +
                ", summary='" + summary + '\'' +
                ", content='" + content + '\'' +
                ", createAt='" + createAt + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

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

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreateAt() {
        return createAt;
    }

    public void setCreateAt(String createAt) {
        this.createAt = createAt;
    }
}

创建 BlogMapper.java 接口

package com.gen.mybatis.dao;

import com.gen.mybatis.entity.BlogEntity;

public interface BlogMapper {

    BlogEntity queryById(String id);
}

创建 BlogMapper.xml (resource/mapper/)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gen.mybatis.dao.BlogMapper">
    <select id="queryById" resultType="com.gen.mybatis.entity.BlogEntity">
    select * from blogs where id = #{id}
  </select>
</mapper>

使用BlogMapper.java,创建BlogDao.java

package com.gen.mybatis.dao;

import com.gen.mybatis.entity.BlogEntity;
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 java.io.IOException;
import java.io.InputStream;

public class BlogDao {

    private SqlSessionFactory sqlSessionFactory;

    public BlogDao() {
        String res = "mybatis-config.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(is);
    }

    public BlogEntity queryById(String id) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            BlogMapper mapper = session.getMapper(BlogMapper.class);
            return mapper.queryById(id);
        }
    }

}

测试 BlogDaoTest.java

package com.gen.mybatis.dao;

import com.gen.mybatis.entity.BlogEntity;

import java.util.logging.Logger;

public class BlogDaoTest {

    private static final Logger logger = Logger.getLogger("BlogDaoTest");

    @org.junit.Test
    public void queryById() {
        BlogDao dao = new BlogDao();

        BlogEntity blogEntity = dao.queryById("1");

        logger.info(blogEntity.toString());
    }
}

相关文章

  • MyBatis

    MyBatis学习总结(一)——MyBatis快速入门 超详细MyBatis入门讲解

  • MyBatis之快速入门

    title: MyBatis之快速入门tags: MyBatiscategories: MyBatis 若图片无法...

  • 1.Mybatis - 搭建

    参考 Mybatis 官方 MyBatis学习总结(一)——MyBatis快速入门 安装 说明基于Maven 步骤...

  • 17.Mybites

    一、快速入门官网MyBatis[https://mybatis.net.cn/getting-started.ht...

  • mybatis学习

    Mybatis:ORM框架 一、一些资料: 一个小实例供快速入门:MyBatis学习笔记(一)入门 - NAYNE...

  • Java框架--Mybaits(一)

    为什么要使用Mybatis? 快速入门 高级映射 动态SQL 1. 为什么要使用Mybatis? 通过简单配置就可...

  • (1)MyBatis 简单介绍

    mybatis 快速入门 1.加入mybatis 的依赖2.添加mybatis的配置文件3.场景介绍4.编写实体类...

  • Mybatis 快速入门

    前言 介绍mybatis框架的基本使用方法。更详细的源码学习 参考另外一篇文章: mybatis mybatis-...

  • Mybatis快速入门

    mybatis依赖jar包 创建数据库 Mybatis框架原理(掌握) 1、Mybatis 是什么? Mybati...

  • MyBatis快速入门

    一、概述 MyBatis是Apache的一个开源项目,是一个基于Java的持久层框架。它支持普通SQL查询、存储过...

网友评论

      本文标题:Mybatis 快速入门

      本文链接:https://www.haomeiwen.com/subject/gxiybftx.html