package com.kkb.mybatis.test;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import com.kkb.mybatis.framework.config.Configuration;
import com.kkb.mybatis.framework.config.MappedStatement;
import com.kkb.mybatis.framework.sqlsource.BoundSql;
import com.kkb.mybatis.framework.sqlsource.ParameterMapping;
import com.kkb.mybatis.framework.sqlsource.iface.SqlSource;
import com.kkb.mybatis.po.User;
import com.kkb.mybatis.utils.SimpleTypeRegistry;
/**
* 目的是使用XML来表达mybatis的全局配置信息,和业务相关的SQL映射信息 (映射文件) 其次,优化数据连接的创建(使用连接池)
*
* @author 灭霸詹
*
*/
public class MybatisV2 {
/**
* 封装了全局配置文件和映射文件中的所有信息
*/
private Configuration configuration = new Configuration();
private String namespace;
@Test
public void test() {
loadConfiguration();
User user = new User();
user.setUsername("王五");
List<User> users = selectList("queryUserById", user);
System.out.println(users);
}
private void loadConfiguration() {
// 指定全局配置文件的路径
String location = "mybatis-config.xml";
// 获取对应的InputStream对象
InputStream inputStream = getInputStream(location);
// 获取Document对象
Document document = createDocument(inputStream);
// 按照全局配置文件和映射文件的XML语义进行解析
loadConfigurationElement(document.getRootElement());
}
/**
*
* @param rootElement
* <configuration>
*/
private void loadConfigurationElement(Element rootElement) {
Element environments = rootElement.element("environments");
parseEnvironments(environments);
Element mappers = rootElement.element("mappers");
parseMappers(mappers);
}
/**
*
* @param mappers
* <mappers>
*/
@SuppressWarnings("unchecked")
private void parseMappers(Element mappers) {
List<Element> mapperElements = mappers.elements("mapper");
for (Element mapperElement : mapperElements) {
parseMapper(mapperElement);
}
}
private void parseMapper(Element mapperElement) {
String resource = mapperElement.attributeValue("resource");
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resource);
Document document = createDocument(inputStream);
// 按照映射文件的语义进行解析
parseXmlMapper(document.getRootElement());
}
@SuppressWarnings("unchecked")
private void parseXmlMapper(Element rootElement) {
// 为了方便管理statement,需要使用statement唯一标识
namespace = rootElement.attributeValue("namespace");
List<Element> selectElements = rootElement.elements("select");
for (Element selectElement : selectElements) {
parseStatementElement(selectElement);
}
}
private void parseStatementElement(Element selectElement) {
String statementId = selectElement.attributeValue("id");
if (statementId == null || selectElement.equals("")) {
return;
}
// 一个CURD标签对应一个MappedStatement对象
// 一个MappedStatement对象由一个statementId来标识,所以保证唯一性
// statementId = namespace + "." + CRUD标签的id属性
statementId = namespace + "." + statementId;
String parameterType = selectElement.attributeValue("parameterType");
Class<?> parameterClass = resolveType(parameterType);
String resultType = selectElement.attributeValue("resultType");
Class<?> resultClass = resolveType(resultType);
String statementType = selectElement.attributeValue("statementType");
statementType = statementType == null || statementType == "" ? "prepared" : statementType;
// SqlSource的封装过程
SqlSource sqlSource = createSqlSource(selectElement);
// TODO 建议使用构建者模式去优化
MappedStatement mappedStatement = new MappedStatement(statementId, parameterClass, resultClass, statementType,
sqlSource);
configuration.addMappedStatement(statementId, mappedStatement);
}
private SqlSource createSqlSource(Element selectElement) {
return null;
}
private Class<?> resolveType(String parameterType) {
try {
Class<?> clazz = Class.forName(parameterType);
return clazz;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param environments
* <environments>
*/
@SuppressWarnings("unchecked")
private void parseEnvironments(Element environments) {
String def = environments.attributeValue("default");
List<Element> envList = environments.elements();
for (Element element : envList) {
String id = element.attributeValue("id");
if (id.equals(def)) {
parseDataSource(element);
}
}
}
/**
*
* @param element
* <environment>
*/
private void parseDataSource(Element element) {
Element dataSourceElement = element.element("dataSource");
String type = dataSourceElement.attributeValue("type");
if (type.equals("DBCP")) {
BasicDataSource dataSource = new BasicDataSource();
Properties properties = parseProperty(dataSourceElement);
dataSource.setDriverClassName(properties.getProperty("driver"));
dataSource.setUrl(properties.getProperty("url"));
dataSource.setUsername(properties.getProperty("username"));
dataSource.setPassword(properties.getProperty("password"));
configuration.setDataSource(dataSource);
}
}
@SuppressWarnings("unchecked")
private Properties parseProperty(Element dataSourceElement) {
Properties properties = new Properties();
List<Element> propElements = dataSourceElement.elements("property");
for (Element propElement : propElements) {
String name = propElement.attributeValue("name");
String value = propElement.attributeValue("value");
properties.put(name, value);
}
return properties;
}
private Document createDocument(InputStream inputStream) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputStream);
return document;
} catch (Exception e) {
}
return null;
}
private InputStream getInputStream(String location) {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(location);
return inputStream;
}
@SuppressWarnings("unchecked")
private <T> List<T> selectList(String statementId, Object paramObject) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
// 要返回的结果集合
List<Object> results = new ArrayList<Object>();
try {
// 获取数据源对象,通过数据源获取连接
connection = getConnection(configuration.getDataSource());
MappedStatement mappedStatement = configuration.getMappedStatementById(statementId);
// 定义sql语句 ?表示占位符
SqlSource sqlSource = mappedStatement.getSqlSource();
// SqlSource的执行过程
BoundSql boundSql = sqlSource.getBoundSql(paramObject);
String sql = boundSql.getSql();
// 获取预处理 statement
preparedStatement = connection.prepareStatement(sql);
// 设置参数
handleParameter(preparedStatement, mappedStatement, boundSql, paramObject);
// 向数据库发出 sql 执行查询,查询出结果集
rs = preparedStatement.executeQuery();
// 获取要映射的结果类型
handleResultSet(rs, results, mappedStatement);
} catch (Exception e) {
没有合适的资源?快使用搜索试试~ 我知道了~
手写mybatis核心流程代码demo
共39个文件
class:12个
java:12个
xml:6个
需积分: 1 0 下载量 129 浏览量
2023-04-22
22:56:37
上传
评论
收藏 18KB 7Z 举报
温馨提示
手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写mybatis核心流程代码demo手写myba
资源推荐
资源详情
资源评论
收起资源包目录
mybatis-demo-13.7z (39个子文件)
mybatis-demo-13
.classpath 1KB
.settings
org.eclipse.jdt.core.prefs 243B
org.eclipse.core.resources.prefs 93B
org.eclipse.m2e.core.prefs 90B
pom.xml 2KB
src
test
resources
java
main
resources
mapper
UserMapper.xml 662B
jdbc.properties 399B
mybatis-config.xml 688B
java
com
kkb
mybatis
utils
SimpleTypeRegistry.java 2KB
framework
sqlsource
iface
SqlSource.java 451B
BoundSql.java 781B
ParameterMapping.java 497B
config
MappedStatement.java 2KB
Configuration.java 732B
test
MybatisV1.java 5KB
JdbcDemo.java 2KB
OgnlDemo.java 4KB
MybatisV3.java 64B
MybatisV2.java 10KB
po
User.java 973B
target
classes
mapper
UserMapper.xml 662B
jdbc.properties 399B
mybatis-config.xml 688B
META-INF
MANIFEST.MF 129B
maven
com.kkb
mybatis-demo-13
pom.properties 245B
pom.xml 2KB
com
kkb
mybatis
utils
SimpleTypeRegistry.class 1KB
framework
sqlsource
iface
SqlSource.class 276B
ParameterMapping.class 1KB
BoundSql.class 1KB
config
MappedStatement.class 2KB
Configuration.class 2KB
test
JdbcDemo.class 3KB
MybatisV3.class 294B
MybatisV1.class 6KB
OgnlDemo.class 3KB
MybatisV2.class 12KB
po
User.class 2KB
test-classes
.project 567B
共 39 条
- 1
资源评论
Java码库
- 粉丝: 1424
- 资源: 3918
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab开发的全面详解LTE:MATLAB建模、仿真与实现-simulink.rar
- 自动驾驶定位系列教程二:系统架构.pdf
- 整站程序8优技巧网-8ujq.rar
- 世界各个国家或地区国际域名缩写
- 基于matlab开发的根据rvm回归模型自己编的matlab程序.rar
- 基于matlab开发的该程序为国内一所大学编写的LTE链路层仿真程序,根据LTE标准协议编写的,很容易看懂.rar
- 高效C++学生成绩管理系统:教育技术+C++17编程+数据管理+教务自动化
- 搜索链接要广告分类系统 v2.0-yad20.rar
- 基于matlab开发的Tipping的相关向量机RVM的回归MATLAB程序,有英文注释,可以运行.rar
- 一个点击正反转程序实例,可实现案件电机正反转
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功