Ibatis.net-use ibatis.net to read data

Source: Internet
Author: User

ViewArticleIndex Via http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html

 

Ibatis.net official: http://www.mybatis.org/

Ibatis.net is used to establish a ing between database queries and object attributes. However, it is not a ing tool for object relations and is only used for help.ProgramYou can map objects to SQL statements or stored procedures. Therefore, it can only be called semi-automatic or/M tools.

Ibatis.net Configuration:

1. Reference several DLL files. Pay attention to reference them at the data layer.

When you use ing separately, you only need to reference ibatisnet. datamapper. dll.

Ibatisnet. Common. dll and Castle. dynamicproxy. dll are required.

Common is my own.

Ibatisnet. dataaccess. dll is optional. If you use the data access component, you also need to add a reference to ibatisnet. dataaccess. dll.

2. After adding components, you need to add three XML documents

1 providers. config ---- datamapper determines the type of Database Based on this (put on the data layer)

2 sqlmap. xml ---- data ing document, which contains SQL statements (placed on the data layer)

3 sqlmap. config ---- datamapper configuration document, which describes in detail the location of the sqlmap. xml and providers. config documents in the project, and other configuration items (which must be placed in the Web and directory ).

First look at sqlmap. config

 

<? XML version = "1.0" encoding = "UTF-8" ?>
< Sqlmapconfig Xmlns = "Http://ibatis.apache.org/dataMapper" Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" > < Settings >
< Setting Usestatementnamespaces = "True" /> // If it is true, the full namespace must be added before the name of the query statement during data query.
< Setting Cachemodelsenabled = "True" /> // Whether or not datamapper can enable the cache for users in regionalization configuration. The default value is true.
</ Settings >
< Providers Embedded = "Dal. providers. config, Dal" /> // Specify the location of providers. config

< Database >
< Provider Name = "Sqlserver2.0" /> // If the default data provider is used, this statement is optional. If multiple databases are used in the system, you need to configure the provider content.
< Datasource Name = "Connstr" Connectionstring = "Data Source = .; database = test; user id = sa; Password = 123456; connection lifetime = 3; min pool size = 1; Max pool size = 50; multipleactiveresultsets = true ;" /> // Database link string,
</ Database >

< Sqlmaps >
< Sqlmap Embedded = "Dal. Maps. userinfo. XML, Dal" /> // The location of the program's data ing file. If there are multiple XML files, write multiple lines. If there are more files, you can also write them in a separate XML file, such < Sqlmap Resource = "Maps/all. xml"/>, and then add a data ing file in all. XML, so that a group of data ing files can be loaded.
</Sqlmaps >
</ Sqlmapconfig >

 

You can download and view the specific document configuration in the demo of the attachment.

3. Create a sqlmapper instance. This is the singleton mode. The file name is sqlmapper. CS and is placed in the data layer.

 

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Ibatisnet. Common. utilities;
Using Ibatisnet. datamapper;
Using Ibatisnet. datamapper. configuration;
Using Ibatisnet. datamapper. sessionstore;
Namespace Dal
{
Public Class Sqlmapper
{
Private Static Volatile Isqlmapper _ mapper = Null ;

Protected Static VoidConfigure (ObjectOBJ)
{
_ Mapper =Null;
}

Protected Static Void Initmapper ()
{
Configurehandler handler = New Configurehandler (configure );
Domsqlmapbuilder builder = New Domsqlmapbuilder ();
_ Mapper = builder. configureandwatch ( " Sqlmap. config " , Handler );
_ Mapper. sessionstore =New Hybridwebthreadsessionstore (_ Mapper. ID );
}

Public Static Isqlmapper instance ()
{
If (_ Mapper = Null )
{
Lock ( Typeof (Sqlmapper ))
{
If (_ Mapper = Null ) // Double-check
{
Initmapper ();
}
}
}
Return _ Mapper;
}

Public StaticIsqlmapper get ()
{
ReturnInstance ();
}
}
}

 

External call method:

Ilist list = sqlmapper. instance (). queryforobject <list> ("userinfo. getcount", Param );

Because it is a Singleton, during the first call, the domsqlmapbuilder object will create a sqlmapper instance by querying sqlmap. config, and then the call will be directly read from the cache.

The domsqlmapbuilder. configureandwatch () method monitors configuration file updates. If the configuration or ing file is updated, the sqlmapper object will be reloaded without restarting the system.

 

The configuration is complete. For details, refer to demo: Demo download.

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.