mybatis

概述

  • MyBatis 是一款优秀的持久层框架,用于简化 JDBC 开发

案例

利用MyBatis框架,从MySQL中查询所有的用户

  • 查询user表中所有数据
    • 创建user表,添加数据
    • 创建模块,导入坐标
    • 编写用户实体类(User)
    • 编写 MyBatis 核心配置文件 复制资料里面的mybatis-config.xml到resources
    • 创建UserMapper接口
    • 编写 SQL 映射文件 复制资料里面的UserMapper.xml到resources
    • 加载核心配置文件,获取 SqlSessionFactory 对象
    • 获取 SqlSession 对象,执行 SQL 语句
    • 释放资源

image.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.GDKM</groupId>
  7. <artifactId>mybatis-day01-01-helloword</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--单元测试-->
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.12</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <!--MySQL驱动-->
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <version>5.1.46</version>
  22. </dependency>
  23. <!--MyBatis核心包-->
  24. <dependency>
  25. <groupId>org.mybatis</groupId>
  26. <artifactId>mybatis</artifactId>
  27. <version>3.5.5</version>
  28. </dependency>
  29. <!--MyBatis日志配置-->
  30. <!-- 添加slf4j日志api -->
  31. <dependency>
  32. <groupId>org.slf4j</groupId>
  33. <artifactId>slf4j-api</artifactId>
  34. <version>1.7.20</version>
  35. </dependency>
  36. <!-- 添加logback-classic依赖 -->
  37. <dependency>
  38. <groupId>ch.qos.logback</groupId>
  39. <artifactId>logback-classic</artifactId>
  40. <version>1.2.3</version>
  41. </dependency>
  42. <!-- 添加logback-core依赖 -->
  43. <dependency>
  44. <groupId>ch.qos.logback</groupId>
  45. <artifactId>logback-core</artifactId>
  46. <version>1.2.3</version>
  47. </dependency>
  48. </dependencies>
  49. </project>
  1. import java.sql.Date;
  2. public class User {
  3. private int id;
  4. private String username;
  5. private Date birthday;
  6. private String sex;
  7. private String address;
  8. 构造方法 set get toString方法
  9. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <setting name="logImpl" value="STDOUT_LOGGING"/>
  8. </settings>
  9. <!--配置所有别名-->
  10. <typeAliases>
  11. <!--typeAlias标签: 给一个实体类配置别名 type属性:包名.类名 alias属性:别名-->
  12. <!--<typeAlias type="com.gdkm.pojo.User" alias="User"/>-->
  13. <!--package标签:包扫描,扫描包中所有类取别名,别名就是类名
  14. name属性:包名-->
  15. <package name="com.gdkm.pojo"/>
  16. </typeAliases>
  17. <environments default="development">
  18. <environment id="development">
  19. <transactionManager type="JDBC"/>
  20. <dataSource type="POOLED">
  21. <!--配置连接池需要的参数-->
  22. <property name="driver" value="com.mysql.jdbc.Driver"/>
  23. <property name="url" value="jdbc:mysql://localhost:3306/day18?useSSL=false"/>
  24. <property name="username" value="root"/>
  25. <property name="password" value="root"/>
  26. </dataSource>
  27. </environment>
  28. </environments>
  29. <mappers>
  30. <!--<mapper resource="UserMapper.xml"/>-->
  31. <!--package标签: 包扫描, 扫描包里面的接口和映射文件
  32. 前提1: 接口名要和映射文件名字相同
  33. 前提2: 编译后接口和映射文件要在同一个包中-->
  34. <package name="com.gdkm.mapper"/>
  35. </mappers>
  36. </configuration>
  1. public interface UserMapper {
  2. List<User>findAllUsers();
  3. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.gdkm.mapper.UserMapper">
  6. <select id="findAllUsers" resultType="User">
  7. SELECT * FROM USER;
  8. </select>
  9. </mapper>
  1. public class TestMybatis {
  2. @Test
  3. public void testHello() throws IOException {
  4. String resource = "mybatis-config.xml";
  5. //把文件变成流 import org.apache.ibatis.io.Resources
  6. InputStream inputStream = Resources.getResourceAsStream(resource);
  7. //解析xml文件 sqlSessionFactory相当于数据库连接池
  8. SqlSessionFactory sqlSessionFactory =
  9. new SqlSessionFactoryBuilder().build(inputStream);
  10. //sqlSessionFactory.openSession()理解为从连接池中获取连接
  11. //sqlSession 表示数据库连接
  12. SqlSession sqlSession = sqlSessionFactory.openSession();
  13. //给UserMapper接口生成一个实现类(代理对象)
  14. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  15. //调用方法,执行对应的SQL语句,把结果封装成对象
  16. List<User> userList = mapper.findAllUsers();
  17. userList.forEach(System.out::println);
  18. sqlSession.close();
  19. }
  20. }