首先官方文档永远是最权威和全面的。

1.导入Maven依赖

当然还是上Maven官网找到所需要的版本

  1. <dependency>
  2. <groupId>org.mybatis</groupId>
  3. <artifactId>mybatis</artifactId>
  4. <version>x.x.x</version>
  5. </dependency>

如果不是用Maven构建的话,就导入相应的 mybatis-x.x.x.jar包到类路径中

2.配置mybatis-config.xml

在Maven项目的resources文件夹中,添加mybatis-config.xml配置文件用来配置MyBatis

image-20210425224603798.png

mybatis-config.xml

  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. <environments default="development">
  7. <environment id="development">
  8. <transactionManager type="JDBC"/>
  9. <dataSource type="POOLED">
  10. <property name="driver" value="${driver}"/>
  11. <property name="url" value="${url}"/>
  12. <property name="username" value="${username}"/>
  13. <property name="password" value="${password}"/>
  14. </dataSource>
  15. </environment>
  16. </environments>
  17. <mappers>
  18. <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  19. </mappers>
  20. </configuration>

其中的数据库配置我们可以写在一个db.properties文件中,也同样在resources文件夹中

  1. //这里驱动是mysql8.0的驱动
  2. driver=com.mysql.cj.jdbc.Driver
  3. url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
  4. username=root
  5. password=

3.从 XML 中构建 SqlSessionFactory

我们配置mybatis-config.xml目的就是为了构建SqlSessionFactory,我们可以使用下面的方法来获得:

  1. public class MybaitsUtils {
  2. private static SqlSessionFactory sqlSessionFactory = null;
  3. static {
  4. try {
  5. //核心配置文件在resource资源文件夹中
  6. String resource = "mybatis-config.xml";
  7. InputStream inputStream = Resources.getResourceAsStream(resource);
  8. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. //既然有了SqlSessionFactory,顾名思义,我们就可以从中获得SqlSession 的实例了
  14. //SqlSession 完全包含了面向数据库执行SQL命令所需的所有方法
  15. public static SqlSession getSqlSession(){
  16. return sqlSessionFactory.openSession();
  17. }
  18. }

4.编写mapper接口和对应的xxxMapper.xml

4.1、编写实体类pojo

  1. public class Blog {
  2. private Integer id;
  3. private String title;
  4. private String name;
  5. ...
  6. }

4.2、编写Dao/Mapper接口
  1. public interface BlogDao {
  2. List<Blog> getBlogList();
  3. }

4.3、编写Mapper接口对应的映射文件BlogMapper.xml
  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. <!--namespace绑定一个对应的dao/mapper-->
  6. <mapper namespace="org.mybatis.builder.BlogMapper">
  7. <!--resultType使用全限定名 -->
  8. <select id="getBlogList" resultType="org.mybatis.pojo.Blog">
  9. select * from `bloh`;
  10. </select>
  11. </mapper>

我们这里是将Mapper接口和xxxMapper.xml文件都放一起的,都在src/java文件夹下,这种方式就需要在pom.xml中配置忽略过滤。

当然也可以分开放,在resources文件夹中建一个和Mapper接口相同结构的目录,放入xxxMapper.xml即可。

4.4、去mybatis-config.xml中注册BlogMapper.xml

你可以使用相对于类路径的资源引用,或完全限定资源定位符(包括 file:/// 形式的 URL),或类名和包名等。例如:
  1. <!-- 使用相对于类路径的资源引用 -->
  2. <mappers>
  3. <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  4. <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  5. <mapper resource="org/mybatis/builder/PostMapper.xml"/>
  6. </mappers>
  7. <!-- 使用完全限定资源定位符(URL) -->
  8. <mappers>
  9. <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  10. <mapper url="file:///var/mappers/BlogMapper.xml"/>
  11. <mapper url="file:///var/mappers/PostMapper.xml"/>
  12. </mappers>
  13. <!-- 使用映射器接口实现类的完全限定类名 -->
  14. <mappers>
  15. <mapper class="org.mybatis.builder.AuthorMapper"/>
  16. <mapper class="org.mybatis.builder.BlogMapper"/>
  17. <mapper class="org.mybatis.builder.PostMapper"/>
  18. </mappers>
  19. <!-- 将包内的映射器接口实现全部注册为映射器 -->
  20. <mappers>
  21. <package name="org.mybatis.builder"/>
  22. </mappers>

这些配置会告诉 MyBatis 去哪里找映射文件。

5.简单使用

  1. try(SqlSession sqlSession = MybaitsUtils.getSqlSession()){
  2. Mapper blogMapper = sqlSession.getMapper(BlogMapper.class);
  3. List<Blog> blogList = blogMapper.getBlogList();
  4. for (Blog blog: blogList){
  5. System.out.println(blog.toString());
  6. }
  7. }

6.出现的问题

.xml文件不能被加载到target中

出现该问题是因为maven默认只加载resource目录中的资源文件,而我的UserMapper.xml文件是放在java目录中的,所以加载不到。只要在maven的配置文件pom.xml中配置允许加载即可。

  1. <!--允许以下格式的资源被加载-->
  2. <build>
  3. <resources>
  4. <resource>
  5. <directory>src/main/resources</directory>
  6. <includes>
  7. <include>**/*.properties</include>
  8. <include>**/*.xml</include>
  9. </includes>
  10. <filtering>true</filtering>
  11. </resource>
  12. <resource>
  13. <directory>src/main/java</directory>
  14. <includes>
  15. <include>**/*.properties</include>
  16. <include>**/*.xml</include>
  17. </includes>
  18. <filtering>true</filtering>
  19. </resource>
  20. </resources>
  21. </build>