PHP sql 处理上,没有类似于 java mybatis 的工具,导致进行一些sql 处理时,会有诸多不便,

楼主抽时间写了一个 php 类似 mybatis 的sql 工具,省去了拼装sql 的时间

代码如下

<?php
$sqlStartTarg = "#{";
$sqlEndTarg = "}"; /**
* 替换 #{key} 部分
* Enter description here ...
* @param $valuePart
* @param $valueArray
*/
function replaceValue($valuePart, $valueArray){
global $sqlStartTarg, $sqlEndTarg; $resultValue = null; $key = str_replace($sqlStartTarg, "", $valuePart);
$key = str_replace($sqlEndTarg, "", $key); $key = trim($key); if(array_key_exists($key, $valueArray)){
$resultValue = $valueArray[$key];
}else{
$resultValue = "NULL_VALUE";
}
return $resultValue;
} /**
* 预处理sql,替换 #{key..} 部分为实际值
* 本方法不做特殊字符处理,如遇特殊字符,请自行在传递的值中做处理
* Enter description here ...
* @param $sql 原始sql
* @param $parameter 传递参数数组
*/
function preparedSql($sql, $parameter){
global $sqlStartTarg, $sqlEndTarg; $resultSql = ""; //为避免参数值包含 #{...} 部分,所以做分组处理
$lastSql = $sql; //首先,查询 #{
while($startIndex = strpos($lastSql, $sqlStartTarg)){//包含参数
//参数前部分
$frontPart = substr($lastSql, 0, $startIndex); //参数部分
$endIndex = strpos($lastSql, $sqlEndTarg, $startIndex); if($endIndex){
//已处理部分
$valueLength = $endIndex - $startIndex + 1;
$valuePart = substr($lastSql, $startIndex, $valueLength); //#{key} 部分
$value = replaceValue($valuePart, $parameter); $resultSql .= $frontPart . $value; //参数后未处理部分
if($endIndex + 1 >= strlen($lastSql)){
$lastSql = "";
}else{
$lastSql = substr($lastSql, $endIndex + 1);
}
}else{
$resultSql .= $lastSql;
$lastSql = "";
}
} if($lastSql != ""){
$resultSql.= $lastSql;
} return $resultSql;
} /**
* 正则匹配方式不可取
* 有可能参数值中包含 #{**},此时,替换会出现异常
* 但此方法保留
* Enter description here ...
*/
function preparedSql1(){
$sql = "select user_id, user_name, `password` from `user_info` where password = '#{password}' ";
$sql .= "and company_name = '#{company_name}' and status = #{status} and comment = '#{comment}' "; //先找出 sql 中包含的 所有参数
/*
$matchArray = array(); $reg = "/\#\{[^\}]*\}/"; preg_match_all($reg, $sql, $matchArray); $resultJson = json_encode($matchArray);
//phpAlert($resultJson); print_r($matchArray);
echo "$resultJson";
*/
} /**
* 测试
* Enter description here ...
*/
function test_db(){
$sql = "select user_id, user_name, `password` from `user_info` where password = '#{password}' ";
$sql .= "and company_name = '#{company_name}' and status = #{status} and comment = '#{comment}' "; $valueArray = array();
$valueArray['password'] = "|#{password}|";
$valueArray['company_name'] = "|company_name|";
$valueArray['comment'] = "|comment|";
$valueArray['status'] = "|status|"; $sql = preparedSql($sql, $valueArray); print_r($sql);
} ?>

php sql 类似 mybatis 传参的更多相关文章

  1. 问题小记(MyBatis传参出现的小问题)

    问题一:在MyBatis中注解@Param和ParameterType不能一起用,会报错Parameter 'XXX' not found. Available parameters are [1, ...

  2. sqlmap动态sql优化,避免传参失误批量修改和删除操作!

    分析以下的sqlmap存在问题: <delete id="deletePartspic" parameterClass="TblSpPartspic"&g ...

  3. Mybatis传参方式

    Mybatis传多个参数(三种解决方案) 据我目前接触到的传多个参数的方案有三种. 第一种方案  DAO层的函数方法 ? 1 Public User selectUser(String name,St ...

  4. mybatis传参的几种方式

    1,@Param @参考文章 @Select("select s_id id,s_name name,class_id classid from student where  s_name= ...

  5. mybatis传参总结

    注:文章引用部分 mybatis传递参数总结文章内容 一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); <sele ...

  6. mybatis传参问题总结

    一. 传入单个参数 当传入的是单个参数时,方法中的参数名和sql语句中参数名一致即可 List<User> getUser(int id); <select id="get ...

  7. Java Mybatis 传参方式

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  8. mybatis 传参为 Integer 时 ,Mapper 文件 中判断 条件 问题。

    <if test="valiStatus==null || valiStatus=='' || valiStatus==4 "> b.work_permit_card_ ...

  9. mybatis 传参是 list<string> 的注意事项

    <!--付款 批量 修改账单状态--><update id="editbillpayALL" parameterType="java.util.List ...

随机推荐

  1. python数据库-MySQL数据库的增删改查基本操作(49)

    一.数据库基础 表 table:数据是保存在表内,保存在一个表内的数据,应该具有相同的数据格式 行:行用于记录数据 记录:行内的数据 列:列用于规定数据格式 字段:数据的某个列 主键:唯一地标识表中的 ...

  2. 个人亲测,在win10系统下安装多实例mysql8.0详细教程

    由于公司的新项目需要导入sql脚本,需要更高版本的mysql数据库,原来的数据库我也不想删除和升级,因此安装了第二个mysql8的实例,废话不多说,步骤如下: 1.下载mysqlGPL版本,我下载的版 ...

  3. 9.秋招复习简单整理之Spring面试AOP和IOC的理解

    1.Spring的AOP理解: OOP面向对象,允许开发者定义纵向的关系,但不适用于定义横向的关系,导致了大量代码的重复,而不利于各个模块的重用. AOP,一般称为面向切面,作为面向对象的一种补充,用 ...

  4. Bzoj 2288 生日礼物题解

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 856  Solved: 260[Submit][S ...

  5. ~~核心编程(四):面向对象——单继承&复写~~

    进击のpython 继承 继承的出现!就是一个作用! 为了节省代码! 为什么说节省代码? 你爸爸要是王健林 你是不是就不用愁怎么获得钱了? 为啥? 是不是钱已经有了! 直接拿来用就行了! 我们想一下奥 ...

  6. ElementUI 源码简析——源码结构篇

    ElementUI 作为当前运用的最广的 Vue PC 端组件库,很多 Vue 组件库的架构都是参照 ElementUI 做的.作为一个有梦想的前端(咸鱼),当然需要好好学习一番这套比较成熟的架构. ...

  7. MySQL5.7.20源码安装以及pt-query-digest用法示例

    MySQL5.7.20源码安装1.下载解压cd /data/app/mysql5.7wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5 ...

  8. .md 文件格式

    # .md 文件怎么编写 > 整理一套常用操作,自己来使用 > ## 标题 >> 写法: \# 这是一个一级标题 \## 这是一个二级标题 \### 这是一个三级标题 \### ...

  9. [leetcode] 111.Mininum Depth of Binary Tree (Easy)

    原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...

  10. Python连载25-函数tell&write&writeline$&持久化

    一. 1.连续打印举例 #打开文件,三个字符一组读出来内容,然后显示在屏幕上,每读一次,停一秒 import time with open(r"test01.txt",'r') a ...