First of all, I want to declare that I am not very familiar with ibatis, and I am still in the learning stage. I am only recording the process of learning ibatis. Of course, by following this process, you will eventually complete your first ibatis program. There must be a lot of unsatisfactory things, or something that needs optimization. I hope you can discuss this with me.
Why should I learn ibatis? It is actually very simple, because I can use 20% of the Code to complete 80% of the functions. Another good thing for me is that my SQL statements can be easily modified through the configuration file.
Step 1: Prepare. In the ibatis package.The http://www.ibatis.com is downloaded, and then add the three files ibatis-dao-2.jar, ibatis-sqlmap-2.jar, and ibatis-common-2.jar to the classpath of your project. (It is also recommended to download the ibast source program package and instructions. I don't need to talk about this document. Everyone just needs to use it. The main purpose of downloading the source package is to make better use of this open-source project. Haha, you can modify some of the functions here by yourself :))
?? Step 2: create a simple table in the database. For example, we have created a test table with the ID, name, and email fields. By the way, the more fields, the more benefits you will find :)
?? Step 3: Create an entity for the table. The Code is as follows:
/*
? * Created on 2004-8-9 *
? */
Package test;
/**
? * @ Author? Reason
? *
? * Copyright (c) 2004? Reason br>? *
? */
Public class testinfo {
??? Int ID;
??? String name;
??? String email;
???
??? /**
???? * @ Return returns the email.
???? */
??? Public String getemail (){
??????? Return email;
??? }
??? /**
???? * @ Param email the email to set.
???? */
??? Public void setemail (string email ){
??????? This. Email = Email;
??? }
??? /**
???? * @ Return returns the ID.
???? */
??? Public int GETID (){
??????? Return ID;
??? }
??? /**
???? * @ Param ID the ID to set.
???? */
??? Public void setid (int id ){
??????? This. ID = ID;
??? }
??? /**
???? * @ Return returns the name.
???? */
??? Public String getname (){
??????? Return name;
??? }
??? /**
???? * @ Param name the name to set.
???? */
??? Public void setname (string name ){
??????? This. Name = Name;
??? }
}
??? Step 4, OK, let's get started with ibast!
?????? Today, we first create an interface for our development program. As for why the interface should be used, I will see it with you. (Note: I didn't classify the packages in this test program. Well, to write this test program faster, you don't want to write it like this .)
/*
? * Created on 2004-8-9 *
? */
Package test;
/**
? * @ Author? Reason
? *?
? * Copyright? (C) 2004 reason
? *
? */
Import java. util. List;
Public interface testdao {
? Public list getlist (int id );
? Public int insert (testorder test );
? Public int Delete (testorder test );
? Public int Update (testorder test );
}
??? We have defined the basic database operation method here. If you have other requirements in actual development, you can write other methods according to this!
?? Haha, I am a little tired. I am still working overtime today. I will introduce the reference of ibatis mapdao and the final implementation of testservice to the source code tomorrow :)
----- Ibast we can start with the most acceptable method, so I write ibatis Database Configuration and SQL statement at the end according to my own ideas, I think it may be easier to see the ing later after reading these methods!