Mybatis框架:逆向工程,运行原理(底层原码分析)

逆向工程

逆向工程就是根据你的表,自动来创建相应的POJO,mapper接口,sql映射文件
主要看官方文档的说明的例子,这里给个例子

XML配置文档

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
	
	<!-- context是一些环境设置
	The <context> element is used to specify the environment for generating a set of objects.
	 -->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<!-- jdbcConnection:指定如何连接到目标数据库 -->
		<jdbcConnection
			driverClass="com.mysql.cj.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8" 
			userId="root" 
			password="1234">
		</jdbcConnection>
		
		<!-- 类型解析器,默认就行 -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		
		<!-- javaModelGenerator:指定JavaBean的生成策略
				targetPackage:指定目标包名
				targetProject:指定目标工程
		 -->
		<javaModelGenerator 
			targetPackage="bean"
			targetProject=".\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		
		<!-- sqlMapGenerator:sql映射生成策略,就是那个映射xml文件
				targetPackage:包
				targetProject:工程
		 -->
		<sqlMapGenerator 
			targetPackage="bean"
			targetProject=".\conf">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		
		<!--javaClientGenerator:mapper接口生成策略 
		 -->
		<javaClientGenerator 
			type="XMLMAPPER"
			targetPackage="bean" 
			targetProject=".\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		<!-- 指定要逆向分析哪些表,根据表创建JavaBean -->
		<table tableName="emp" domainObjectName="Emp"></table>
		<table tableName="dept" domainObjectName="Dept"></table>

	</context>
</generatorConfiguration>

自动生成的Java代码

	@org.junit.Test
	public void test2() throws Exception
	{
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("mbg.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}

使用自动生成的查询

生成后的类你会发现还有一个是EmpExample的类
这就是自动生成的具有QBC(Query By Criteria)风格的复杂查询,
下面就使用一下

	@org.junit.Test
	public void test1() throws IOException
	{
		String resource = "mybatis.xml"; 
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession openSession = sqlSessionFactory.openSession();
		try
		{
			EmpMapper mapper = openSession.getMapper(EmpMapper.class);
			//xxxByExample就是用来封装查询条件的
			//查询所有的数据
			//发送的sql是:select id, lastname, email, gender, did from emp
//			List<Emp> selectByExample = mapper.selectByExample(null);
//			for (Emp emp : selectByExample)
//			{
//				System.out.println(emp);
//			}
			
			/*
			 * 现在想复杂查询,查询名字带有e的男性员工,或者email中带有e的员工
			 * 1.首先example是封装员工查询条件的类
			 * 2.而criteria是拼接查询条件的
			 * 如果是and的条件,全部放在第一个criteria里面就行,它会以and方式进行拼接
			 * 如果是or的条件,这得新建一个criteria,并且调用example.or(criteria2);
			 * 发的SQL语句:select id, lastname, email, gender, did from emp WHERE ( lastname like ? and gender = ? ) or( email like ? )
			 */
			
			EmpExample example=new EmpExample();
			Criteria criteria = example.createCriteria();
			criteria.andLastnameLike("%e%");
			criteria.andGenderEqualTo("男");
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andEmailLike("%e%");
			example.or(criteria2);
			
			List<Emp> selectByExample = mapper.selectByExample(example);
			for (Emp emp : selectByExample)
			{
				System.out.println(emp);
			}
			
		}finally
		{
			openSession.close();
		}
	}

运行原理

结构层次

在这里插入图片描述

1.获取sqlSessionFactory对象

根据配置文件创建SQLSessionFactory
在这里插入图片描述
Configuration封装了所有配置文件的详细信息
步骤是:
在这里插入图片描述
一些重要的对象的操作过程
解析器,使用DOM4j的解析器
在这里插入图片描述
解析器对映射文件的解析,也是使用DOM4j的解析器
在这里插入图片描述

Configuration对象保存了所有配置文件的详细信息
在这里插入图片描述

全局Configuration中的一个重要属性:mappedStatements,每一个代表一个增删改查标签
在这里插入图片描述

全局Configuration中的一个重要属性:mapperRegistry
mapperRegistry里面是mapper接口的代理工厂,代理对象就是由工厂创建的
在这里插入图片描述

总结:把配置文件的信息解析并保存在Configuration对象中,返回包含了Configuration的DefaultSqlSession对象。

2.获取sqlSession对象

返回SqlSession的实现类DefaultSqlSession对象。
他里面包含了Executor和Configuration;
Executor会在这一步被创建

步骤是
在这里插入图片描述

getMapper

getMapper返回接口的代理对象包含了SqlSession对象
在这里插入图片描述
步骤是
在这里插入图片描述

查询

步骤
在这里插入图片描述

重要对象
boundSql对象:是在执行executor.query()的时候创建的,里面包含很多SQL语句,设置参数,参数的Java类型等等信息
在这里插入图片描述
使用缓存,缓存中保存的key:方法id+sql+参数xxx
在这里插入图片描述

总结
StatementHandler:处理sql语句预编译,设置参数等相关工作;
ParameterHandler:设置预编译参数用的
ResultHandler:处理结果集
TypeHandler:在整个过程中,进行数据库类型和javaBean类型的映射

在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReflectMirroring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值