520 ago, I gave up my time with my girlfriend and was forced to write a code: "SSM framework integration + excel file upload to database + data update“

SSM framework integration + excel file upload to database + data update

Tips:
If you are not familiar with SSM framework, you can try the simple exercise of "spring and mybatis integration".
Last article wrote "spring and mybatis integration" complete test questions and analysis, interested partners can go to have a look, hope to help you.

Integration of spring and mybatis

preface

Today, the teacher has issued a new task!!
A few days ago, I learned the integration of SSM framework, and the teacher asked to add an excel file to upload it to the database and update it.

??? New homework???

No way! I have such a bad temper! do not do!
I stood up on the spot, angrily said to the teacher: "that, excuse me, old teacher, can you not do it? Because it's 520, I have to prepare gifts for my girlfriend, and I want to talk to my girlfriend.. Friends are going to travel. "
Teacher: "cheat who, little brother! The whole class knows you don't have a girlfriend. Go back and do your homework honestly
Me: death, the end of the play!

Hurt, broken and tired.....
But I can only start to work....

1, SSM framework integration

SSM framework: integration of Spring, Spring MVC and Mybatis

1. Create customer table

2. Create persistent classes

package com.po;

public class customer {
	private Integer id;
	private String name;;
	private String job;
	private String phone;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "customer [id=" + id + ", name=" + name + ", job=" + job + ", phone=" + phone + "]";
	}
	
	
}

3.Dao layer

customerdao.java:

package com.dao;

import java.util.List;

import com.po.customer;


public interface customerdao {
	//	Query all the information in the customer table
	public List<customer> findallcustomer();
	//  Add customer information
	public void addcustomer(customer customer);
}

Two methods are defined in the interface

1. Query all the information in the customer table
It is used to search directly later and display on the initial page
2. Add customer information
It is used to import excel file, parse excel file, export customer object and add it to database later!!!

customerdao.xml:
Implement the methods in the interface~~~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.dao.customerdao">
    <!-- Query all the information in the customer table -->
    <select id="findallcustomer"  resultType="customer">
        select * from customer 
    </select>
    
   <!-- Add customer information -->
    <insert id="addcustomer" parameterType="customer" >
        insert into customer(name,job,phone) values(#{name},#{job},#{phone}) 
    </insert>
 
</mapper>

4.service layer

Define interface:
customerservice.java

Implement the methods in the interface:
customerserviceimpl.java

5.controller layer

customercontrol.java:
Define a method to display the customer table, which will get the data from the database and send it to the front end.
The path is findallcustomer.

uploadcontrol.java:
Realize file upload function:

6. Operation results:

Input path: http://localhost:8005/17/findallcustomer
Get the results!

Successfully read the data from the database and return to the front-end jsp.
After the successful integration of ssm framework, let's add some functions to it!

2, SSM framework integration + excel file upload to database + data update

1. Analyze excel data

We create a new method to analyze excel data in the controller layer
Poi.java:
To parse the data in excel and return the customer object:

Analysis of excel reference from https://blog.csdn.net/qq_38638148/article/details/81103171

2. Add the data to the database and update it

The method of adding customers is defined in customerdao.xml of dao layer.
Here we just need to quote.
uploadcontrol.java:
Use for to traverse the customer list of the customer collection, and parse out the customers one by one,
And call the add customer method, which returns to the customer.jsp page.
Since then, loading excel file into database + updating data is completed......

3. Operation results:


Select excel file and upload:

Then is to add data success!!!!!!

Oh, give me another directory:

That's it!

summary

The first part is about the most basic application integration example of ssm framework;
The second part is to write a file upload and parsing function on this basis;

Oh, no, no, it should be:

oh! My God, it's so tiring to be a programmer!

I hope this article will help you.
If you can, please give me a compliment!

Tags: Java MySQL Spring Mybatis

Posted by a1ias on Wed, 19 May 2021 02:44:51 +0930