Skip to content

Commit ea2a511

Browse files
committed
Add example in README.md
1 parent 9c4d3ca commit ea2a511

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ INSERT INTO fruits (name, category, price) VALUES ('Bob', 'B', 200)
2929

3030
refer to [test_mybatis.py](https://github.com/ralgond/mybatis-py/blob/main/test/test_mybatis.py)[test2.xml](https://github.com/ralgond/mybatis-py/blob/main/mapper/test.xml)
3131

32+
创建一个mapper目录,创建一个文件mapper/test.xml,如下:
33+
```xml
34+
<?xml version="1.0" encoding="UTF-8"?>
35+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
36+
<mapper>
37+
<select id="testBasic1">
38+
SELECT * from fruits where id=#{id}
39+
</select>
40+
</mapper>
41+
```
42+
编写python文件test.py, 如下:
43+
```python
44+
from mybatis import *
45+
import mysql.connector
46+
47+
def main():
48+
# 连接到 MySQL 数据库
49+
conn = mysql.connector.connect(
50+
host="localhost", # MySQL 主机地址
51+
user="mybatis", # MySQL 用户名
52+
password="mybatis", # MySQL 密码
53+
database="mybatis" # 需要连接的数据库
54+
)
55+
56+
mb = Mybatis(conn, "mapper", cache_memory_limit=50*1024*1024)
57+
58+
ret = mb.select_one("testBasic1", {'id':1})
59+
60+
print(ret)
61+
62+
if __name__ == "__main__":
63+
main()
64+
```
65+
3266
## Dynamic SQL
3367
### ${}和#{}的区别
3468
#{}是一个占位符,为prepared statement而存在,在MapperManager处理后会变成字符'?';

example2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from mybatis import *
2+
import mysql.connector
3+
4+
def main():
5+
# 连接到 MySQL 数据库
6+
conn = mysql.connector.connect(
7+
host="localhost", # MySQL 主机地址
8+
user="mybatis", # MySQL 用户名
9+
password="mybatis", # MySQL 密码
10+
database="mybatis" # 需要连接的数据库
11+
)
12+
13+
mb = Mybatis(conn, "mapper", cache_memory_limit=50*1024*1024)
14+
15+
ret = mb.select_one("testBasic1", {'id':1})
16+
17+
print(ret)
18+
19+
if __name__ == "__main__":
20+
main()

mapper/test.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,8 @@
151151
<select id="testStringReplace">
152152
SELECT * from fruits_${date} where id=#{id}
153153
</select>
154+
155+
<select id="testBasic1">
156+
SELECT * from fruits where id=#{id}
157+
</select>
154158
</mapper>

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='mybatis',
5-
version='0.0.3',
5+
version='0.0.4',
66
description='A python ORM like mybatis.',
77
long_description=open('README.md').read(),
88
long_description_content_type='text/markdown', # 如果你使用的是Markdown格式的README
@@ -16,7 +16,8 @@
1616
],
1717
classifiers=[
1818
'Programming Language :: Python :: 3',
19-
'License :: OSI Approved :: Apache License',
19+
# 'License :: OSI Approved :: Apache-2.0 License',
20+
'License :: OSI Approved :: Apache Software License',
2021
'Operating System :: OS Independent',
2122
],
2223
python_requires='>=3.8',

0 commit comments

Comments
 (0)