用过java开源项目mybatis(ibatis)后,觉得它的思路挺好,于是做了一个风格类似于batis的组件。
将SQL从MVC中的模型中分离出来,一方面便于后期SQL的优化,也让PHP不再夹杂着SQL,让PHPer更专注于业务逻辑。
下面是demo
一、主配置文件config.xml(名字任意)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 全局别名 -->
<typeAliases>
<typeAlias key="DAOPATH" value="application/"/>
</typeAliases>
<!-- 数据源 -->
<databases>
<database id="sysvotes" default="true">
<property name="driver" value="pdo"/>
<property name="charset" value="UTF8"/>
<property name="dsn" value="mysql:host=localhost;dbname=test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</database>
<database id="syshotels">
<property name="driver" value="pdo"/>
<property name="dsn" value="mysql:host=192.168.1.251;dbname=syshotels"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</database>
</databases>
<caches>
<cache id="memcache" default="true">
<property name="type" value="memcache"/>
<property name="host" value="localhost"/>
<property name="port" value="11211"/>
</cache>
<cache id="file">
<property name="type" value="file"/>
<property name="filepath" value="Cache/Temp/"/>
</cache>
</caches>
</configuration>
二、mapper文件log.xml
<?xml version="1.0" encoding="utf-8" ?>
<mapper>
<sql id="allFields">`TIMESTAMP`, logger, `LEVEL`, message, thread, `FILE`, line</sql>
<!--
id:作为Batis引用的唯一标识
resultClass:返回数据的封装为Class对象。若没有此参数,将不会对结果封装为对象
cacheId:缓存配置标签ID。目前支持:file、memcache
cacheTime:缓存超时时间,单位秒
cahce:是否缓存,默认false
prepare:是否使用PDO的prepare,默认true
-->
<select id="SelectAll" resultClass="Log" cacheId="file" cacheTime="3600" cache="true" prepare="true">
SELECT
#allFields#
FROM
log2
WHERE
thread > :number
</select>
<!--
对于复杂的搜索条件,phpBatis不支持if-else或trim这样的标签。
解决方法:关闭使用PDO的prepare绑定,将搜索条件在PHP中处理好,然后传给phpBatis直接替换。
-->
<select id="getLogs" cache="false" prepare="false">
SELECT
#allFields#
FROM
log2
WHERE
:terms
</select>
<insert id="insertlog">
insert into log2
(#allFields#)
values
(:timestamp, :logger, :level, :message, :thread, :file, :line)
</insert>
<update id="Update" parameterClass="Person">
update log2 set
logger = :logger,
message = :message,
thread = :thread,
line = :line
where `timestamp`=:timestamp
</update>
<delete id="Delete" parameterClass="int">
delete from PERSON
where PER_ID = #value#
</delete>
</mapper>
三、调用phpBatis的测试文件test.php
<?php
require 'phpBatis.php';
$batis = new PhpBatis('application/config.xml');
/**
* 事务
*/
$time = time();
$params = array(
'timestamp'=>$time,
'logger'=>'Mr.jinyong',
'level' =>'WARN',
'message'=>'test',
'thread' =>'5501',
'file' => __FILE__,
'line' =>50
);
$batis->beginTransaction();
try{
$batis->setMapper("application/log.xml")->insert("insertlog", $params);
$updateParams = array(
'timestamp'=>$time,
'logger'=>'Mr.jinyong',
'message'=>'update',
'thread' =>'5501',
'line' =>50
);
$batis->setMapper("application/log.xml")->update("Update", $updateParams);
$batis->setMapper("application/log.xml")->insert("insertlog", $params);
$batis->commitTransaction();
}catch(BatisException $e){
$batis->rollBackTransaction();
}
// /**
// * 插入一条记录
// */
// $params = array(
// 'timestamp'=>time(),
// 'logger'=>'Mr.jinyong',
// 'level' =>'WARN',
// 'message'=>'test',
// 'thread' =>'5501',
// 'file' => __FILE__,
// 'line' =>50
// );
// $batis->setMapper("application/log.xml")->insert("insertlog", $params);
// /**
// * 更新一条记录
// */
// $batis->setMapper("application/log.xml")->update("Update", $params);
// /**
// * 查询一条记录,并返回Log对象
// */
// $result = $batis->setMapper("application/log.xml")->queryOne("SelectAll", array(':number'=> 0));
// echo $result->getLogger();
// /**
// * 复杂的查询条件,关闭prepare
// */
// $result = $batis->setMapper("application/log.xml")->queryList("getLogs", array(':terms'=> "thread=5400 OR thread<3000"));
// print_r($result);