亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Java Spring動(dòng)態(tài)生成Mysql存儲過程詳解

瀏覽:47日期:2022-08-31 16:37:09

一、 背景

由于公司業(yè)務(wù)需要?jiǎng)討B(tài)配置一些存儲過程來生成數(shù)據(jù),之前嘗試過使用jpa來完成,或多或少都存在一些問題,最后使用了spring的Jdbctemplate。

二、 環(huán)境

1.此隨筆內(nèi)容基于spring boot項(xiàng)目

2.數(shù)據(jù)庫為mysql 5.7.9版本

3.jdk 版本為1.8

三、 說明

說明:為方便表示,下列存儲過程在代碼中的表示我稱之為接口配置

四、 內(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ù)名稱 private String explain_info; //參數(shù)描述 private String type;// 輸入輸出類型 private String paraType; // 參數(shù)類型 private Integer paraLen;//此處省略get、set…}

2、編寫頁面輸入接口配置的信息;

1)Html部分代碼:

<div class='form-group'> <label for='name' class='col-sm-2 control-label'>接口名稱<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'>接口說明:</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部分代碼太長,就只貼一個(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)建失敗,請檢查sql語句,注意結(jié)尾不能有分號!具體錯(cuò)誤信息:'+result.msg, {icon: 5}); } }, error: function (xhr, textStatus) { layer.alert(textStatus); } });}

3、將數(shù)據(jù)上傳到后臺之后,后臺生成存儲過程。當(dāng)然一般情況下,我們還是先把數(shù)據(jù)接口和接口明細(xì)數(shù)據(jù)持久化保存,再來執(zhí)行創(chuàng)建操作,可以保證數(shù)據(jù)不會丟失。此處由于篇幅問題,我就省略了中間這一步。

1)創(chuàng)建一個(gè)service 的接口:

public interface TestService { ResultInfo createPro(Map<String,Object> map);}

2)然后創(chuàng)建接口的實(shí)現(xiàn)類:

@Servicepublic class TestServiceImpl implements TestService { /** * 創(chuàng)建存儲過程 * * @param map 接口配置和接口參數(shù)信息 * 參數(shù)詳解: type 輸入輸出參數(shù),取值為 in,out * paraType 參數(shù)類型。取值為: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(); // 建立生成過程的語句 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ù)類型 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()); // 存儲過程內(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) { // 接口配置名稱 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ù)名稱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)建過程成功'); } 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 ’測試接口’BEGINselect * from userInfo where user_id=a_user_id;END

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 俄罗斯胖老太与小伙交 | a级毛片免费在线观看 | 边做边摸边揉的免费视频 | 天天看a| 国产在线一区二区三区 | 国产精品久久久久久久久久久搜索 | 精品免费在线视频 | 欧美性野久久久久久久久 | 九九免费高清在线观看视频 | 久久蜜桃亚洲一区二区 | 免费一区二区三区视频狠狠 | 大黄一级片 | 久久久久在线视频 | 49pao强力免费打造在线高清 | 亚洲国产精品线在线观看 | 国产成人精品曰本亚洲78 | 天天爽影院一区二区在线影院 | 免费黄视频网站 | 国产在线拍国产拍拍偷 | 二区在线播放 | 暧暧视频在线观看免费 | 在线亚洲一区 | 91精品国产91久久久久久最新 | 国产freexxxx性播放麻豆 | 成人午夜久久精品 | 国产麻豆91网在线看 | 91成人午夜性a一级毛片 | 亚洲aⅴ在线 | 免费网站直接看 | 国产精品成人亚洲 | 欧美三级不卡视频 | 精品国产一级在线观看 | 成年性午夜免费视频网站不卡 | 亚洲主播 | 久久久亚洲精品蜜桃臀 | 国产在线精品一区二区夜色 | 亚洲欧美日韩国产精品第不页 | 这里有精品 | 国产永久免费视频m3u8 | 亚洲免费观看在线视频 | 色婷婷丁香六月 |