使用Spring MVC将动态表保存到数据库

时间:2018-06-30 07:08:52

标签: java spring-mvc jsp

这是我的简单HTML表格。我在哪里用添加按钮在下面的表中追加了学生的姓名和标记...如何使用Spring MVC在数据库中保存所有学生的姓名和标记,我无法理解如何使用JDBC模板或Hibernate模板实现Student Dao ..我是Spring Framework的新手...请帮助我解决此问题。

DROP TABLE IF EXISTS TABLE1,TABLE2;
CREATE TABLE TABLE1(ID INT, VALUE INT);
CREATE TABLE TABLE2(ID INT PRIMARY KEY, VALUE INT);

DROP TRIGGER IF EXISTS T;
DELIMITER $$
CREATE TRIGGER T AFTER INSERT ON TABLE1
FOR EACH ROW
BEGIN
    INSERT IGNORE INTO TABLE2 VALUES (NEW.ID,NEW.VALUE);

END $$

INSERT INTO TABLE1 VALUES(1,1);
SELECT * FROM TABLE2;
INSERT INTO TABLE1 VALUES(1,2);
SELECT * FROM TABLE1;
SELECT * FROM TABLE2;

DROP TRIGGER IF EXISTS T;
DELIMITER $$
CREATE TRIGGER T AFTER INSERT ON TABLE1
FOR EACH ROW
BEGIN
    INSERT INTO TABLE2 VALUES (NEW.ID,NEW.VALUE)
        ON DUPLICATE KEY UPDATE VALUE = NEW.VALUE;

END $$

DELIMITER ;

SELECT * FROM TABLE2;
INSERT INTO TABLE1 VALUES(1,3);
SELECT * FROM TABLE1;
SELECT * FROM TABLE2;

DROP TRIGGER IF EXISTS T;
DELIMITER $$
CREATE TRIGGER T AFTER INSERT ON TABLE1
FOR EACH ROW
BEGIN
    DECLARE FOUND INT DEFAULT 0;
    SELECT  1 INTO FOUND FROM DUAL WHERE EXISTS(SELECT ID FROM TABLE2 WHERE ID = NEW.ID);
    IF FOUND = 0 THEN
        INSERT INTO TABLE2 VALUES (NEW.ID,NEW.VALUE);
    ELSE
        INSERT INTO TABLE2 VALUES (NEW.ID,NEW.VALUE)
        ON DUPLICATE KEY UPDATE VALUE = NEW.VALUE;
    END IF;

END $$

DELIMITER ;

SELECT * FROM TABLE2;
INSERT INTO TABLE1 VALUES(1,4),(2,1);
SELECT * FROM TABLE1;
SELECT * FROM TABLE2;

1 个答案:

答案 0 :(得分:1)

您可以在如下所示的控制器方法中接受这些参数。

@RequestMapping(method = RequestMethod.POST, produces = "text/html")
       public String create(
               @RequestParam(value = "stname1") String[] stname1,
               @RequestParam(value = "marks1") Integer[] marks1,
               BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {

       List<EntityPojo> list = new ArrayList<EntityPojo>();
       for(int i=0; i < stname1.length; i++){
           EntityPojo pojo = new EntityPojo();
           pojo.setStName(stname1[i]);
           pojo.setMarks(marks1[i]);
           list.add(pojo);
       }

       //pass this list to service and DAO.
}