目录

介绍

# 介绍

什么是MyBatis?

  • 一款优秀的持久层框架,用于简化JDBC开发
  • 官网:https://mybatis.org/mybatis-3/zh/index.html

持久层

  • 负责将数据保存到数据库的那一层代码
  • JavaEE三层架构:表现层、业务层、持久层

# 搭建流程

  1. 构建项目 ![[Pasted image 20220608184155.png]]
  2. 添加项目所需的依赖(MyBatis和MySQL-Connection)
<dependencies>  
  <dependency>    
	  <groupId>mysql</groupId>  
      <artifactId>mysql-connector-java</artifactId>  
      <version>8.0.29</version>  
  </dependency>  
  <dependency>    
	  <groupId>org.mybatis</groupId>  
      <artifactId>mybatis</artifactId>  
      <version>3.5.7</version>  
  </dependency>  
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
  1. src/main/javasrc/main/resources路径下构建相同的包名,如com.test.mapper,请务必确保相同的包名,否则会影响到mapper映射。
  2. resource下创建任意名称的xml类型文件作为mybatis的配置文件,例如config.xml
<!-- 简单的配置文件 -->

<?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.cj.jdbc.Driver"/>  
                <property name="url" value="jdbc:mysql:///testing?"/>  
                <property name="username" value="root"/>  
                <property name="password" value="Lam1134312514"/>  
            </dataSource>        
        </environment>    
    </environments>    
	<mappers>        
		<mapper resource="com/testbatis/mapper/FirstMapper.xml"/>  
	</mappers>
</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  1. java/com/testbatis/mapper/路径下创建接口用于命名方法。如命名: FirstMapper,并在resources/com/testbatis/mapper下创建相同名称的文件,类型为xml
// FirstMapper接口
package com.testbatis.mapper;  
  
import java.util.List;  
  
public interface FirstMapper {  
    List<Integer> selectID();  
    List<String> selectName();  
}
1
2
3
4
5
6
7
8
9
// FirstMapper.xml配置文件
<?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="com.testbatis.mapper.FirstMapper">  <!-- 此处路径必须一致 -->
    <select id="selectID" resultType="int">  
        select id from stu;  
    </select>  
    <select id="selectName" resultType="String">  
        select name from stu;  
    </select>  
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
  1. 在入口主类写入以下代码完成配置
public static void main(String[] args) throws IOException {  
    //1. 加载mybatis的核心配置文件,获取SqlSessionFactory  
    String resource = "config.xml";  
    InputStream inputStream = Resources.getResourceAsStream(resource);  
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
  
    //2. 获取SqlSession对象,用它来执行sql  
    SqlSession sqlSession = sqlSessionFactory.openSession();  
  
    //3. 执行sql  
    FirstMapper um = sqlSession.getMapper(FirstMapper.class);  
    System.out.println(um.selectName());  
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 潜在问题 - SqlSessionFactory

SqlSessionFactory在创建时会占用许多资源,倘若许多网页都有一个SqlSessionFactory,这将占用服务器许多资源,因此我们可以考虑将其只初始化一次,例如创建一个SqlSessionFactoryUtil类

public class SqlSessionFactoryUtil {  
    private static SqlSessionFactory sqlSessionFactory;  
    static{  
        try {  
            String resource = "config.xml";  
            InputStream inputStream = Resources.getResourceAsStream(resource);  
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
        } catch (IOException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    public static SqlSessionFactory getSqlSessionFactory(){  
        return sqlSessionFactory;  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

放在static代码块中的代码会在加载类时只运行一次,因此能确保不会生成额外的SqlSessionFactory,并且通过getSqlSessionFactory()方法能够从外部获取到它。

最近更新
01
基本知识
07-18
02
卷积神经网络识别图像
07-18
03
损失函数
07-18
更多文章>