造个轮子之实现python-mybatis和SqlTemplate
py_mybatis
- python的mybatis实现
- python的SqlTemplate实现
项目起源
特别鸣谢:mybatis-mapper2sql
借助这位大佬使我快速实现了py_mybatis,mapper xml解析主体代码都是 hhyo大佬的
本人是位javaer,刚刚开始学习python,当开始学习数据库相关api时,发现python中并未有很好的开发包,
重型orm 直接淘汰,不要问为什么,问就是我喜欢写sql,虽然sql写的不咋的
急需一种轻型的,开箱即用的类似mybatis的开发包,找了半天也没有,算了,不如自己造轮子,这个念头一直在我心中萦绕
无奈,python刚学没多久,稍微有些难度,时间也不太充足,直到遇到了mybatis-mapper2sql,才成为可能,感谢大佬
特别提醒
暂未发布开发包,只能下载使用
私以为未经过测试就发布,容易挨骂,所以等等看,看看反馈吧
mybatis 语法支持
标签支持
-
sql,select,update,insert,delete
-
include,if,choose,when,otherwise
-
trim,where,set,foreach,bind
动态语法支持
- #{},${}
- 新增 $f{}函数语法 ,允许开发者自行注册函数,参见:mapper_func.py
- 废弃ognl语法 改为 python语法
- 参数转换(核心代码在type_handler.py)
sql 参数支持
参数 目前只支持 dict 类型 名称为params
结果映射
暂不支持,查询结果参见pymysql,pymysql.cursors.DictCursor
数据库支持
- mysql 理论上可支持所有sql类型数据库
使用示例
基本示例
测试表结构
DROP TABLE IF EXISTS `fruits`;
CREATE TABLE `fruits` (
`id` bigint(20) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`category` varchar(20) DEFAULT NULL,
`price` double DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`type` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into `fruits`(`id`,`name`,`category`,`price`,`create_time`,`type`) values
(1,'苹果','apple',5,'2020-06-27 11:54:38',0),
(2,'梨','pear',4,'2020-06-28 11:54:38',1),
(3,'香蕉','banana',3,'2020-06-30 11:55:19',2);
PdbcSqlTemplate(sql模板用法)
参见:tests/sql_template_test.py
from py_mybatis.sql.pdbc_sql_template import *
import pymysql
import unittest
def query_function(connection):
cursor = connection.cursor()
try:
cursor.execute('select * from fruits')
data = cursor.fetchall()
connection.commit()
return data
finally:
cursor.close()
class PyMybatisTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.sql_template = PdbcSqlTemplate(dataSource=PooledDB(
creator=pymysql,
maxconnections=6,
mincached=2,
maxcached=5,
blocking=True,
maxusage=None,
setsession=[],
ping=0,
host="localhost",
user="root",
password="root",
database="api_user",
cursorclass=pymysql.cursors.DictCursor,
charset='utf8'
))
def test_select_list(self):
print("============{}============".format('select_with row_bound'))
print(
self.sql_template.select_list(sql="select * from fruits where id in (%s,%s,%s)",
row_bound=RowBound(1, 2), args=(1, 2, 3))
)
print("============{}============".format('select_only'))
print(
self.sql_template.select_list(sql="select * from fruits where id in (%s,%s,%s)", args=(1, 2, 3))
)
def test_select_one(self):
print("============{}============".format('test_select_one'))
print(
self.sql_template.select_one(sql="select * from fruits where id=%s", args=(1))
)
def test_select_with_no_params(self):
print("============{}============".format('test_select_with_no_params'))
print(
self.sql_template.select_one(sql="select * from fruits where id=1")
)
def test_select_page(self):
print("============{}============".format('test_select_page'))
print(
self.sql_template.select_page(sql="select * from fruits", row_bound=RowBound(1, 2))
)
def test_delete(self):
print("============{}============".format('test_delete'))
row = self.sql_template.delete(
sql="delete from fruits where id in(%s,%s,%s)",
args=(10, 11, 9))
print(row)
def test_insert_one(self):
print("============{}============".format('test_insert_one'))
row = self.sql_template.insert(sql="insert into fruits(id,name,price,category) values(%s,%s,%s,%s)",
args=(10, '菠萝', 10, 'boluo'))
print(row)
def test_insert_mul(self):
print("============{}============".format('test_insert_mul'))
row = self.sql_template.insert_batch(
sql="insert into fruits(id,name,price,category) values(%s,%s,%s,%s)",
args=((11,