A python ORM like mybatis.
Project description
mybatis-py
A python ORM like mybatis.
How to Use
Install
pip install mybatis
Create Database
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE IF NOT EXISTS fruits (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
category VARCHAR(100),
price int)
INSERT INTO fruits (name, category, price) VALUES ('Alice', 'A', 100)
INSERT INTO fruits (name, category, price) VALUES ('Bob', 'B', 200)
Write Code
refer to test_mybatis.py、test2.xml
Dynamic SQL
${}和#{}的区别
#{}是一个占位符,为prepared statement而存在,在MapperManager处理后会变成字符'?'; ${}表示简单的字符串替换。下面一个例子能说明它的区别:
from mybatis import *
mm = MapperManager()
'''
test.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper>
<select id="testStringReplace">
SELECT * from fruits_${date} where id=#{id}
</select>
</mapper>
'''
mm.read_mapper_xml_file("mapper/test.xml")
sql, param_list = mm.select("testStringReplace", {'id':1, 'date':"20241204"})
print(sql, param_list)
结果是
SELECT * from fruits_20241204 where id=? [1]
可以看见${date}被替换成"20241204",而#{id}替换成了'?',同时param_list中只有一个参数值为1。
基于安全性的考虑,为了防止SQL注入,建议只要能使用#{}就不要使用${},除非你有足够的把握。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
mybatis-0.0.4-py3-none-any.whl
(13.3 kB
view details)
File details
Details for the file mybatis-0.0.4-py3-none-any.whl.
File metadata
- Download URL: mybatis-0.0.4-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cb0d88a81de6f595c477b6d7d3d8d3c0f5c350cf39663eac7e956770350fc14
|
|
| MD5 |
923feffdeeff8183d79408425aaf7312
|
|
| BLAKE2b-256 |
bb3b35b3013468a23cccdbbf31a4d50c7b4a8d260ef63dc62fd6016f7570ec26
|