ssm框架一个表单插入多个数据的实现

2 篇文章 0 订阅

此处只是用来记录自己在学习java的问题(本人是做php的,因为php与java的却别比较大,所以可能有些简单的问题就会困扰自己,因此在此记录下来)
平时项目开发中往往会遇到这样的问题就是一个表单的数据需要进行拆分并同时插入多个关联表中。
一下是我的解决方案,有更好的解决方案,烦请各位大神、大咖不吝赐教发在评论中,小弟在此谢谢各位了.
先看控制的写法:

		// 首先实例化对应的实体类(存放get和set的类)(bean层下面的类)
        Test test = new test();
        TestTwo testTwo = new TestTwo();
        // 对属性进行赋值
        test.setName("张三");
       	test.setSex(1);
       	// 这是两个不同的类 由于是测试的所以使用的是同样的字段,在testTwo中会有一个test的主键ID
       	testTwo.setName("李四");
       	testTwo.setSex(1);
       	// 将对象添加Map中
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("test", test);
        map.put("testTwo", testTwo);
        testService.addTest(map);

Dao层接口的写法:

package com.ssm.dao;

import com.ssm.bean.testInfo;
import java.util.List;

public interface ITestDao {
    /**
     * 添加信息
     * @param testInfo
     */
    public int addTest(TestInfo testInfo);
}

service层接口的写法:

package com.ssm.service;

import com.ssm.bean.TestInfo;

import java.util.List;
import java.util.Map;

public interface ITestService {
    /**
     * 添加信息
     * @param objectMap
     */
    public int addTest(Map<String, Object> objectMap);
}

// testTwo
package com.ssm.service;

import com.ssm.bean.TestTwoInfo;

public interface ITestTwoService {
    /**
     * 添加商户员工信息
     * @param bizStaffInfo
     */
    public int AddTestTwo(TestTwoInfo testTwoInfo);
}

service接口的具体实现类

package com.ssm.service.impl;

import com.ssm.bean.TestInfo;
import com.ssm.bean.TestTwoInfo;
import com.ssm.dao.ITestDao;
import com.ssm.dao.ITestTwoDao;
import com.ssm.service.ITestService ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TestService implements ITestService {
    @Autowired
    private ITestDao testDao;
    @Autowired
    private ITestTwoDao testTwoDao;
    /**
     * 添加商户信息
     * 开启事务
     * @param objectMap
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addTest(Map<String, Object> objectMap) {
        int id = 0;
        TestInfo testInfo = (TestInfo) objectMap.get("test");
        String now_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        testInfo.setCreated_at(Timestamp.valueOf(now_time));
        testInfo.setUpdated_at(Timestamp.valueOf(now_time));
        id = testDao.addTest(testInfo);
        if (member_id > 0) {
            TestTwoInfo testTwoInfo = (TestTwoInfo) objectMap.get("testTwo");
            testTwoInfo.setTest_id(id);
            testTwoInfo.setUpdated_at(Timestamp.valueOf(now_time));
            testTwoInfo.setCreated_at(Timestamp.valueOf(now_time));
            int testTwo_id = 0;
            testTwo_id = testTwoDao.AddTestTwo(testTwoInfo);
            if (testTwo_id < 0) {
                throw new RuntimeException("新增testTwo失败");
            }
        } else {
            throw new RuntimeException("新增test失败");
        }
        return id;
    }
}


// testTwo
package com.ssm.service.impl;

import com.ssm.bean.TestTwoInfo;
import com.ssm.dao.ITestTwoDao;
import com.ssm.service.ITestTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BizStaffService implements IBizStaffService {
    @Autowired
    private ITestTwoDao testTwoDao;

    /**
     * 添加商户员工信息
     * @param testTwoInfo
     */
    @Override
    public int AddTestTwof(TestTwoInfo testTwoInfo) {
        return testTwoDao.AddTestTwo(testTwoInfo);
    }
}

mybatis的maooer,xml此处就不提供了,相信大家都是没有问题。
各位大神有什么更好的解决方案希望能够提供给小弟

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[ 2021年,将Spring全家桶的课程进行Review,确保不再有顺序错乱等问题导致学员看不懂内容,进入2022年,将Spring的课程进行整理,整理为案例精讲的系列课程,并开始逐步增加高阶的Spring Security等内容,课件将逐步进行上传,敬请期待! ]本课程是Spring全家桶案例精讲课程的第二部分Spring MVC,Spring案例精讲课程以真实场景、项目实战为导向,循序渐进,深入浅出的讲解Java网络编程,助力您在技术工作中更进一步。 本课程聚焦Java Spring的Web知识点,主要是关于Spring MVC的应用,包含:表单的增删改查、国际化、过滤器、拦截器、日志Log4j2及slf4j的使用、主题更改网站皮肤及样式、文件上传等的案例讲解,并且最后以一个SSM(Spring+Spring MVC+Mybatis)贯穿前后台的案例作为Spring MVC课程的终奖, 从而使大家快速掌握Spring的基础核心知识,快速上手,为面试、工作等做好充足准备。 由于本课程聚焦于案例,即直接上手操作,对于Spring的原理等不会做过多介绍,希望了解原理等内容的需要通过其他视频或者书籍去了解,建议按照该案例课程一步步做下来,之后再去进一步回顾原理,这样能够促进大家对原理有更好的理解。 【通过Spring全家桶,我们保证你能收获到以下几点】 1、掌握Spring全家桶主要部分的开发、实现2、可以使用Spring MVC、Spring Boot、Spring Cloud及Spring Data进行大部分的Spring开发3、初步了解使用微服务、了解使用Spring进行微服务的设计实现4、奠定扎实的Spring技术,具备了一定的独立开发的能力  【实力讲师】 毕业于清华大学软件学院软件工程专业,曾在Accenture、IBM等知名外企任管理及架构职位,近15年的JavaEE经验,近8年的Spring经验,一直致力于架构、设计、开发及管理工作,在电商、零售、制造业等有丰富的项目实施经验 【本课程适用人群】如果你是一定不要错过!  适合于有JavaEE基础的,如:JSP、JSTL、Java基础等的学习者没有基础的学习者跟着课程可以学习,但是需要补充相关基础知识后,才能很好的参与到相关的工作中。 【Spring全家桶课程共包含如下几门】 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值