2 The first Mybatis program

Set up the environment

Build a database

CREATE DATABASE mybatis;

USE mybatis;

CREATE TABLE `user`(
    `id` INT AUTO_INCREMENT NOT NULL,
    `name` VARCHAR(20),
    `pwd` VARCHAR(50),
    PRIMARY KEY(id)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

New Project

Use idea to create a normal maven project

Import dependencies

<dependencies>
    <!--    sql依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <!--    mybatis依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
    <!--    junit依赖-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version> 4.13</version>
    </dependency>
</dependencies>

Create a module

Write core configuration files

Create mybatis-config.xml 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">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

Ordinary maven projects cannot access the resources file in the src directory

Need to add in pom.xml

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

Write mybatis util tool class

The tool class here helps us connect to the database, and the SqlSession object is similar to the Connection object

package com.qin;

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;

public class utils {
    private static SqlSessionFactory sqlSessionFactory;
    static{
        //获取sqlSessionFactory对象
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //使用SqlSessionFactory sqlSessionFactory创建sqlSession实例
    //SqlSession完全包含了面向数据库执行sql命令所需要的所有方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

Write code

Write UserDao

package com.qin.dao;

import com.qin.pojo.User;

import java.util.List;

public interface UserDao {
    List<User> getUserList();
}

Write Mapper.xml file -> similar to write UserDaoImpl

<?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">
<!--类似继承UserDao接口-->
<mapper namespace="com.qin.dao.UserDao">
<!--    实现UserDao里面的方法-->
    <select id="getUserList" resultType="com.qin.pojo.User">
    select * from mybatis.user;
  </select>
</mapper>

Register mapper in mabatis-config.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="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
<!--    注册mapper-->
    <mappers>
        <mapper resource="com/qin/dao/UserMapper.xml"/>
    </mappers>
</configuration>

When this error occurs, the mapper is not registered

Insert picture description here

test

package com.qin.dao;

import com.qin.pojo.User;
import com.qin.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test1(){
        //使用工具类获得SqlSesison
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> userList = userDao.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
}

Solve the idea of ​​automatically lowering the jdk version

Add the maven-compiler-plugin plug-in to the pom file and specify the jdk version used by jdk to solve the above problem. The configuration of maven-compiler-plugin is effective for both Java compiler and Language level at the same time.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>