Java Spring動(dòng)態(tài)生成Mysql存儲(chǔ)過(guò)程詳解
一、 背景
由于公司業(yè)務(wù)需要?jiǎng)討B(tài)配置一些存儲(chǔ)過(guò)程來(lái)生成數(shù)據(jù),之前嘗試過(guò)使用jpa來(lái)完成,或多或少都存在一些問(wèn)題,最后使用了spring的Jdbctemplate。
二、 環(huán)境
1.此隨筆內(nèi)容基于spring boot項(xiàng)目
2.數(shù)據(jù)庫(kù)為mysql 5.7.9版本
3.jdk 版本為1.8
三、 說(shuō)明
說(shuō)明:為方便表示,下列存儲(chǔ)過(guò)程在代碼中的表示我稱(chēng)之為接口配置
四、 內(nèi)容
1、定義接口和接口參數(shù)bean;
1)接口配置bean:
@Entity@Table(name='qt_interface')public class QtInterface { @Id private String id; private String name; private String content; private String info; private String status;//此處省略get、set…}
2)接口配置參數(shù)bean:
@Entity@Table(name='qt_interface_parameter')public class QtInterfaceParameter { @Id private String id; @Column(name='inter_id') private String interId; private String name; //參數(shù)名稱(chēng) private String explain_info; //參數(shù)描述 private String type;// 輸入輸出類(lèi)型 private String paraType; // 參數(shù)類(lèi)型 private Integer paraLen;//此處省略get、set…}
2、編寫(xiě)頁(yè)面輸入接口配置的信息;
1)Html部分代碼:
<div class='form-group'> <label for='name' class='col-sm-2 control-label'>接口名稱(chēng)<a style='color:red;'>*</a>:</label> <div class='col-sm-4'> <input type='text' name='name' /> </div> <label for='status' class='col-sm-2 control-label'>接口狀態(tài)<a style='color:red;'>*</a>:</label> <div > <select disabled='disabled' class='form-control'> <option value='0'>保存</option> <option value='1'>已創(chuàng)建</option> </select> </div></div><div class='form-group'> <label for='content' class='col-sm-2 control-label'>接口內(nèi)容<a style='color:red;'>*</a>:</label> <div class='col-sm-10'> <textarea name='content' rows='5' class='form-control'></textarea> </div></div><div class='form-group'> <label for='explain_info' class='col-sm-2 control-label'>接口說(shuō)明:</label> <div class='col-sm-10'> <textarea name='explain_info' rows='3' class='form-control'></textarea> </div></div><div class='form-group'> <label for='qtInterList' class='col-sm-2 control-label'>接口參數(shù):</label> <div class='col-sm-10'> <div style='width:100%;'> <table class='easyui-datagrid'> </table> </div> </div></div>
2)Js部分代碼太長(zhǎng),就只貼一個(gè)提交方法吧
function createProduce(inter_id) { var postData = { id: $('#inter_id').val(), item_id: $('#item_id').val(), name: $('#name').val(), content: $('#content').val(), explain_info: $('#explain_info').val(), jsonData: JSON.stringify(jsonData)// 參數(shù)明細(xì)信息,字段就是接口配置參數(shù)bean 中的字段信息}; $.ajax({ url: Url + ’test/createPro’, type: ’get’, //GET async: false, //或false,是否異步 data: JSON.stringify(postData), timeout: 5000, //超時(shí)時(shí)間 dataType: ’json’, //返回的數(shù)據(jù)格式: success: function (result, textStatus, jqXHR) { if (result.result == '1') { // 編輯賦值layer.alert('創(chuàng)建成功', {icon: 0}); } else {layer.alert('創(chuàng)建失敗,請(qǐng)檢查sql語(yǔ)句,注意結(jié)尾不能有分號(hào)!具體錯(cuò)誤信息:'+result.msg, {icon: 5}); } }, error: function (xhr, textStatus) { layer.alert(textStatus); } });}
3、將數(shù)據(jù)上傳到后臺(tái)之后,后臺(tái)生成存儲(chǔ)過(guò)程。當(dāng)然一般情況下,我們還是先把數(shù)據(jù)接口和接口明細(xì)數(shù)據(jù)持久化保存,再來(lái)執(zhí)行創(chuàng)建操作,可以保證數(shù)據(jù)不會(huì)丟失。此處由于篇幅問(wèn)題,我就省略了中間這一步。
1)創(chuàng)建一個(gè)service 的接口:
public interface TestService { ResultInfo createPro(Map<String,Object> map);}
2)然后創(chuàng)建接口的實(shí)現(xiàn)類(lèi):
@Servicepublic class TestServiceImpl implements TestService { /** * 創(chuàng)建存儲(chǔ)過(guò)程 * * @param map 接口配置和接口參數(shù)信息 * 參數(shù)詳解: type 輸入輸出參數(shù),取值為 in,out * paraType 參數(shù)類(lèi)型。取值為:1:int 2:double 3:varchar 4:datetime * @return */@Override@Transactionalpublic void createPro(Map<String,Object> map) { ResultInfo resultInfo = new ResultInfo(); QtInterface qtInterface=new QtInterface(); qtInterface =buildInterface(map, qtInterface);// 加載接口配置信息 List<QtInterfaceParameter> paraList = new ArrayList<QtInterfaceParameter>(); paraList = buildParam(map.get('jsonData'));// 加載接口配置信息 StringBuffer bf = new StringBuffer(); // 建立生成過(guò)程的語(yǔ)句 bf.append('create procedure t'); bf.append(qtInterface.getName()); bf.append('n'); bf.append('('); String para_type = ''; int i = 1; for (QtInterfaceParameter qt : paraList) { switch (qt.getParaType()) { // 參數(shù)類(lèi)型 case '1':para_type = 'int';break; case '2':para_type = 'double';break; case '3':para_type = 'varchar(' + qt.getParaLen() + ')';break; case '4':para_type = 'datetime';break; default:para_type = 'varchar(255)';break; } if (i == paraList.size()) { bf.append('' + qt.getType() + ' ' + qt.getName() + ' ' + para_type + ') '); } else { bf.append('' + qt.getType() + ' ' + qt.getName() + ' ' + para_type + ', '); } i++; } bf.append(' COMMENT ’'+ qtMonitorWarnInterface.getInfo() +'’n'); // 添加描述信息 bf.append('BEGINn'); bf.append(qtInterface.getContent()); // 存儲(chǔ)過(guò)程內(nèi)容 bf.append(';nEND;'); // 先執(zhí)行刪除操作 jdbcTemplate.execute('drop procedure if exists ' + qtInterface.getName() + ' ;'); jdbcTemplate.execute(bf.toString()); } /** * 初始化接口配置信息 * */private QtInterface buildInterface(Map<String, Object> map, QtInterface qtInterface) { // 接口配置名稱(chēng) if (map.get('name') != null && !''.equals(map.get('name '))) { qtInterface.setName((String) map.get('name ')); } //此處省略其他項(xiàng),其他項(xiàng)的取值方法跟上面的一樣 … return qtInterface;} /** * 初始化接口配置參數(shù)明細(xì) * */ private List<QtInterfaceParameter> buildParam(String postData) { List<QtInterfaceParameter> list = new ArrayList<QtInterfaceParameter>(); if(postData!=null &&!''.equals(postData)){ List<Map<String, Object>> listParam = (List<Map<String, Object>>) JsonMapper.fromJsonString(postData, ArrayList.class); for (Map<String, Object> map : listParam) {QtInterfaceParameter para = new QtInterfaceParameter();// 接口配置參數(shù)名稱(chēng)if (map.get('name') != null && !''.equals(map.get('name '))) { para.setName((String) map.get('name '));}// 此處省略其他項(xiàng),其他項(xiàng)的取值方法跟上面的一樣 …list.add(para); } } return list; }
3) 添加控制器進(jìn)行調(diào)用:
@Controller@RequestMapping(value = '/test')public class TestController {@Autowiredprivate TestService testService; @RequestMapping(value = '/createPro', method = RequestMethod.GET)public ResultInfo createPro(@RequestBody Map<String, Object> map) { ResultInfo resultInfo = new ResultInfo(); try { testService.createPro(Id); resultInfo.setResult(1); resultInfo.setMsg('創(chuàng)建過(guò)程成功'); } catch (Exception e) { resultInfo.setResult(-1); resultInfo.setMsg(e.getMessage()); } return resultInfo; }}
4)最后動(dòng)態(tài)生成的SQL就是這個(gè)樣子:
CREATE PROCEDURE `testbase`.`test`(in a_user_id varchar(100)) COMMENT ’測(cè)試接口’BEGINselect * from userInfo where user_id=a_user_id;END
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式2. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程3. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼4. php redis setnx分布式鎖簡(jiǎn)單原理解析5. 《Java程序員修煉之道》作者Ben Evans:保守的設(shè)計(jì)思想是Java的最大優(yōu)勢(shì)6. CSS3中Transition屬性詳解以及示例分享7. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼8. 如何在PHP中讀寫(xiě)文件9. java加載屬性配置properties文件的方法10. 什么是Python變量作用域
