MyBatis第一个入门程序

本文阅读 2 分钟
首页 代码,Java 正文

img

🍁博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:MyBatis
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
🔥欢迎大佬指正,一起 学习!一起加油!
在这里插入图片描述

img 学习文档: MyBatis中文网

  • JDK1.8
  • Mysql5.7
  • maven 3.6.3
  • IDEA

步骤: 搭建环境–>导入Mybatis–>编写代码–>测试。

🔥搭建环境

  • 搭建数据库
  • 新建项目
  • 新建一个普通maven项目
  • 导入maven依赖

🔥搭建数据库

1.创建一个数据库名为mybatis的tb_user表 img 2.向表中添加数据 img

🔥导入maven依赖

pom:

  • maven依赖代码置于 pom.xml 文件中:
<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>5.1.47</version>
        </dependency>

-单元测试依赖

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

🔥编写mybatis-config.xml的核心配置文件

mybatis-config.xml:

<?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 core file-->
<configuration>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载sql映射文件-->
    <mappers>
        <mapper resource="com.jkj.UserMapper.xml"/>
    </mappers>
</configuration>

🔥 编写Mapper.xml的配置文件

userMapper.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">
<mapper namespace="test">
    <select id="selectAll" resultType="com.jkj.pojo.User">
    select * from tb_user;
  </select>
</mapper>

🔥 编写User实体类代码:

package com.jkj.pojo;

public class User { 
    private Integer id;
    private String username;
    private String password;
    private String gender;
    private String addr;

    public User() { 
    }

    public User(Integer id, String username, String password, String gender, String addr) { 
        this.id = id;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.addr = addr;
    }

    public Integer getId() { 
        return id;
    }

    public void setId(Integer 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 + '\'' +
                '}';
    }
}

🔥编写mybatisTest测试类

MyBatisUtils:

import com.jkj.pojo.User;
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;
import java.util.List;

public class MybatisTest { 
    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
        List<User> users = sqlSession.selectList("test.selectAll");
        System.out.println(users);
        //4.释放资源
        sqlSession.close();
    }
}

🔥运行截图:

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125279278
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复