Ibatis Step by Step 2

zhaozj2021-02-16  70

In this section, an actual example is used to illustrate the simple use of Dynamic-mapped-statement and insert.

After the system releases, some logs left, especially the SQL statements left, in particular the SQL statement, to obtain the first hand of the user's habit, so that we will improve. Today, I have made this simple analysis. Because almost all queries in the system use my unified excuse to query, SQL's log has a unified flag, which is relatively easy to collect. Next, step by step Next process: 1. Collecting log 1. Get the log file from the server, no need to say 2. Use the program to analyze log, and save SQL, do some processing, save it to the database. To save a data into the database, you need to configure a saved SQL:

INSERT INTO SQL_STMT (ID, EXE_TIME, SQL, PARSED_SQL, SQL_VALUES) VALUES ( seq_sql_stmt.nextval,?,?,?,?)

Just provide a simple bean in the program, this bean only needs "INSERT-PARAMS" to configure the field, which we only need:

Sqllog log = parseqllog (sqlstring); // set related fields SQLMAPCONFIG.GETSQLMAP (). ExecuteUpdate ("InsertSQL", LOG);

This will be saved, simple. 3. Use the program to run all LOG analysis, write to the database to three, analyze logs may save data, see how much and other ORM tools, even feel more troublesome, below with a query Example to illustrate the flexibility and power of Ibatis. In order to analyze these logs, when I wrote in the database, the values ​​in SQL were removed, replaced with Prepared similar SQL to facilitate the analysis of SQL universality. Here, you will take a question, when designing the DAO framework, it is best to consider the possible extension, it is very important to use a better frame, just collect logs here, because we use a unified interface, not only log collection is easy For future extensions and modifications, it is also important. LOG analysis has more than 800,000 records, and we need to get some useful information, you must provide a simple query. After a simple understanding, we will generally take several conditions: execution time, SQL statement (blur query), count of statistics.

Then, he began to write the configuration file: [code] SELECT * FROM (SELECT ROWNUM count_row_num , w_o_l_f_w. * fROM (select * from (select PARSED_SQL, COUNT (*) CNT from sQL_STMT = # Exacestart #]]> (SQL Like '%' || # SQL # || '%' ory_SQL Like '%' || # SQL # || '%') = # countfrom #]> group by pased_sql = #__ startpoint #)]]]]]]]> <

/ Dynamic-mapped-statement> [/ code] is a dynamic SQL, it may be more ugly, but he is used. In this way, the program is simple, in the action, we will handle the submitted data: [code] parameterParser PP = data.getParameters (); map params = new hashmap (pp.getKeys (). Length-2) Object [] keys = pp.getKeys (); for (int i = 0; i

Return Sqlmapconfig.getsqlmap (). ExecuteQueryforList ("Getsqllogstatistics", params;

Very simple?

In fact, in order to develop, we just put some logic written in the Java program, such as something worth it, what is put in the XML of SQLMap, how much workload is not reduced, but modified When adjusting, the workload is greatly reduced, we don't need to modify the program, compile, release, and then run, we only need to simply modify the XML of SQLMap to do this, for the post-period adjustment ( For example, DBA wants to turn SQL to TUNING), maintenance after project release, etc., can be said to be greatly reduced.

Fourth, post

This is just a very simple application, simply uses the IBATI SQLMAP function, I think we can consider using it in the project, but any tool has his most applicable place, my opinion is to add modifications and deletion in the future. You can still continue to use some ORM tools, as for querying, single-table or simple regular queries can be born in the code generation tool, for very complex, and users may modify or have the need to perform performance in later periods Adjustment query, we can consider using Ibatis.

转载请注明原文地址:https://www.9cbs.com/read-15853.html

New Post(0)