diff --git a/code/springboot/newBank/src/main/java/demo/zhangjialei/controller/makeNo.java b/code/springboot/newBank/src/main/java/demo/zhangjialei/controller/makeNo.java new file mode 100644 index 000000000..4d46affe1 --- /dev/null +++ b/code/springboot/newBank/src/main/java/demo/zhangjialei/controller/makeNo.java @@ -0,0 +1,51 @@ +package demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.Map; + +@RestController +public class makeNo { + + @Autowired + private JdbcTemplate jdbcTemplate; + + /* + * 表名: noMaker + * + * 字段: + * noName (varchar) + * noValue (int) + * */ + + @RequestMapping("/makeNo") + public int makeNoFn(HttpServletRequest request){ + + // 接收编号名noName(String) + String noName = request.getParameter("noName"); + + // 向编号表查询 编号名noName是否已经存在 + String sql_qry = String.format("select * from noMaker where noName = '%s'", noName); + List> list = jdbcTemplate.queryForList(sql_qry); + + if (list.size() != 0) { + // 若存在 则将 noValue +1 + String sql_upd = String.format("update noMaker set noValue = noValue +1 where noName = '%s'", noName); + jdbcTemplate.update(sql_upd); + + } else { + // 若不存在 则插入新行 + String sql_ins = String.format("insert into noMaker (noName,noValue) value ('%s',1)", noName); + jdbcTemplate.update(sql_ins); + } + + // 返回 noValue + String sql_sel = String.format("select noValue from noMaker where noName = '%s'", noName); + return (int) jdbcTemplate.queryForList(sql_sel).get(0).get("noValue"); + } +}