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: XML Version = "1.0" Encoding = "UTF-8"?> "- Always Ensure to Use the Correct XML Header As Above! ->
<-! Configure a datasource to use with this SQL Map using SimpleDataSource.Notice the use of the properties from the above resource ->
XML Version = "1.0" encoding = "UTF-8"?> < sqlmap namespace = "author">
SQLMAP>
(3) The program call is long because of the source code, so I only give some simple program calling methods, so if someone wants the source code, leave your mailbox. 1. Initialize a SqlMapClient object code is as follows: public class SqlMapConf {private static SqlMapClient sqlMapClient; static {try {System.out.println ( "sqlMapClient initing ....."); String resource = "SqlMapConfig.xml"; Reader reader = Resources.getResourceAsReader (resource); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient (reader);} catch (Exception e) {e.printStackTrace (); throw new RuntimeException ( "Error initializing MyAppSqlConfig class Cause:." e);}} . public static SqlMapClient getInstance () {return sqlMapClient;}} 2 and then to write the Author table of a bean, the following code: public class Author {private int id; private int age; private String name; private String address; private String telephone; Public int getId;} public void setid (int id) {this.id = id;} public int getage () {Return Age;} public void setage (int agent) {this.age = agn Public string getname () { return name;} public void setName (String name) {this.name = name;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} public String getTelephone () { Return telephone;} public void settelephone (string telephone) {this.telephone = telephone;}}
3. Program call: Here will only demonstrate the method of getAuthor, INSERTAUTHOR1, UPDATEOUTHOR, and Deleteauthor. First, a SQLMAPCLIENT instance should be obtained: sqlmapclient sqlmapclient = SQLMAPCONF.GETInstance ();
(1) getAuthor: Author author = (Author) sqlMapClient.queryForObject ( "getAuthor", new Integer (1)); (2) getAllAuthor List authorList = (List) sqlMapClient.queryForList ( "getAllAuthor", null); (3) INSERTAUTHOR: AUTHOR Author = new author (); author.setname ("Author 3"); author.setage (31); Author.SetAddress ("Nanjing"); author.settelephone ("025-987654321"); SQLMapClient.insert (operaName, author); (4) updateAuthor author author = new author (); author.setName ( "Updated"); author.setId (authorID); sqlMapClient.update (operaName, author); (5) deleteAuthor sqlMapClient.delete ("deleteauthor", new integer (authorid); here is just a simple example, hoping to help fast entry, and do not analyze the principle of Ibatis, but through these calls, I think you may be able to guess For part of Ibatis, please pay attention to follow-up article about Ibatis's principles and advanced applications.