Simple introduction to Mybatis-config configuration xml, mapper configuration xml

Prerequisite preparation

  • Install MySQL database
  • Create a table in advance

Create a Student database:

// create database 数据库名;
create database  student;

Insert picture description here
Check whether the creation is successful:

show databases;

If the picture below appears, it means the creation is successful.
Insert picture description here
Then create a student table:

use student; 
CREATE TABLE IF NOT EXISTS student(
 	 id INT,
     name VARCHAR(25),
     age INT,
     );

Insert data:

insert into student (id, name, age) values (1, 'tom', 23);

Check whether the data is inserted successfully:

select * from student;

Insert picture description here
The preparations are done.

Use Maven to create a project

The introduced Mybatis and mysql-connector:

<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.46</version>
    </dependencies>

File Directory:
Insert picture description here

Mybatis's main configuration file SqlMapConfig.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>
    <!--配置环境-->
    <environments default="mysql">
        <!--配置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/student"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/radish/dao/IStudentDao.xml"/>
    </mappers>
</configuration>

Mapper configuration file of Mybatis: IStudentDao.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="com.radish.dao.IStudentDao" >
    <!--查询所有-->
    <select id="findAll" resultType="com.radish.domain.Student">
        select * from student;
    </select>
</mapper>

Insert picture description here
The IStudentDao code is as follows:

package com.radish.dao;

import com.radish.domain.Student;

import java.util.List;

public interface IStudentDao {
    
    
    /**
     * 查询所有
     * @return
     */
    List<Student> findAll();
}

Insert picture description here
Student's code is as follows:

package com.radish.domain;

import java.io.Serializable;

public class Student implements Serializable {
    
    
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

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

Insert picture description here
test program:

package com.radish;

import com.radish.dao.IStudentDao;
import com.radish.domain.Student;
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.InputStream;
import java.util.List;

public class Test {
    
    
    public static void main(String[] args) throws Exception {
    
    
        // 1.加载配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        // 3.使用工厂生成SqlSession对象
        SqlSession session = factory.openSession();
        // 4.使用session创建Dao的代理对象
        IStudentDao dao = session.getMapper(IStudentDao.class);
        // 5.执行findAll方法
        List<Student> students = dao.findAll();
        for(Student student: students) {
    
    
            System.out.println(student);
        }
        // 6.释放资源
        session.close();
        in.close();

    }
}

Results of the:
Insert picture description here

Precautions

  1. The operation interface name and mapping file of the persistence layer in Mybatis are also called: Mapper. So IUserDao and IUserMapper are the same
  2. When creating a directory in idea, it is different from a package. When the package is created: com.radish.dao is a three-level structure, and the directory is created as a first-level directory.
  3. The location of the mapping configuration file of mybatis must be the same as the package structure of the dao interface
  4. The value of the namespace attribute of the mapper tag of the mapping configuration file must be the fully qualified class name of the dao interface
  5. The operation configuration of the mapping configuration file, the value of id must be the method name of the dao interface

Guess you like

Origin blog.csdn.net/weixin_44736584/article/details/105960930