Simple IBatis instance

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.