Examples of building Mybatis framework, take a look at the official website

1. First open the official website

2. Build a father and son project 

3. POM file configuration dependency package

4. Create a core configuration file

5. Obtain SqlSession and extract it as MybatisUtils tool class

6. Write the pojo class, our bean class

7. Write the corresponding dao layer

8. Write the corresponding dao layer xml mapping file

9. Write unit tests

10. Perform unit tests to view the results


 

1. First open the official website

https://mybatis.org/mybatis-3/zh/index.html

 All the building steps are explained on the official website, just follow the official website below

2. Build a father and son project 

Build a parent-child project

3. POM file configuration dependency package

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
    </dependencies>

 

4. Create a core configuration file

<?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="test">
        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/study?userSSL=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"/>
                <property name="username" value="root"/>
                <property name="password" value="password"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/lingaolu/dao/DeptMapper.xml"/>
    </mappers>
</configuration>

 

5. Obtain SqlSession and extract it as MybatisUtils tool class

 

 

So the correct tool class is as follows: 

package com.lingaolu.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 林高禄
 * @create 2020-10-12-9:33
 */
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static{
        String resource = "mybatis-config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

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

 

6. Write the pojo class, our bean class

Idea connects to the database  Idea connects to the database and executes SQL statements

package com.lingaolu.pojo;

/**
 * @author 林高禄
 * @create 2020-10-12-9:41
 */
public class Dept {
    private Long id;
    private String dname;
    private String loc;

    public Long getId() {
        return id;
    }

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

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "id=" + id +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}

 

7. Write the corresponding dao layer

package com.lingaolu.dao;

import com.lingaolu.pojo.Dept;

import java.util.List;

/**
 * @author 林高禄
 * @create 2020-10-12-9:46
 */
public interface DeptDao {
    List<Dept> getDeptList();
}

 

8. Write the corresponding dao layer xml mapping file

The original practice when mybatis is not used is to implement the dao layer, and then obtain various connections, write SQL, and now use mybatis to convert the previous implementation class to an xml mapping file

<?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.lingaolu.dao.DeptDao">
    <select id="getDeptList" resultType="com.lingaolu.pojo.Dept">
      select * from study.dept
  </select>
</mapper>

 

Because our DeptMapper.xml file is currently placed in the java folder, there will be a big problem that some resources cannot be found. The specific problem is that the configuration file in the Maven item src/main/java directory cannot be exported or become effective. And treatment plan

So we have to add the following configuration to the pom file

So the total configuration of the parent pom.xml in this example is

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lingaolu</groupId>
    <artifactId>MybatisStudy</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>MybatisStudy-service</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

 

9. Write unit tests

 

package com.dao;

import com.lingaolu.dao.DeptDao;
import com.lingaolu.pojo.Dept;
import com.lingaolu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * @author 林高禄
 * @create 2020-10-12-9:56
 */
public class DeptDaoTest {

    @Test
    public void getDeptList(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        DeptDao mapper = sqlSession.getMapper(DeptDao.class);
        List<Dept> deptList = mapper.getDeptList();
        deptList.forEach(System.out::println);
        sqlSession.close();
    }
}

 

10. Perform unit tests to view the results

 

Guess you like

Origin blog.csdn.net/lgl782519197/article/details/109044268