dMapper下载地址1:https://pan.baidu.com/s/1gj95X-ndTsSnSFL8t7oevw
数据库文件
文件路径:mapper\test.xml
<?xml version="1.0" encoding="utf-8" ?>
<mapper>
<fields>*</fields>
<select id="test" attr="fetchAll">
SELECT
#fields#
FROM
test
</select>
<select id="test1" attr="fetch">
SELECT
#fields#
FROM
test
WHERE
id = :id
</select>
<select id="test2" attr="fetchColumn">
SELECT
id
FROM
test
WHERE
id = :id
</select>
<insert id="add">
insert into test(`title`,`remarks`) #VALUES#
</insert>
<update id="upd">
update test set `title`=:title,`remarks`=:remarks where id=:id
</update>
<delete id="del">
delete
from
test
where id=:id
</delete>
</mapper>
控制器类
文件路径:app\www\controller\Test.php
<?php
/**
* Created by lizhongwen.
* User: pc
* Date: 2018/5/15
* Time: 23:39
* qq:360811363
*/
namespace app\www\controller;
use core\base\Controller;
use core\db\Mapper;
use core\Lib;
use core\Request;
class Test extends Controller
{
/**
* 使用模型查询
*/
public function index()
{
$ret = $this->model->getList();
$this->assign('ret', $ret);
$this->view();
}
/**
* 删除
*
* @param number $id
*/
public function delete($id = 0)
{
$ret = Mapper::orm('test')->map('delete')
->byId('del')
->exec([
'id' => $id
]);
if ($ret > 0) {
header('location:' . Lib::roteUrl('test', 'index'));
} else {
echo "删除失败";
}
}
/**
* 添加一条数据
*
* @param string $act
*/
public function add($act = '')
{
if ($act == 'do') {
$data = [
'title' => Request::post('title'),
'remarks' => Request::post('remarks')
];
try {
$ret = Mapper::orm('test')->map('insert')
->byId('add')
->exec($data);
header('location:' . Lib::roteUrl('test', 'index'));
} catch (\Exception $e) {
die($e->getMessage());
}
}
$this->view();
}
/**
* 添加多条数据
*
* @param string $act
*/
public function adds($act = '')
{
if ($act == 'do') {
try {
$data = [
[
'title' => Request::post('title1'),
'remarks' => Request::post('remarks1')
],
[
'title' => Request::post('title2'),
'remarks' => Request::post('remarks2')
]
];
$ret = Mapper::orm('test')->map('insert')
->byId('add')
->exec($data);
header('location:' . Lib::roteUrl('test', 'index'));
} catch (\Exception $e) {
die($e->getMessage());
}
}
$this->view();
}
/**
* 修改数据
* @param number $id
* @param string $act
*/
public function upd($id = 0, $act = '')
{
if ($act == 'do') {
$data = [
'title' => Request::post('title'),
'remarks' => Request::post('remarks'),
'id'=>$id
];
try {
$ret = Mapper::orm('test')->map('update')
->byId('upd')
->exec($data);
header('location:' . Lib::roteUrl('test', 'index'));
} catch (\Exception $e) {
die($e->getMessage());
}
}
// 修改前先查询数据
$ret = Mapper::orm('test')->map('select')
->byId('test1')
->exec([
'id' => $id
]);
$this->assign('ret', $ret);
$this->view();
}
/**
* 事物处理
*/
public function transaction()
{
try {
//开启事物
Mapper::beginTransaction();
//数据库中title字段是唯一属性,我联系插入两条一样的数据,这时候就会触发事物,则自动回滚
$data = [
'title' => "一样的标题",
'remarks' => "不一样的备注"
];
//第一次执行
$ret = Mapper::orm('test')->map('insert')
->byId('add')
->exec($data);
//第二次执行,这时候就会回滚,数据库中一条也没有,你可以尝试不要执行下面的一条插入(记得换个标题),
$ret = Mapper::orm('test')->map('insert')
->byId('add')
->exec($data);
echo "执行成功";
//提交事物
Mapper::commit();
}catch (\Exception $e){
//echo $e->getMessage();
echo "这里产生了事物回滚,原因:".$e->getMessage();
//回滚事物
Mapper::rollBack();
}
}
}
模型类
文件路径:app\www\model\Test.php
<?php
/**
* Created by lizhongwen.
* User: pc
* Date: 2018/5/15
* Time: 23:39
* qq:360811363
*/
namespace app\www\model;
use core\db\Mapper;
class Test
{
public function getList(){
$ret = Mapper::orm('test')->map('select')->byId('test')->exec();
return $ret;
}
}
dMapper下载地址1:https://pan.baidu.com/s/1gj95X-ndTsSnSFL8t7oevw
331



被折叠的 条评论
为什么被折叠?



