Mybatis学习笔记

Mybatis学习笔记

项目总目录

在这里插入图片描述

数据库准备

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int(50) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', '张三', '20');
INSERT INTO `users` VALUES ('2', '李四', '22');

####项目依赖准备

		<!-- mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- junit测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

创建实体类

com.schong.domain.Student

public class Student {
    private Integer id;
    
    private String name;
    
    private Integer age;

    set/get
}

创建Dao层接口

com.schong.dao.StudentDao

public interface StudentDao {
    void selectAll();
}

创建Dao层映射文件和Mybatis主配置文件

Dao层映射

该文件是用来与StudentDao文件进行映射的,也可以看成是StudentDao接口的实现

文件目录

在这里插入图片描述

<?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">
<!--namespace:用来区别不同的类的名字 -->
<mapper namespace="com.schong.dao.StudentDao">
<select id="selectAll" resultType="com.schong.domain.Student">
    select * from users
</select>

</mapper>
Mybatis主配置文件

!!!!!注意!!!!!

上面创建的Dao层映射文件要在这个主配置文件中声明

文件目录

在这里插入图片描述

<?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"/>
            <!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test_mybatis" />
                <property name="username" value="root" />
                <property name="password" value="1234" />
            </dataSource>
        </environment>
    </environments>
    <!-- mapping文件路径配置 -->
    <mappers>
        <mapper resource="mapper/StudentDao.xml"/>
    </mappers>

</configuration>

编写测试类

文件位置

在这里插入图片描述

import com.schong.domain.Student;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MainTest {
    @Test
    public void test1() throws IOException {
        //1.读取配置文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //3.使用工厂生产SqlSession对象
        SqlSession session = sqlSessionFactory.openSession();
        //4.执行Sql语句
        List<Student> all = session.selectList("selectAll");
        //5. 打印结果
        for (Student student : all) {
            System.out.println(student.getName());
        }
        //6.释放资源
        session.close();
        in.close();
    }
}

控制台输出结果

ent student : all) {
System.out.println(student.getName());
}
//6.释放资源
session.close();
in.close();
}
}

控制台输出结果
在这里插入图片描述


版权声明:本文为weixin_45319698原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。