springboot集成mybatis,使用xml的方式调用Oracle数据库存储过程;
mybatis框架使用xml文件的方式调用Oracle数据库存储过程; postman工具使用;
·
目录
项目目录结构如图所示

配置项目
mybatis:
configuration:
# 在控制台打印SQL执行的语句 print sql in console
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 让mybatis找到在mapper目录下的xml文件 make mybatis find xml file in mapper folder
mapper-locations: classpath:mapper/*.xml
# 实体参数或返回值可以省略包名 entity param or return type can be abbreviated package names
type-aliases-package: com.jhzy.financeReport.entity
准备实体类
public class CanteenSheet {
/**
* 食堂资产负债表
*/
private String GSDM;
private String gsmc;
private String ZTH;
private String KJND;
private String KJQJ;
// 为了节省篇幅,以下省略了若干属性...
}
编写CanteenMapper.xml文件
<?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 namespace="com.jhzy.proName.dao.CanteenDao">
<resultMap id="canteenSheet" type="com.jhzy.proName.entity.CanteenSheet">
<!--
这里和注解差不多,
当实体类属性名和数据表字段名不一样时,这样写让个别几个不一样的实体类属性和数据表字段相互对应;
若两者已经完全对应了,则不需要写 result 去对应了,让其为空就好;若有不对应的,写法如下;
-->
<!-- <result column="GSDM" property="GSDM"/>-->
</resultMap>
<!-- 这里注意要声明parameterType的类型为Map; -->
<select id="callCanteenSheet" statementType="CALLABLE" parameterType="java.util.Map">
call zxzcfzb_report(#{gsdm_in}, #{zth_in}, #{kjnd_in}, #{kjqj_in},
#{ResCanteen,mode=OUT,jdbcType=CURSOR,resultMap=canteenSheet})
--resultMap的值canteenSheet和上面resultMap标签的id对应;
--返回结果放在ResCanteen里面,最后当作map的键来获取;
</select>
</mapper>
dao文件
@Mapper
public interface CanteenDao {
void callCanteenSheet(Map map);
}
service文件
@Service
public class CanteenService {
@Resource
private CanteenDao canteenDao;
public void listCanteen (Map map) {
/*
获取前端参数
*/
String gsdm = map.get("gsdm").toString();
String zth = map.get("zth").toString();
String kjnd = map.get("kjnd").toString();
String kjqj = map.get("kjqj").toString();
map.put("gsdm_in", gsdm);
map.put("zth_in", zth);
map.put("kjnd_in", kjnd);
map.put("kjqj_in", kjqj);
canteenDao.callCanteenSheet(map);
}
}
controller文件
@RestController
@Slf4j // lombok的注解,调试时使用;使用log.info();可以在控制台输出测试内容;
public class CanteenController {
@Resource
CanteenService canteenService;
// http://localhost/canteen?gsdm=01001002&zth=002&kjnd=2023&kjqj=10 // 餐厅资产负债
// { "gsdm": "01001002", "zth": "002", "kjnd": "2023", "kjqj": "9" }
@PostMapping("/canteen")
@CrossOrigin
public List<CanteenSheet> canteenSheets(@RequestBody Map map){
canteenService.listCanteen(map);
log.info("canteen: {}",map.get("ResCanteen"));
List<CanteenSheet> columns = (List) map.get("ResCanteen");
return columns;
}
由于是Post请求,不方便使用浏览器测试,可以使用postman工具进行测试;
测试结果如下所示:

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)