Mybatis框架搭建实例,手把手带看官网教

1、首先打开官网

2、搭建父子工程项目 

3、POM文件配置依赖包

4、创建核心配置文件

5、获取SqlSession,抽取为MybatisUtils工具类

6、编写pojo类,即我们的bean类

7、编写相应的dao层

8、编写相应的dao层xml映射文件

9、编写单元测试

10、执行单元测试查看结果


1、首先打开官网

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

 所有的搭建步骤官网都有说明,下面就跟着官网一起来

2、搭建父子工程项目 

搭建父子工程项目

3、POM文件配置依赖包

<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、创建核心配置文件

<?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、获取SqlSession,抽取为MybatisUtils工具类

 

 

所以正确的工具类如下: 

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、编写pojo类,即我们的bean类

idea连接数据库 Idea连接数据库并执行SQL语句

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、编写相应的dao层

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、编写相应的dao层xml映射文件

原来不用mybatis的时候的做法是,实现dao层,然后各种获取连接,编写sql,现在使用mybatis了,把之前的实现类转变为了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.lingaolu.dao.DeptDao">
    <select id="getDeptList" resultType="com.lingaolu.pojo.Dept">
      select * from study.dept
  </select>
</mapper>

因为目前我们的DeptMapper.xml文件是放在java文件夹下的,所以会出点资源找不到大的问题,具体问题为Maven项src/main/java目录下配置文件无法被导出或者生效的问题和处理方案

所以我们要在pom文件加以下配置

所以本例子父pom.xml的总配置为

<?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、编写单元测试

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、执行单元测试查看结果

猜你喜欢

转载自blog.csdn.net/lgl782519197/article/details/109044268