MyBatis入门 (一) 第一个MyBatis程序

MyBatis入门(一)

第一个MyBatis程序

思路:搭建环境——导入MyBatis——编写代码——测试

搭建环境

搭建数据库

CREATE DATABASE`mybatis`;

USE `mybatis`;

CREATE TABLE `user`(
   `id` INT(20) NOT NULL PRIMARY KEY,
	 `name` VARCHAR(30) DEFAULT NULL,
	 `pwd` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO `user` (`id`,`name`,`pwd`) VALUES
(1,'张三','123456'),
(2,'张三','123456'),
(3,'张三','123456')

导入MyBatis

  1. 新建一个maven项目
  2. 删除src目录,
  3. 导入maven依赖
<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <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>
    </dependencies>
  • 编写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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

</configuration>
  • 编写mybatis工具类
package com.godwin.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;

/**
 * Created by Godwin on 2021/1/3.
 */
public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try{
            //使用MyBatis第一步:获取sqlsession对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

编写代码

  • 实体类
package com.godwin.pojo;

/**
 * Created by Godwin on 2021/1/3.
 */
//实体类
public class User {
    private int id;
    private String name;
    private String pws;

    public User() {
    }

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

    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 getPws() {
        return pws;
    }

    public void setPws(String pws) {
        this.pws = pws;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pws='" + pws + '\'' +
                '}';
    }
}

  • mapper接口
package com.godwin.mapper;

import com.godwin.pojo.User;

import java.util.List;

/**
 * Created by admin on 2021/1/3.
 */
//UserDao接口
public interface UserMapper {
    List<User> getUserList();
}

  • 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.godwin.mapper.UserMapper">
    <select id="getUserList" resultType="com.godwin.pojo.User">
        SELECT * FROM mybatis.user;
    </select>
</mapper>

测试

  • junit测试
​```
package com.godwin.mapper;

import com.godwin.pojo.User;
import com.godwin.utils.MyBatisUtils;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * Created by admin on 2021/1/3.
 */
public class UserMapperTest {

    @Test
    public void test(){
        //第一步,获得SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //方式一getMapper:执行SQL
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();

        for (User user : userList) {
            System.out.println(user);
        }

        //关闭SqlSession
        sqlSession.close();
    }
}

复习笔记资料来自B站UP主:狂神说

posted @ 2021-01-03 23:52  Godwin_Zhang  阅读(67)  评论(0编辑  收藏  举报