你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

第一个MyBatis程序

2021/11/29 16:19:37

环境的搭建

我们使用IDEA创建一个maven项目,在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>org.example</groupId>
    <artifactId>MyBatisDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>

        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

右键重新载入项目即可实现导入
在这里插入图片描述

在mysql中提前建好表

<?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/user?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="c8918833"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/caoyuqi/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

书写mybatis-config.xml文件
这里要注意数据库url的书写,&amp代表&。在下面的mappers中书写匹配的mapper配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caoyuqi.dao.UserDao">
    <select id="getAllUser" resultType="com.caoyuqi.model.User">
        select * from User.man;
    </select>
</mapper>

在mapper中我们要书写resultType代表返回的结果类型,selectid对应Mapper相应的方法名 namespace对应相关的Dao

package com.caoyuqi.dao;

import com.caoyuqi.model.User;

import java.util.List;

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

package com.caoyuqi.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 MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSession getMapping()
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}

Mybatis最重要的获取session代码如上所示

import com.caoyuqi.dao.UserDao;
import com.caoyuqi.model.User;
import com.caoyuqi.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserTest {
    @Test
    public void testQuery()
    {
        SqlSession mapping = MybatisUtils.getMapping();
        UserDao mapper = mapping.getMapper(UserDao.class);
        List<User> allUser = mapper.getAllUser();
        for (User user :
                allUser) {
            System.out.println(user);
        }

    }
}

编写相关的测试代码
在这里插入图片描述
如此我们便编写了第一个Mybatis程序对数据库进行了查询操作