Mybatis学习笔记-第一个Mybatis程序

思路

搭建环境

  • 搭建数据库(略)
CREATE DDATABASE
CREATE TABLE
INSERT VALUES
  • 新建项目
    • 普通Maven项目
    • 删除src文件夹 --> 建立父工程

导入Maven依赖

<!--导入依赖-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <!--数据库连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <!--Junit测试单元-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

编写代码

创建模块

  • 编写Mybatis的核心配置文件
<!--核心配置文件-->
<configuration>
    <!--配置环境-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
</configuration>
  • 编写Mybatis的工具类
package cn.iris.utils;

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;

// SqlSessionFactory --> SqlSession
public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            // 使用Mybatis获取SqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 有了 SqlSessionFactory,我们可以从中获得 SqlSession 的实例
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
  • 实体类
package cn.iris.pojo;

public class User {

    private int id;
    private String name;
    private String pwd;

    public User(){}

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
  • Dao接口
public interface UserDao {
    List<User> getUserList();
}
  • 接口实现类
<?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">

<!--绑定对应的Dao/Mapper接口-->
<mapper namespace="cn.iris.dao.UserDao">

    <!--select查询语句-->
    <select id="getUserList" resultType="cn.iris.pojo.User">
        SELECT * FROM mybatis.user
    </select>
</mapper>

测试

注意点

每一个mapper.xml都需要在mybatis-config.xml中注册
mapper注册路径用‘/’代替‘.’(mybatis-config.xml)
<mappers>
        <mapper resource="cn/iris/dao/userMapper.xml"/>
    </mappers>
资源过滤问题(pom.xml)
<!--资源过滤-->
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>
posted @ 2021-08-03 16:08  菜鸢爱敲bug  阅读(52)  评论(0编辑  收藏  举报