① MyBatis使用入门,解决IDEA中Mapper映射文件警告。-LMLPHP



  • MyBatis,一款优秀的持久层框架,用于简化 JDBC 的操作。
  • 持久层:负责将数据保存到数据库的代码。

  • JDBC 缺点:
    • 硬编码
    • 操作繁琐

硬编码直接在代码中 注册驱动、获取连接;设置SQL语句;

操作繁琐需要 手动设置参数;手动封装结果集;


⚪MyBatis使用


1. 准备操作的数据库表

create database mybatis;
use mybatis;

drop table if exists tb_user;

create table tb_user(
id int primary key auto_increment,
username varchar(20),
password varchar(20),
gender char(1),
addr varchar(30)  
);

insert into tb_user values(1, '张三', '123', '男', '北京');
insert into tb_user values(2, '李四', '456', '男', '上海');
insert into tb_user values(3, '王麻子', '789', '女', '广州');

2. 创建工程,导入mybatis坐标

    <dependencies>
        <!-- mybatis坐标 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <!-- 连接MySQL的坐标 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>


    </dependencies>

3. MyBatis核心配置文件

<?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>
    <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?useSSL=false&useUnicode=true&CharacterEncoding=utf-8&serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="abc123"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--配置映射文件,通常都是Mapper结尾-->
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

4. 依据表结构,构建实体类

package com.java.pojo;

/**
 * @author .29.
 * @create 2024-03-12 11:47
 */
public class User {
    private int id;
    private String username;
    private String password;
    private String gender;
    private String addr;

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

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


5. 编写SQL映射文件(Mapper.xml)

<?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">

<!--配置命名空间,id,返回值类型-->
<mapper namespace="test">
    <select id="selectAll" resultType="com.java.pojo.User">
        <!--设置SQL语句-->
        select * from tb_user;
    </select>
</mapper>

6. 获取sqlSession对象,操作数据。

/**
 * @author .29.
 * @create 2024-03-12 12:02
 */
public class mybatisDemo {
    public static void main(String[] args) throws IOException {
        //1. 加载mybatis配置文件,获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象去执行SQL
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3. 执行SQL语句
        //传入Mapper映射文件中SQL语句的 "命名空间+id"
        List<User> users = sqlSession.selectList("test.selectAll");

        System.out.println(users);

        //释放资源
        sqlSession.close();
    }
}

测试输出结果




⚪解决IDEA中映射文件警告

  • 原因:IDEA与数据库未建立连接,不识别表信息
  • 解决方式:配置IDEA与MySQL数据库的连接

🚀步骤

① MyBatis使用入门,解决IDEA中Mapper映射文件警告。-LMLPHP


👇👇


① MyBatis使用入门,解决IDEA中Mapper映射文件警告。-LMLPHP

点击OK即可完成连接!




① MyBatis使用入门,解决IDEA中Mapper映射文件警告。-LMLPHP

03-13 02:54