I recently studied IBatis and made a simple example, which is clear and concise, as follows:
Environment: vs2008 + SQL Server2005
1. Structure
A WebSite (IbatisSample), a Model for entity classes, and a Service for methods.
2. WebSite content Introduction
Map File
The SQL statement is written here.
SqlMap. Config file
Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<SqlMapConfig xmlns = "http://ibatis.apache.org/dataMapper" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
<! -- External configuration file
<Properties resource = "properties. config"/>
-->
<Settings>
<Setting useStatementNamespaces = "false"/>
</Settings>
<Providers resource = "providers. config"/>
<! -- Database connection information -->
<Database>
<Provider name = "sqlServer2.0"/>
<DataSource name = "Person" connectionString = "server = (local); database = Northwind; Integrated Security = SSPI; Persist Security Info = False;"/>
</Database>
<! -- SqlMap. xml Information -->
<SqlMaps>
<SqlMap resource = "Maps/Person. xml"/>
</SqlMaps>
</SqlMapConfig>
Database connections and map files are defined here.
The database is SqlServer, where connectionString uses Windows integration verification.
In SqlMaps, the map file is defined.
3. Introduction to Model
Stores object classes.
4. Service content
You must first reference two components. In PersonBiz, the method corresponds to the object class Person.
public static Person SelectPersonById(string id)
{
return Mapper.Instance().QueryForObject<Person>("SelectPersonById", id);
}
The SelectPersonById in double quotation marks is the name of the SQL statement written in the previous Person. xml map file.
5. Person table creation statement
The Northwind database is used. If O does not have this database, you can create it yourself. Instead, you must replace the database name with your own database name in SqlMap. Config.
Code
USE NORTHWIND
CREATE TABLE PERSON(
ID VARCHAR (20) NOT NULL,
FIRST_NAME VARCHAR (40) ,
LAST_NAME VARCHAR (40) ,
BIRTH_DATE DATETIME ,
WEIGHT DECIMAL(4, 2) ,
HEIGHT DECIMAL(4, 2) ,
PRIMARY KEY (ID)
)
This is fine and easy.
Code packages saved here: http://www.brsbox.com/filebox/down/fc/1905a7f2284350713b079c8ac3c9d18d
Which one can tell me how to upload the code package? Thank you.