Skip to content

Commit d7ee95c

Browse files
authored
Update README.md
1 parent d035ac4 commit d7ee95c

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

README.md

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A python ORM like mybatis.
1111
pip install -U mybatis
1212

1313
### Create Database
14+
#### mysql
1415
```sql
1516
CREATE DATABASE mybatis;
1617

@@ -20,10 +21,22 @@ CREATE TABLE IF NOT EXISTS fruits (
2021
id INT AUTO_INCREMENT PRIMARY KEY,
2122
name VARCHAR(100),
2223
category VARCHAR(100),
23-
price int)
24+
price int);
2425

25-
INSERT INTO fruits (name, category, price) VALUES ('Alice', 'A', 100)
26-
INSERT INTO fruits (name, category, price) VALUES ('Bob', 'B', 200)
26+
INSERT INTO fruits (name, category, price) VALUES ('Alice', 'A', 100);
27+
INSERT INTO fruits (name, category, price) VALUES ('Bob', 'B', 200);
28+
```
29+
#### postgresql
30+
```sql
31+
CREATE TABLE if not exists fruits (
32+
id SERIAL PRIMARY KEY,
33+
name VARCHAR(100),
34+
category VARCHAR(100),
35+
price int);
36+
37+
INSERT INTO fruits (name, category, price) VALUES ('Alice', 'A', 100);
38+
39+
INSERT INTO fruits (name, category, price) VALUES ('Bob', 'B', 200);
2740
```
2841

2942
### Write Code
@@ -35,6 +48,9 @@ Create a mapper directory, and create a file named mapper/test.xml, as follows:
3548
<?xml version="1.0" encoding="UTF-8"?>
3649
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3750
<mapper>
51+
<insert id="testInsert1", primary_key="id">
52+
INSERT INTO fruits (name, category, price) VALUES (#{name}, #{category}, #{price})
53+
</insert>
3854
<select id="testBasic1">
3955
SELECT * from fruits where id=#{id}
4056
</select>
@@ -43,18 +59,18 @@ Create a mapper directory, and create a file named mapper/test.xml, as follows:
4359
Create a Python file named "test.py" as follows:
4460
```python
4561
from mybatis import *
46-
import mysql.connector
4762

4863
def main():
49-
conn = mysql.connector.connect(
50-
host="localhost",
51-
user="mybatis",
52-
password="mybatis",
53-
database="mybatis"
54-
)
64+
conn = ConnectionFactory.get_connection(
65+
dbms_name='mysql', # change to 'postgresql' if you are using PostgreSQL
66+
host="localhost",
67+
user="mybatis",
68+
password="mybatis",
69+
database="mybatis")
5570

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

73+
ret = mb.insert('testInsert1', {'name':'Alice', 'category':'C', 'price':500}
5874
ret = mb.select_one("testBasic1", {'id':1})
5975

6076
print(ret)
@@ -66,15 +82,14 @@ if __name__ == "__main__":
6682
## Decorator
6783
The example is as follows:
6884
```python
69-
import mysql.connector
70-
from mybatis import Mybatis
85+
from mybatis import Mybatis, ConnectionFactory
7186

72-
conn = mysql.connector.connect(
87+
conn = ConnectionFactory.get_connection(
88+
dbms_name='mysql', # change to 'postgresql' if you are using PostgreSQL
7389
host="localhost",
7490
user="mybatis",
7591
password="mybatis",
76-
database="mybatis"
77-
)
92+
database="mybatis")
7893

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

@@ -86,7 +101,7 @@ def get_one(id:int):
86101
def get_many():
87102
pass
88103

89-
@mb.Insert("INSERT INTO fruits (name, category, price) VALUES (#{name}, #{category}, #{price})")
104+
@mb.Insert("INSERT INTO fruits (name, category, price) VALUES (#{name}, #{category}, #{price})", primary_key="id")
90105
def insert(name:str, category:str, price:int):
91106
pass
92107

@@ -123,6 +138,9 @@ Create xml mapper as follows:
123138
<mapper namespace="test_namespace">
124139
<insert id="testInsert">
125140
INSERT INTO fruits (name, category, price) VALUES (#{name},#{category},#{price})
141+
<if test="'__need_returning_id__' in params">
142+
RETURNING id
143+
</if>
126144
</insert>
127145
<delete id="testDelete">
128146
DELETE FROM fruits WHERE id=#{id}
@@ -179,22 +197,21 @@ Based on security considerations, in order to prevent SQL injection, it is recom
179197
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:
180198
```python
181199
from mybatis import *
182-
import mysql.connector
183200

184201
def main():
185-
conn1 = mysql.connector.connect(
186-
host="localhost",
187-
user="mybatis",
188-
password="mybatis",
189-
database="mybatis"
190-
)
191-
192-
conn2 = mysql.connector.connect(
193-
host="localhost",
194-
user="mybatis",
195-
password="mybatis",
196-
database="mybatis"
197-
)
202+
conn1 = ConnectionFactory.get_connection(
203+
dbms_name='mysql', # change to 'postgresql' if you are using PostgreSQL
204+
host="localhost",
205+
user="mybatis",
206+
password="mybatis",
207+
database="mybatis")
208+
209+
conn2 = ConnectionFactory.get_connection(
210+
dbms_name='mysql', # change to 'postgresql' if you are using PostgreSQL
211+
host="localhost",
212+
user="mybatis",
213+
password="mybatis",
214+
database="mybatis")
198215

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

0 commit comments

Comments
 (0)