代码拉取完成,页面将自动刷新
同步操作将从 freakchicken/dbapi-spring-boot-starter 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<dependency>
<groupId>com.gitee.freakchicken</groupId>
<artifactId>dbApi-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.gitee.freakchicken</groupId>
<artifactId>dbApi-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
<!--需要引入数据库的jdbc驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<datasource>
<ds id="mysql">
<!--注意xml下&符号要写成&-->
<url>jdbc:mysql://localhost:3306/story?useSSL=false&characterEncoding=UTF-8</url>
<username>root</username>
<password>root</password>
</ds>
<ds id="local_mysql">
<url>jdbc:mysql://localhost:3306/story?useSSL=false&characterEncoding=UTF-8</url>
<username>root</username>
<password>root</password>
</ds>
</datasource>
<sql>
<select id="getUser" db="mysql">
select * from user
<where>
<if test = "id != null">
id <= #{id}
</if>
</where>
</select>
<select id="getUserIn" db="local_mysql">
select * from user where id in
<foreach collection="ids" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
</sql>
dbapi.config.datasource=classpath:ds.xml
dbapi.config.sql=classpath:sql.xml
@RestController
public class HomeController {
@Autowired
DBApi dbApi;
@RequestMapping("/hello")
public ResponseDto hello(@RequestBody Map<String,Object> map) {
// 第一个参数是sql执行的所有参数,一定要封装成Map<String,Object>类型
// 第二个参数是上一步sql.xml中sql脚本对应的id
ResponseDto execute = dbApi.execute(map, "getUser");
return execute;
}
@RequestMapping("/getUserIn")
public ResponseDto getUserIn(@RequestBody Map<String,Object> map) {
// 第一个参数是sql执行的所有参数,一定要封装成Map<String,Object>类型
// 第二个参数是上一步sql.xml中sql脚本对应的id
ResponseDto execute = dbApi.execute(map, "getUserIn");
return execute;
}
}
import json
import requests
data = {"ids": [1, 2, 3, 4, 5, 6]}
re = requests.post("http://localhost:8888/getUserIn", json.dumps(data), headers={"Content-Type": "application/json"})
print(re.text)
#执行结果:
#{"msg":"dbApi接口访问成功","data":[{"name":"tom","id":1,"age":12,"height":1.83},{"name":"lily","id":2,"age":45,"height":1.75},{"name":"sss","id":5,"age":2,"height":1.65},{"name":"sss","id":6,"age":34,"height":1.84}],"success":true}
import json
import requests
data = {"id":3}
re = requests.post("http://localhost:8888/hello", json.dumps(data),headers={"Content-Type": "application/json"})print(re.text)
#执行结果:
#{"msg":"dbApi接口访问成功","data":[{"name":"tom","id":1,"age":12,"height":1.83},{"name":"lily","id":2,"age":45,"height":1.75}],"success":true}
支持多数据源,使用ds标签来指定, ds标签有个id属性,值是任意字符串,这个id必须全局唯一,sql配置的时候会指定db属性,也就是指向这个id
<datasource>
<ds id="">
<url></url>
<username></username>
<password></password>
</ds>
</datasource>
<sql>
<defaultDB></defaultDB>
<select id="" db="">
</select>
<update id="" db="">
</update>
<insert id="" db="">
</insert>
<delete id="" db="">
</delete>
</sql>
如果您喜欢此项目,请给捐助作者一杯咖啡
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。