SSM multi table addition

Development tools and key technologies: Eclipse Software, SSM Multi table addition 
Author: Chen Long
 Written on: April 28, 2021

Multi table addition should be a basic addition function required by every project. One function of this practical new data is a basic small function that every programmer must learn. How to realize this function of new data? In fact, there are many ways to complete a new function, I use eclipse software and the ssm framework in java to realize this new function. Programming software does not necessarily need to use eclipse software. There are many programming software. Choose what is suitable for the current fashion or familiar to myself. Then I will write code to complete this practical multi table new function. First, Mapper and Mapper XML to write a query statement.
Mapper and mapper The code in XML is usually generated directly by using some code generators or plug-ins. Of course, if the technology is hard enough, you can write it yourself. (add mapper code of the first table)

int insertSelective(salaryregistration record);

The code in Mapper is just the above sentence. The use of int is used to obtain whether the addition is successful, and then is the method name. This name can be set by yourself, but you still need to set some meaningful names. Otherwise, you may forget. You can write Mapper after writing Mapper The query statement in xml. (add the xml query code of the first table)

<insert id="insertSelective" parameterType="com.gx.po.salaryregistration" useGeneratedKeys="true"
	keyColumn="SalaryRegistrationID" keyProperty="salaryregistrationid">
    insert into pw_salaryregistration
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="salaryregistrationid != null">
        SalaryRegistrationID,
      </if>
      <if test="compensationnumber != null">
        CompensationNumber,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="salaryregistrationid != null">
        #{salaryregistrationid,jdbcType=INTEGER},
      </if>
      <if test="compensationnumber != null">
        #{compensationnumber,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

The above code is a new SQL statement. The ID here must be consistent with the method name in Mapper, otherwise the corresponding query statement cannot be found. parameterType is the mapping of parameters. I'll just make the parameters consistent with the entity class here. The role of userGeneratedKeys is to enable the return of successfully added ID, because what needs to be done here is multi table addition, Therefore, you need to return a new ID, which can be obtained in several ways. Then, keyColumn corresponds to the ID field in the database and keyProperty corresponds to the field in the entity class. Then the following query statements must be a simple part to judge whether there is data. If so, they need to be added to the new statement, If not, there is no need to add. I have omitted many fields here. These fields need to be added according to the fields in the new table. You can add as many as you want.
After you write Mapper, you start to write the code in the service persistence layer and serviceimpl implementation interface. The method name does not have to be the same as that in Mapper, but it needs to be consistent with that in serviceimpl.
(service code)

int InsertSalary(salaryregistration salary);

In fact, there is very little code here, and then there is the code in serviceImpl.

@Override
	public int InsertSalary(salaryregistration salary) {
		return saMapper.insertSelective(salary);
	}

In serviceImpe, you can directly call the query statement in Mapper. Of course, you can also write some required code here.
After the above codes are written, you can start to write the code in jsp. In fact, the code in jsp is also some relatively simple code. I won't write the front-end code here. Just write the code after clicking save.
//Preserve

$("#btnSaveUser").click(function(){	
		var layIndex=layer.load();
		$("#formSalaryInfo").ajaxSubmit(function(data){
			layer.close(layIndex);
			if(data.state){
				layer.open({
					  title:'Tips',
					  content: data.msg
					  ,icon:1
					  ,btn: ['determine']
					  ,yes: function(index, layero){
					    //Callback of button [button 1]
						  history.go(0);//Return to previous page
					  }
					  ,cancel: function(){ 
					    //Close callback in the upper right corner
					    history.go(0);//Return to previous page
					    //return false enable this code to disable clicking this button to close
					  }
					});
				//Close modal box
			}else{
				layer.alert(data.msg,{icon:2,title:"Tips"});
			}
		})
})

The js save code in Jsp is also relatively simple. Get the id of the clicked button, click it and enter the function method to execute the code. First, use the loading layer of the layui plug-in, and then submit the form to the controller. The submission path here is written in the HTML code. After submitting the form, all the data in the form will be submitted to the controller, After submission, a field is used to receive some data returned by the controller to the page. Then close the loading layer and judge whether the state returned by the controller is successful. If successful, it will prompt relevant information. If the save fails, just give the same prompt. The code of Jsp is written. Then there is the code in the controller, that is, the code in the web.
//Preserve

@RequestMapping(value = "InsertSalary")
	public JsonReturn InsertSalary(salaryregistration salary) {
		JsonReturn jsonReturn=new JsonReturn();
		jsonReturn.setState(false);
		//Judge whether the required data is filled in completely 		 if(Tools.isNotNull(salary.getCompensationnumber())&&Tools. isNotNull(salary.getCompensationname()) 				&& Tools. isNotNull(salary.getSetter())&&salary. getStandardtime()!= null) { 			// Judge whether the data already exists in the database
			List<salaryregistration> intsa=saService.getSalary(salary);
			if(intsa.size()==0) {				
				//Details of new salary standard
				int InsertSa=saService.InsertSalary(salary);
				if(InsertSa>0) {
					//Get the ID of the new data
				int SalaryRegistationID=salary.getSalaryregistrationid();	
					//Create salary basic information table object
					salaryStandard saStan=new salaryStandard();
					if(SalaryRegistationID>0) {
						saStan.setSalaryregistrationid(SalaryRegistationID);
					}if(Tools.isNotNull(salary.getSetter())) {
						saStan.setSetter(salary.getSetter());
					}if(salary.getStandardtime()!=null) {
						saStan.setStandardtime(salary.getStandardtime());
					}if(Tools.isNotNull(salary.getStandardregistrant())) {			saStan.setStandardregistrant(salary.getStandardregistrant());
					}					
					//New salary basic information table
					int iSa=saService.InsertSalarySta(saStan);
					if(iSa>0) {
						jsonReturn.setState(true);
						jsonReturn.setMsg("Saved successfully.");
					}else {
						jsonReturn.setMsg("Save failed!");
					}					
				}else {
					jsonReturn.setMsg("Failed to save salary standard detail table!");
				}
			}else {
				jsonReturn.setMsg("This salary information already exists!");
			}
		}		
		return jsonReturn;
	}

The code in the controller is basically annotated. You can see it carefully. What I use here is to return json data, so I encapsulate a JSONReturn class and post the code of this JSONReturn later. In addition, whether the data already exists in the database can be written or not, which can be added. If not written, duplicate data may be generated. Add the data of the second table, first or take the ID of the first table, and then create the object of the second table. Then write the required data into the object and save it. The SQL code of the second table is basically the same as that of the first table. You can refer to it to complete it. (encapsulated JSONReturn code)

public class JsonReturn implements Serializable{	
	private static final long serialVersionUID = 5863100019332084445L;
	private Boolean state;
	private String msg;
	public Boolean getState() {
		return state;
	}
	public void setState(Boolean state) {
		this.state = state;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}	
}

The above code is the code in the encapsulated JSONReturn, which can be used directly. In this way, a new function of multi table is completed.

Keywords: Java

Added by cyclops on Fri, 18 Feb 2022 23:34:08 +0200