SQL修复最终验证.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 学生信息管理SQL修复最终验证脚本
  5. 测试修复后的XML映射文件是否能正确生成SQL
  6. """
  7. import requests
  8. import json
  9. import time
  10. from datetime import datetime
  11. # 配置
  12. BASE_URL = "http://localhost:8080"
  13. LOGIN_URL = f"{BASE_URL}/auth/login"
  14. STUDENT_LIST_URL = f"{BASE_URL}/xg-xx/studentInfo/list"
  15. # 测试数据
  16. LOGIN_DATA = {
  17. "clientId": "e5cd7e4891bf95d1d19206ce24a7b32e",
  18. "username": "admin",
  19. "password": "admin123",
  20. "grant_type": "password",
  21. "tenantId": "000000"
  22. }
  23. def test_server_connection():
  24. """测试服务器连接"""
  25. print("🔍 测试服务器连接...")
  26. try:
  27. response = requests.get(f"{BASE_URL}/health", timeout=5)
  28. if response.status_code == 200:
  29. print("✅ 服务器连接正常")
  30. return True
  31. else:
  32. print(f"❌ 服务器响应异常: {response.status_code}")
  33. return False
  34. except Exception as e:
  35. print(f"❌ 服务器连接失败: {e}")
  36. return False
  37. def get_auth_token():
  38. """获取认证Token"""
  39. print("🔐 获取认证Token...")
  40. try:
  41. response = requests.post(LOGIN_URL, json=LOGIN_DATA, timeout=10)
  42. if response.status_code == 200:
  43. result = response.json()
  44. if result.get('code') == 200:
  45. token = result['data']['access_token']
  46. print("✅ Token获取成功")
  47. return token
  48. else:
  49. print(f"❌ 登录失败: {result.get('msg', '未知错误')}")
  50. return None
  51. else:
  52. print(f"❌ 登录请求失败: {response.status_code}")
  53. return None
  54. except Exception as e:
  55. print(f"❌ 登录异常: {e}")
  56. return None
  57. def test_student_list_without_params(token):
  58. """测试不带参数的学生列表查询"""
  59. print("📋 测试不带参数的学生列表查询...")
  60. headers = {
  61. 'Authorization': f'Bearer {token}',
  62. 'Content-Type': 'application/json',
  63. 'clientid': 'e5cd7e4891bf95d1d19206ce24a7b32e',
  64. 'referer': 'http://localhost',
  65. 'tenantId': '000000'
  66. }
  67. params = {
  68. 'pageNum': 1,
  69. 'pageSize': 10
  70. }
  71. try:
  72. response = requests.get(STUDENT_LIST_URL, params=params, headers=headers, timeout=10)
  73. print(f"响应状态码: {response.status_code}")
  74. if response.status_code == 200:
  75. result = response.json()
  76. if result.get('code') == 200:
  77. print("✅ 基础查询成功")
  78. data = result.get('data', {})
  79. total = data.get('total', 0)
  80. rows = data.get('rows', [])
  81. print(f"📊 查询结果: 总数={total}, 当前页记录数={len(rows)}")
  82. return True
  83. else:
  84. print(f"❌ 查询失败: {result.get('msg', '未知错误')}")
  85. return False
  86. else:
  87. print(f"❌ 请求失败: {response.status_code}")
  88. print(f"响应内容: {response.text}")
  89. return False
  90. except Exception as e:
  91. print(f"❌ 查询异常: {e}")
  92. return False
  93. def test_student_list_with_gender_filter(token):
  94. """测试带性别过滤的学生列表查询(这是之前报错的场景)"""
  95. print("🔍 测试带性别过滤的学生列表查询...")
  96. headers = {
  97. 'Authorization': f'Bearer {token}',
  98. 'Content-Type': 'application/json',
  99. 'clientid': 'e5cd7e4891bf95d1d19206ce24a7b32e',
  100. 'referer': 'http://localhost',
  101. 'tenantId': '000000'
  102. }
  103. params = {
  104. 'pageNum': 1,
  105. 'pageSize': 10,
  106. 'gender': '1' # 这是之前导致SQL重复WHERE的参数
  107. }
  108. try:
  109. response = requests.get(STUDENT_LIST_URL, params=params, headers=headers, timeout=10)
  110. print(f"响应状态码: {response.status_code}")
  111. if response.status_code == 200:
  112. result = response.json()
  113. if result.get('code') == 200:
  114. print("✅ 性别过滤查询成功 - SQL重复WHERE问题已修复!")
  115. data = result.get('data', {})
  116. total = data.get('total', 0)
  117. rows = data.get('rows', [])
  118. print(f"📊 查询结果: 总数={total}, 当前页记录数={len(rows)}")
  119. return True
  120. else:
  121. print(f"❌ 查询失败: {result.get('msg', '未知错误')}")
  122. return False
  123. else:
  124. print(f"❌ 请求失败: {response.status_code}")
  125. print(f"响应内容: {response.text}")
  126. return False
  127. except Exception as e:
  128. print(f"❌ 查询异常: {e}")
  129. return False
  130. def test_student_list_with_multiple_filters(token):
  131. """测试多条件过滤查询"""
  132. print("🔍 测试多条件过滤查询...")
  133. headers = {
  134. 'Authorization': f'Bearer {token}',
  135. 'Content-Type': 'application/json',
  136. 'clientid': 'e5cd7e4891bf95d1d19206ce24a7b32e',
  137. 'referer': 'http://localhost',
  138. 'tenantId': '000000'
  139. }
  140. params = {
  141. 'pageNum': 1,
  142. 'pageSize': 10,
  143. 'gender': '1',
  144. 'studentStatus': '1',
  145. 'name': '张'
  146. }
  147. try:
  148. response = requests.get(STUDENT_LIST_URL, params=params, headers=headers, timeout=10)
  149. print(f"响应状态码: {response.status_code}")
  150. if response.status_code == 200:
  151. result = response.json()
  152. if result.get('code') == 200:
  153. print("✅ 多条件过滤查询成功")
  154. data = result.get('data', {})
  155. total = data.get('total', 0)
  156. rows = data.get('rows', [])
  157. print(f"📊 查询结果: 总数={total}, 当前页记录数={len(rows)}")
  158. return True
  159. else:
  160. print(f"❌ 查询失败: {result.get('msg', '未知错误')}")
  161. return False
  162. else:
  163. print(f"❌ 请求失败: {response.status_code}")
  164. print(f"响应内容: {response.text}")
  165. return False
  166. except Exception as e:
  167. print(f"❌ 查询异常: {e}")
  168. return False
  169. def main():
  170. """主函数"""
  171. print("="*80)
  172. print("🎯 学生信息管理SQL修复最终验证")
  173. print("="*80)
  174. print(f"⏰ 测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  175. print()
  176. # 1. 测试服务器连接
  177. if not test_server_connection():
  178. print("❌ 服务器未启动,请先启动后端服务")
  179. return
  180. print()
  181. # 2. 获取认证Token
  182. token = get_auth_token()
  183. if not token:
  184. print("❌ 无法获取认证Token,测试中止")
  185. return
  186. print()
  187. # 3. 测试基础查询
  188. if not test_student_list_without_params(token):
  189. print("❌ 基础查询失败")
  190. return
  191. print()
  192. # 4. 测试性别过滤查询(关键测试)
  193. if not test_student_list_with_gender_filter(token):
  194. print("❌ 性别过滤查询失败 - SQL重复WHERE问题可能仍存在")
  195. return
  196. print()
  197. # 5. 测试多条件过滤查询
  198. if not test_student_list_with_multiple_filters(token):
  199. print("❌ 多条件过滤查询失败")
  200. return
  201. print()
  202. print("="*80)
  203. print("🎉 所有测试通过!SQL重复WHERE问题已完全修复!")
  204. print("="*80)
  205. print("📋 修复总结:")
  206. print(" 1. ✅ 修复了XML映射文件中的SQL语句结构")
  207. print(" 2. ✅ 使用条件判断避免重复WHERE子句")
  208. print(" 3. ✅ 验证了单条件和多条件查询都正常工作")
  209. print(" 4. ✅ 确认MyBatis-Plus的customSqlSegment正确集成")
  210. print()
  211. print("🔧 技术细节:")
  212. print(" - 修改前: WHERE deleted = 0 AND tenant_id = '000000' ${ew.customSqlSegment}")
  213. print(" - 修改后: WHERE deleted = 0 AND tenant_id = '000000'")
  214. print(" <if test=\"ew != null and ew.sqlSegment != null\">")
  215. print(" AND ${ew.sqlSegment}")
  216. print(" </if>")
  217. print()
  218. if __name__ == "__main__":
  219. main()