第一个Mybatis程序

1. maven依赖

父级pom.xml

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.49</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. 连接数据库配置文件

mybatis-config.xml resources目录下

<?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/java_web?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="vip/yushihua/dao/UserMapper.xml"/>
    </mappers>
</configuration>

如果没有写这个mappers的话,就会报错

org.apache.ibatis.binding.BindingException: Type interface vip.yushihua.dao.UserDao is not known to the MapperRegistry.

3. 编写Mybatis工具类

MybatisUtil utils目录下

package vip.yushihua.utils;

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 MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory = null;

    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

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

}

4. 编写dao接口mapper文件

以前是写的daoImpl实现类,现在不用写这个实现类了,将sql语句写到mapper.xml里面

UserMapper.xml 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">
<mapper namespace="vip.yushihua.dao.UserDao">
    <select id="getUserList" resultType="vip.yushihua.pojo.User">
        select * from users
    </select>
</mapper>

namespace是数据接口的类名

id是接口的方法名

resultType是返回数据的类名,写全

select 里面放sql语句

5. 编写测试

src/test/java/vip/yushihua/dao/UserDaoTest.java

package vip.yushihua.dao;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import vip.yushihua.pojo.User;
import vip.yushihua.utils.MybatisUtil;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test() {
        // 1. 获取sqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> userList = userDao.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
}
Last modification:May 2nd, 2021 at 08:16 pm
哎呀呀,大家随意,随意就好!