mybatis Quick Start Case

Function: all single-table data query
detailed steps:

  1. Create a maven project
  2. Import jar package
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
  1. Dao create an interface in main / java path:
    Here Insert Picture Description
  2. Creating user tables in the database, and create the opposite Javabean class in maven engineering, heavy getter, setter, tostring method
package com.ZepngLin.domain;

import java.util.Date;

public class User {
    private Integer id;
    private  String username;
    private Date birthday;
    private String sex;
    private String address;

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

  1. Create a directory of resources and UserDao.java in the directory structure the same xml configuration file
    Here Insert Picture Description
<?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.ZepngLin.dao.UserDao">
    <!--配置查询所有-->
    <select id="findAll" resultType="com.ZepngLin.domain.User">
        SELECT * FROM user
    </select>
</mapper>

  1. Creating mybatis profile in resourceHere Insert Picture Description
<?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="mysql">
        <!--配置MySQL的环境-->
        <environment id="mysql">
            <!--配置事物的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--配置连接数据库的4个基本信息-->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://IP地址:3306/eesy_mybatis"></property>
                <property name="username" value="root"></property>
                <property name="password" value="root"></property>
            </dataSource>
        </environment>
    </environments>

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <mappers>
        <mapper resource="com/ZepngLin/dao/UserDao.xml"/>
    </mappers>
</configuration>
  1. Run the test code
package com.ZepngLin;

import com.ZepngLin.dao.UserDao;
import com.ZepngLin.domain.User;
import com.mysql.cj.Session;
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 Test {
    public static void main(String[] args) throws IOException {
        //1、读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2、创建SQLSessionFactory
        SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3、使用工厂生产sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //4、使用sqlSession创建接口的代理对象
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //5、使用代理对象执行方法
        List<User> users = userDao.findAll();
        for (User user:users) {
            System.out.println(user);
        }
        //6、释放资源
        sqlSession.close();


    }
}

Published 42 original articles · won praise 16 · views 3382

Guess you like

Origin blog.csdn.net/qq_41542638/article/details/105142322