package org.itstack.demo.design.mediator;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.Date;
import java.util.*;
/**
* 公众号 | bugstack虫洞栈
* 博 客 | https://bugstack.cn
* Create by 小傅哥 @2020
*/
public class DefaultSqlSession implements SqlSession {
private Connection connection;
private Map<String, XNode> mapperElement;
public DefaultSqlSession(Connection connection, Map<String, XNode> mapperElement) {
this.connection = connection;
this.mapperElement = mapperElement;
}
@Override
public <T> T selectOne(String statement) {
try {
XNode xNode = mapperElement.get(statement);
PreparedStatement preparedStatement = connection.prepareStatement(xNode.getSql());
ResultSet resultSet = preparedStatement.executeQuery();
List<T> objects = resultSet2Obj(resultSet, Class.forName(xNode.getResultType()));
return objects.get(0);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public <T> T selectOne(String statement, Object parameter) {
XNode xNode = mapperElement.get(statement);
Map<Integer, String> parameterMap = xNode.getParameter();
try {
PreparedStatement preparedStatement = connection.prepareStatement(xNode.getSql());
buildParameter(preparedStatement, parameter, parameterMap);
ResultSet resultSet = preparedStatement.executeQuery();
List<T> objects = resultSet2Obj(resultSet, Class.forName(xNode.getResultType()));
return objects.get(0);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public <T> List<T> selectList(String statement) {
XNode xNode = mapperElement.get(statement);
try {
PreparedStatement preparedStatement = connection.prepareStatement(xNode.getSql());
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet2Obj(resultSet, Class.forName(xNode.getResultType()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public <T> List<T> selectList(String statement, Object parameter) {
XNode xNode = mapperElement.get(statement);
Map<Integer, String> parameterMap = xNode.getParameter();
try {
PreparedStatement preparedStatement = connection.prepareStatement(xNode.getSql());
buildParameter(preparedStatement, parameter, parameterMap);
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet2Obj(resultSet, Class.forName(xNode.getResultType()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void buildParameter(PreparedStatement preparedStatement, Object parameter, Map<Integer, String> parameterMap) throws SQLException, IllegalAccessException {
int size = parameterMap.size();
// 单个参数
if (parameter instanceof Long) {
for (int i = 1; i <= size; i++) {
preparedStatement.setLong(i, Long.parseLong(parameter.toString()));
}
return;
}
if (parameter instanceof Integer) {
for (int i = 1; i <= size; i++) {
preparedStatement.setInt(i, Integer.parseInt(parameter.toString()));
}
return;
}
if (parameter instanceof String) {
for (int i = 1; i <= size; i++) {
preparedStatement.setString(i, parameter.toString());
}
return;
}
Map<String, Object> fieldMap = new HashMap<>();
// 对象参数
Field[] declaredFields = parameter.getClass().getDeclaredFields();
for (Field field : declaredFields) {
String name = field.getName();
field.setAccessible(true);
Object obj = field.get(parameter);
field.setAccessible(false);
fieldMap.put(name, obj);
}
for (int i = 1; i <= size; i++) {
String parameterDefine = parameterMap.get(i);
Object obj = fieldMap.get(parameterDefine);
if (obj instanceof Short) {
preparedStatement.setShort(i, Short.parseShort(obj.toString()));
continue;
}
if (obj instanceof Integer) {
preparedStatement.setInt(i, Integer.parseInt(obj.toString()));
continue;
}
if (obj instanceof Long) {
preparedStatement.setLong(i, Long.parseLong(obj.toString()));
continue;
}
if (obj instanceof String) {
preparedStatement.setString(i, obj.toString());
continue;
}
if (obj instanceof Date) {
preparedStatement.setDate(i, (java.sql.Date) obj);
}
}
}
private <T> List<T> resultSet2Obj(ResultSet resultSet, Class<?> clazz) {
List<T> list = new ArrayList<>();
try {
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
// 每次遍历行值
while (resultSet.next()) {
T obj = (T) clazz.newInstance();
for (int i = 1; i <= columnCount; i++) {
Object value = resultSet.getObject(i);
String columnName = metaData.getColumnName(i);
String setMethod = "set" + columnName.substring(0, 1).toUpperCase() + columnName.substring(1);
Method method;
if (value instanceof Timestamp) {
method = clazz.getMethod(setMethod, Date.class);
} else {
method = clazz.getMethod(setMethod, value.getClass());
}
method.invoke(obj, value);
}
list.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public void close() {
if (null == connection) return;
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本项目汇集了基于Java语言的23种经典设计模式的源码分析,包含356个文件,其中297个为Java源文件,其余为XML配置文件、Git忽略规则文件、Markdown文件、YAML文件等,旨在深入理解和应用设计模式。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java语言的23种经典设计模式源码分析 (356个子文件)
spring.factories 119B
.gitignore 2KB
DefaultSqlSession.java 6KB
ApiTest.java 6KB
ClassLoaderUtils.java 6KB
SqlSessionFactoryBuilder.java 5KB
QuestionBankController.java 4KB
DecorationPackageController.java 3KB
GroupStructure.java 3KB
DoJoinPoint.java 3KB
PrizeController.java 3KB
ActivityExecStatusController.java 3KB
QuestionBank.java 3KB
ApiTest.java 3KB
QuestionBankController.java 3KB
ApiTest.java 3KB
Bilibili.java 2KB
HttpClient.java 2KB
ApiTest.java 2KB
ApiTest.java 2KB
ApiTest.java 2KB
ApiTest.java 2KB
DecorationPackageMenu.java 2KB
ApiTest.java 2KB
Bilibili.java 2KB
EngineBase.java 2KB
ActivityService.java 2KB
StateHandler.java 2KB
ApiTest.java 2KB
TreeNode.java 2KB
ApiTest.java 2KB
Demo.java 2KB
AbstractFactoryClient.java 2KB
DeliverReq.java 2KB
State.java 2KB
CheckState.java 2KB
GoodsCommodityService.java 2KB
CacheServiceImpl.java 2KB
BaseLogic.java 2KB
AbstractFactoryTest.java 2KB
RefuseState.java 2KB
PassState.java 2KB
TopicRandomUtil.java 2KB
EngineResult.java 2KB
Cmd.java 1KB
ApiTest.java 1KB
ApiTest.java 1KB
XiaoEr.java 1KB
EventManager.java 1KB
EditingState.java 1KB
OpenState.java 1KB
RegisterBeanFactory.java 1KB
HelloWorldController.java 1KB
DangDangNetMall.java 1KB
Builder.java 1KB
TaoBaoNetMall.java 1KB
CarFactory.java 1KB
PayController.java 1KB
UserInfo.java 1KB
JDNetMall.java 1KB
CloseState.java 1KB
Level2AuthLink.java 1KB
Level3AuthLink.java 1KB
DoingState.java 1KB
POPOrderDelivered.java 1KB
AuthController.java 1KB
ApiTest.java 1KB
Activity.java 1KB
XNode.java 1KB
NetMall.java 1KB
AwardReq.java 1KB
MapperFactoryBean.java 1KB
JDBCUtil.java 1KB
Product.java 1KB
StarterAutoConfigure.java 1KB
CarTest.java 1KB
TreeNodeLink.java 1KB
DataView.java 1KB
Product.java 1KB
AuthLink.java 1KB
Video.java 1KB
ApiTest.java 1KB
ActivityInfo.java 1KB
ConfigFile.java 1KB
Resources.java 1KB
LoginSsoDecorator.java 1KB
MJCouponDiscount.java 1KB
CouponCommodityService.java 1KB
RedisUtils.java 1KB
TreeRich.java 1KB
IIR.java 1KB
EGM.java 1KB
CardCommodityService.java 1KB
create_account.java 1016B
LotteryService.java 1001B
Rectangle.java 993B
Level1AuthLink.java 993B
EngineConfig.java 991B
OrderMq.java 988B
Admin.java 988B
共 356 条
- 1
- 2
- 3
- 4
资源评论
csbysj2020
- 粉丝: 2664
- 资源: 5507
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Java 多线程课程的代码及少量注释.zip
- 数据库课程设计-基于的个性化购物平台的建表语句.sql
- 数据库课程设计-基于的图书智能一体化管理系统的建表语句.sql
- Java 代码覆盖率库.zip
- Java 代码和算法的存储库 也为该存储库加注星标 .zip
- 免安装Windows10/Windows11系统截图工具,无需安装第三方截图工具 双击直接使用截图即可 是一款免费可靠的截图小工具哦~
- Libero Soc v11.9的安装以及证书的获取(2021新版).zip
- BouncyCastle.Cryptography.dll
- 5.1 孤立奇点(JD).ppt
- 基于51单片机的智能交通灯控制系统的设计与实现源码+报告(高分项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功