Mybatis introduction and the environment set up

I. INTRODUCTION

mybatis is a persistence framework, is written in java. It encapsulates many of the details jdbc, so that developers can focus sql statement itself, without annotation-driven focus, create complicated process connections. It uses the idea of ​​ORM package achieved the result set.

Second, the environment is set up

  • Create Project

  • Preparing the database

  • Introduced in the maven dependency mybatis

       mybatis official website: https://mybatis.org/mybatis-3/zh/getting-started.html

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • Create a user entity class
import java.io.Serializable;
import java.util.Date;

/**
 * @author jcH
 * @create 2020-01-27 16:23
 */
public class user implements Serializable {
    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 + '\'' +
                '}';
    }
}
  •  Creating mybatis master configuration file in the resource directory

  

<?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">
<!--mybatis主配置文件-->
<configuration>
    <!--配置环境-->
    <environments default="mysql">
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--配置数据库四个基本信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <mappers>
        <mapper resource="com/dgut/dao/IUserDao.xml"/>
    </mappers>
</configuration>

 

  • Create a separate profile dao

<?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">
<!--命名空间(IUserDao的路径)-->
<mapper namespace="com.dgut.dao.IUserDao">
    <!--id不能乱写,要写接口的方法名称-->
    <select id="findAll">
        select * from user
    </select>
</mapper>

 

So far, environmental set up is completed

Third, pay attention

  1. Create a directory and create a package idea is not the same. When you create a package, com.dgut.dao a tertiary structure; and when you create a directory, com.dgut.dao is the primary structure. So when you create IUserDao.xml, the directory you want to create a level to ensure that when the tertiary structure.
  2. Mapping configuration file must be consistent with the location mybatis packet structure dao interface.
  3. The value mapper mapping configuration file of the namespace attribute tag is not allowed to fully qualified class name dao interface.
  4. Operation mapping configuration file (e.g., select) the id attribute value must be the name of the method dao interface.

After following the 2,3,4 point, we do not need to write dao implementation class in the works.

  

Published 52 original articles · won praise 36 · views 8038

Guess you like

Origin blog.csdn.net/weixin_40391011/article/details/104093203