In the code I showed you in the last article, there are a lot of repetitions, like writing query, update, delete, add methods for each entity class like StudentInfo, ClassInfo, and RoomInfo with basically identical logic. Since it is repetitive, you can find ways to extract rules and simplify code.

Cat came up with two directions.

First, since the read is the database table memory map, so the simplest, we can define a class as follows:

public class Table{ String column1; String column2; . String columnN. }Copy the code

N is just a little more than the number of columns in the database table with the largest number of columns. So no matter how many tables we want to concatenate from the database, we just pull out the data and put column1, column2, column1, column2, and so on in order. And delete, modify, increase directly write SQL statements on the line, is also very convenient.

At this point, you can use List<Map<String, String>> to store all the data in a table directly. For example, we could use a List<Map<String, String>> to store the student data we queried in the previous article:

public static void main(String[] args) {/ / test
		StudentOperation stuOper=new StudentOperation();
		List students=stuOper.selectAll();
		List<Map<String, String>> table=new ArrayList<Map<String, String>>();
		for(Object obj:students){
			StudentInfo stu=(StudentInfo)obj;
			Map<String, String> row = new HashMap<String, String>();
			row.put("Name", stu.getStudentName());
			row.put("Class", stu.getStudentClass().getClassName());
			table.add(row);
		}
		System.out.println(table.toString());
	}
Copy the code

Take a look at the output:

[= {name = martians, class, grade 1 class 1}, {name = tyrannosaurus rex, class = a class 1 grade}, {name = Zhao Liu, class = class two, grade one}]Copy the code

StudentInfo = List<Map<String, String>> StudentInfo = List<Map<String, String>> StudentInfo = List<Map<String, String>> Assign a value to a List<Map<String, which is a String>> object.

When you get here, does it feel almost perfect? Each Map<String, String> object contains column names (e.g. name, class). In the real world, you only need to save the column name once, so should you encapsulate it? There is a DataTable class in the C# world that perfectly simulates the shape of a database table in memory. If you are interested, you can see the function for yourself and implement it in Java.

The first direction, which simply simplifies some of the code, is less elegant. Why not elegant, the most intuitive, Java is object-oriented, and the front equivalent of the object is cut, no object, that encapsulation, polymorphism and other object-oriented benefits are not good to enjoy. So we have to think of another way out.

Actually also very want to, we can let the name of the class the same as the database table name, and then let the class name of the property the same as the database column names, this rule is very obvious, the structure of the database we don’t know, but we can through the class structure of the database table structure, which can operate the database, and class structure could be obtained by reflection, In this way, the problem is solved.

And relationships between tables… It seems that class structure alone is not enough to describe, so you can use other things such as XML files, such as Java annotations to describe more of the relationship between class and database structure. Well, as you can imagine, it’s very difficult for something like this to encapsulate itself. Haha, but now there are frameworks out there, we just need to write code according to the rules of the framework, and it’s all fun and games. Frameworks in this category are Hibernate and Mybatis, which will be introduced in a future series.

OVER.


Project source code, series of tutorials and more content, please visit panda programming network