phpMyBatis安全框架[dMapper],phpMVC框架,支持数据库单例模式和多例模式!!!【更新】

dMapper下载地址1:https://pan.baidu.com/s/1gj95X-ndTsSnSFL8t7oevw


数据库文件

文件路径:mapper\test.xml



	*
	
	
	


	
		insert into test(`title`,`remarks`) #VALUES#
	
	
		update test set `title`=:title,`remarks`=:remarks where id=:id
	
	
		delete
		from
		test
		where id=:id
	

控制器类

文件路径:app\www\controller\Test.php

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

map('select')->byId('test')->exec();
        return $ret;
    }
}


dMapper下载地址1:https://pan.baidu.com/s/1gj95X-ndTsSnSFL8t7oevw




你可能感兴趣的:(php开发技术)