Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create applications quickly by iBATIS

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

iBATIS how to quickly create applications, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

The iBATIS framework is actually quite simple, and it's equally simple to get started with. So how simple is it? In fact, it's so simple that you can create a complete application in 5 minutes with iBATIS--not a large enterprise resource planning (ERP) solution or a large e-commerce website, but a simple command-line tool that executes a SQL statement in an SQL mapping file and outputs the results to the console. The following example configures a simple static SQL statement to query a simple database table and output it to the console as follows:

This is not the most beautiful way to output data, but it gives you an idea of what the app is really doing. In the following sections, we will gradually implement this functionality from scratch.

iBATIS Quick App Creation 1: Installing the Database

For the purposes of the example application, we will use MySQL database. The iBATIS framework can use any database as long as it has a spec compliant JDBC driver. All you need to do is provide the driver's fully qualified class name and a JDBC URL in the configuration file.

Installing a database server is beyond the scope of this book, so we assume that the database server is installed and available and tell you what needs to be done on top of that. The following MySQL script constructs the table we will use and adds some sample data to it:

If you already have a different database server installed that contains some other data and you want to execute some SQL queries on that data, you can use it boldly in this example. You only need to modify the query statements in the SqlMap.xml file to include your SQL, and you also need to modify the SqlMapConfig.xml file to configure iBATIS to use your database. For the entire example to work successfully, you also need to know the name of the driver, the JDBC URL, and the username and password for the connection.

iBATIS Rapid App Creation 2 Writing Code

Since this example is a complete example and an introduction to using iBATIS, the code will be much simpler than the actual application. Type safety and exception handling will be discussed in more detail later, so we won't consider them here. Listing 2-4 gives the complete code:

Code Listing 2-4 Main.java

That's it! We configured iBATIS, executed SQL statements, and printed the results in about 10 lines of Java code. That's all the Java code needed for a fully functional iBATIS application. We'll improve on it later, but for now we'll move on to the basics of iBATIS configuration.

iBATIS Quick App Creation 3 Configuring iBATIS (Preview)

Since we'll go deeper into configuring iBATIS in the next chapter, we'll just briefly cover it here. You won't find much explanation of configuration options here, but we'll give you the most important information.

First, let's examine the SqlMapConfig.xml file. It is the starting point for using iBATIS and is responsible for putting together all the SQL mapping files. Listing 2-5 shows the SqlMapConfig.xml file used in our simple application.

Listing 2-5 SQL mapping configuration in the simplest iBATIS application

As you might have guessed, this is where we tell iBATIS how to connect to the database and which SQL mapping files to get. Since this is an XML file, we need to provide doctype and DTD for validation (1). SIMPLE is an alias for iBATIS built-in transaction processor (2). We need to provide the name of the JDBC driver for this transaction processor, the JDBC URL, and the username and password that allow you to connect to the database. Then you will provide your SQL mapping file (3). In this case, we only have one SQL mapping file, but you can provide as many as you want. There are a few other settings you can make in this file, which we'll cover in detail in the next chapter.

Now that you've seen the main configuration file, let's take a look at the SqlMap.xml file (Listing 2-6). This file contains the SQL statements we will run.

Listing 2-6 The simplest SQL mapping

In the XML in Listing 2-6, we accept a parameter of type String (parameterClass) as the value of the GROUPNAME column and map the resultClass to a HashMap.

Warning:

We don't recommend using Maps (e.g. HashMap, TreeMap) as domain models, but doing so does demonstrate the flexibility of mappings offered by iBATIS. You don't always need to map to JavaBeans--you can map directly to Map or primitive types.

Believe it or not, you've now seen all the code and configuration you need to use iBATIS. We purposely spread it out for printing purposes, but even so, the code is only about 50 lines total, including Java and XML. And more importantly, 45 of those 50 lines of code are about configurations that need to be written only once in an application, rather than once for each statement. As you've seen in previous examples in this chapter, using JDBC ends up requiring 50 lines or more of code per SQL statement.

iBATIS Rapid App Creation 4 Construct Apps

Often when building a large application, you use tools like Ant to simplify the build process. Since we only have one class in this example, we don't need to bother creating an Ant script for it. To compile this application, you only need to add two JAR files to classpath: ibatis-common-2.jar and ibatis-sqlmap-2.jar, so we just type them into the Java compiler from the command line:

javac -classpath ibatis-common-2.jar; ibatis-sqlmap-2.jar Main.java

Of course, the above code should be typed on the same line, and you should replace it with the actual path to the JAR file. If all goes well, the compiler should not produce any output to the screen, but simply create a Main.class file in the current directory.

iBATIS Quick App Creation 5 Running Apps

To execute this application, we need a few more JAR files, but not that many. To run our application, we just need to add the following JAR files to classpath: ibatis-common-2.jar, ibatis-sqlmap-2.jar, commons-loging.jar, and the JDBC driver (in this case, mysql-connector-java.jar), so we should type:

java -classpath ;mysql-connector.jar;commons-logging.jar; ibatis-common-2.jar;ibatis-sqlmap-2.jar;.Main

Similarly, the above code must be on the same line when editing and should be replaced with the actual path in the system.

This program will tell you how many records you selected and output them in a coarser format, similar to the following:

The iBATIS framework is designed to be very flexible. It can be a very lightweight and simple framework that only executes SQL and returns data, but it can also be used to do much more.

One of the keys to this flexibility lies in the correct configuration of the framework. In the next chapter, we'll discuss the two main profile types and then examine some patterns for handling complex situations through the use of configurations.

Note:

Configuration files are standard XML documents. This means that if you have a trendy XML editor, you can use a DTD (Document Type Definition) to validate your document for syntax errors, and sometimes even provide code hints and auto-completion during editing.

After reading the above, do you know how iBATIS can create apps quickly? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 298

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report