文章目录
- mybatis原理:
- mybatis缓存机制
mybatis原理:
mybatis的工作原理就是:先封装sql,接着调用jdbc操作数据库,最后把数据库返回的表结果封装成java类。
通过代码实现jdbc查询操作:
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><typeAliases><typeAlias alias="ProductCategory" type="com.example.springbootlearn.mapper.ProductCategory" /></typeAliases><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://127.0.0.1:3306/sell?serverTimezone=UTC" /><property name="username" value="root" /><property name="password" value="" /></dataSource></environment></environments><mappers><mapper resource="mapper/ProductCategoryMapper.xml" /></mappers>
</configuration>
ProductCategory实体类:
@Data
public class ProductCategory {/** 类目id. */private Integer categoryId;/** 类目名字. */private String categoryName;/** 类目编号. */private Integer categoryType;public ProductCategory() {}public ProductCategory(String categoryName, Integer categoryType) {this.categoryName = categoryName;this.categoryType = categoryType;}
}
@Mapper
public interface ProductCategoryMapper {ProductCategory selectByCategoryType(Integer categoryType);
}
<?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.example.springbootlearn.mapper.ProductCategoryMapper" ><resultMap id="BaseResultMap" type="com.example.springbootlearn.mapper.ProductCategory"><id column="category_id" property="categoryId" jdbcType="INTEGER" /><id column="category_name" property="categoryName" jdbcType="VARCHAR" /><id column="category_type" property="categoryType" jdbcType="INTEGER" /></resultMap><select id="selectByCategoryType" resultMap="BaseResultMap" parameterType="java.lang.Integer">select category_id, category_name, category_typefrom product_categorywhere category_type = #{category_type, jdbcType=INTEGER}</select>
</mapper>
private Configuration configuration;
private JdbcTransaction jdbcTransaction;
private Connection connection;
private Reader resourceAsReader;
private SqlSessionFactory sqlSessionFactory;public void init() throws SQLException, IOException {connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sell?characterEncoding=utf8&serverTimezone=UTC", "root", "");resourceAsReader = Resources.getResourceAsReader("Mybatis-config.xml");jdbcTransaction = new JdbcTransaction(connection);sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);configuration = sqlSessionFactory.getConfiguration();
}/*** 相同的sql只会编译处理一次*/
@Test
public void b() throws IOException, SQLException {init();SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);ProductCategoryMapper mapper = sqlSession.getMapper(ProductCategoryMapper.class);System.out.println(mapper.findByCategoryType(1));
}
执行test-b方法,即可。
原理分析:
文字描述如下:
- 读取mybatis配置文件,包含了数据库连接信息。
- 加载映射文件,mybatis-config.xml可以配置多个xml映射文件(一个映射文件对应一个表)
- 构建工厂会话SqlSessionFactory---------new SqlSessionFactoryBuilder().build
- 通过工厂会话创建sqlSession----------sqlSessionFactory.openSession
- Executor执行器:动态执行sql语句。
- MappedStatement 对象:入参映射,结果映射。在Executor 接口中有一个MappedStatement 类型的参数。
mybatis缓存机制
分两个:一级缓存SqlSession级别缓存(本地缓存),默认开启,二级缓存mapper级别的
- sqlSession缓存:
- 第一次执行select查询,会将查到的结果放入map中存起来。
- 第二次查询,如果select相同且参数一样,那么就从缓存中返回数据,不去查数据库,从而提高了效率
- 作用域是session级别的,如果删除了,或者关闭了,下次查询还是重数据库中查询。
- mapper缓存:二级
- 存储结果也是map。作用域为mapper.xml(namespace)
- 默认没开启
- 第一次是调用mapper的sql查询数据库,查到数据会放入改对应的mapper二级缓存区域,
- 第二次调用,相同的sql就会调用二级缓存了。