IBATIS2.0 instructions (1) - Getting Started Examples [Original]

xiaoxiao2021-03-06  106

This article will help you quickly learn about the Ibatis framework from a specific example of iBatis.

A simple IBATIS app contains the following basic steps:

First, configuration file 1. Configure SQLMAPConfig.properties file

2. Configure SQLMAPConfig.xml files

3. Configure the sqlmap.xml file (there may be multiple files, in general, you can correspond to a SQLMAP.XML file with a table, the file name can be the same as the table name)

Note: The SQLMAPConfig.xml files described above must be in the classpath, SQLMAPCONFIG.PROPERTIES and SQLMAP.XML files can be in the class path, or may not be in the classpath. When the SQLMapConfig.properties and SQLMap.xml files are not in the classpath, the configuration is different, in this article, these three files are placed in the class path.

Second, program call 1. Initialize the SQLMAPClient object.

2. Run SQL statement: You can call Queryfor ... (), INSERT (), update (), delete () to perform SELECT, INSERT, UPDATE, and DELETE operations separately.

Ok, let's explain it in conjunction with example: Third, an example:

The following example is to explain as an example in mysql, establish an Author table, in order to facilitate debugging code, you can ibatis-compon-2.jar, ibatis-dao-2.jar, ibatis-sqlmap-2.jar and lib All JARs in the directory are loaded into your program. In subsequent articles, the use of each JAR will be described.

(1) Creating a database and table Creating a database for Ibatisexample, a database Create Table Author (auth_id int (8) Not null auto_increment, auth_name varchar (100) Not null default ', auth_age int (3) Not null default' 0 ', Auth_tel varchar (100) Not null default ', auth_address varchar (100) Not null default', primary key (auth_id)) TYPE = Myisam; Insert Into Author Values ​​(1, 'Author 1 ", 30,' 025-12345678 ',' Nanjing '); Insert Into Author Values ​​(2,' Author's 2 ', 30,' 025-12345678 ',' Nanjing ');

(2) profile

1. Configure SqlMapConfig.properties file contents: driver = org.gjt.mm.mysql.Driverurl = jdbc: mysql: //192.168.0.26: 3306 / IBatisExample useUnicode = true & characterEncoding = GB2312username = rootpassword = 123456?

2. Configure the contents of the SQLMAPConfig.xml file file: "- Always Ensure to Use the Correct XML Header As Above! -> <-! These settings control SqlMapClient configuration details, primarily to do with transactionmanagement They are all optional (more detail later in this document) -..>

<-! Configure a datasource to use with this SQL Map using SimpleDataSource.Notice the use of the properties from the above resource -> 3. Configuring SQLMap.xml File Here We are named Author.xml

< sqlmap namespace = "author">

CopyRight © 2020 All Rights Reserved
Processed: 0.045, SQL: 9