Skip to main content

A python ORM like mybatis.

Project description

mybatis-py

Latest Version codecov

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.pytest2.xml

Create a mapper directory, and create a file named mapper/test.xml, as follows:

<?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="testBasic1">
        SELECT * from fruits where id=#{id}
    </select>
</mapper>

Create a Python file named "test.py" as follows:

from mybatis import *
import mysql.connector

def main():
    conn = mysql.connector.connect(
        host="localhost",
        user="mybatis",
        password="mybatis",
        database="mybatis"
    )

    mb = Mybatis(conn, "mapper", cache_memory_limit=50*1024*1024)

    ret = mb.select_one("testBasic1", {'id':1})

    print(ret)

if __name__ == "__main__":
    main()

Dynamic SQL

The difference between ${} and #{}

#{} is a placeholder that exists for prepared statement and will become the character '?' after processing by MapperManager. ${} represents simple string replacement. The following example illustrates the difference:

from mybatis import *

mm = MapperManager()

'''
The contents of test.xml are as follows:

<?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)

The result is as follows:

SELECT * from fruits_20241204 where id=? [1]

You can see that ${date} is replaced with "20241204", and #{id} is replaced with '?', and only one parameter value in param_list is 1.

Based on security considerations, in order to prevent SQL injection, it is recommended not to use ${} as long as #{} can be used, unless you are confident enough.

Cache

mybatis-py maintains a cache pool for each connection. The elimination strategy is LRU. You can define the maximum byte capacity of the pool. If you do not want to use cache, you can set the parameter configuration. The code is as follows:

def main():
    # conn1 = mysql.connector.connect(
    #     host="localhost",
    #     user="mybatis",
    #     password="mybatis",
    #     database="mybatis"
    # )

    # conn2 = mysql.connector.connect(
    #     host="localhost",
    #     user="mybatis",
    #     password="mybatis",
    #     database="mybatis"
    # )

    mb1 = Mybatis(conn1, "mapper", cache_memory_limit=50*1024*1024) # Capacity limit is 50MB
    mb2 = Mybatis(conn2, "mapper", cache_memory_limit=None) # Disable caching

Timeout mechanism

In order to prevent users from always getting old data, the cache will determine whether the key-value has expired when fetching a key-value. The maximum life milliseconds of the key-value can be specified through the cache_max_live_ms parameter in the constructor of the Mybatis class.

Project details


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

If you're not sure about the file name format, learn more about wheel file names.

mybatis-0.0.8-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file mybatis-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: mybatis-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.8.20

File hashes

Hashes for mybatis-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 49cef59b6f3c838a8d3219007ca091ed66a5e2b4dfbbc46467fbcab71af4f4c3
MD5 5622cc4d26e07c7f812ba4c3527140e7
BLAKE2b-256 ee33d23bf2aa6d9782610d577f698fd7d3c373c390a4e9a1adb041428b2b2f2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page