(1)建立

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">

<!--<properties resource="SqlMap.properties"/>-->

<settings>
<setting useStatementNamespaces="false"/>
<setting cacheModelsEnabled="false"/>
</settings>

<!--<providers embedded="WHTR.PPMoney.DaoCfg.providers.config, WHTR.PPMoney.DaoCfg" />-->
<providers resource="providers.config"/>

<database>
<provider name="sqlServer2005"/>
<dataSource name="pp" connectionString="server=192.168.1.2;database=DB1;user id=sa;password=123"/>
</database>

<sqlMaps>
 
<sqlMap resource="Person.xml"/>
</sqlMaps>

</sqlMapConfig>

(2) 建立 Person.xml

<?xml version="1.0" encoding="utf-8" ?>
<!--<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">-->
<sqlMap namespace="" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<alias>
<typeAlias alias="Person" type="Ibatis.PersonModel"/>
</alias>
<statements>
<select id="selectAllPerson" resultClass="Person">
select top 1 * from
Person
</select>
<insert id="insertPerson">
insert into Person(name)
values(#name#)
<selectKey property="Id" resultClass="int" type="post">
select value = @@IDentity
</selectKey>
</insert>
</statements>
</sqlMap>

(3)建立 PersonModel类

namespace Ibatis
{
public class PersonModel
{
public int Id
{
get;
set;
}

public string Name
{
get;
set;
}
}
}

(4) 建立Dao

public class BaseDao
{
public static ISqlMapper _sqlMap = null;
static BaseDao()
{
_sqlMap = Mapper.Instance();
}
}

public class PersonDao : BaseDao
{
public IList<PersonModel> GetList()
{
ISqlMapper mapper = _sqlMap;
IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("selectAllPerson", null); //这个"SelectAllPerson"就是xml映射文件的Id
return ListPerson;
}

public int AddPerson(PersonModel person)
{
Hashtable ht = new Hashtable();
ht.Add("name",person.Name);
object o = _sqlMap.Insert("insertPerson", ht);
return Convert.ToInt16(o);
}
}

(5) 使用方法

class Program
{
static void Main(string[] args)
{
var person=new PersonModel(){Name="zhang"};

PersonDao dao = new PersonDao();
int id= dao.AddPerson(person);
IList<PersonModel> ListPerson = dao.GetList();
foreach (PersonModel p in ListPerson)
{
Console.WriteLine(p.Id + p.Name);
}

Console.ReadKey();

}
}

IBatis学习的更多相关文章

  1. 【web开发学习笔记】ibatis学习总结

    ibatis学习总结 ibatis数据库配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCT ...

  2. ibatis 学习笔记 3 - pfpfpfpfpf的专栏 - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  3. Ibatis学习总结6--使用 SQL Map API 编程

    SQL Map API 力求简洁.它为程序员提供 4 种功能:配置一个 SQL Map,执行 SQL update操作,执行查询语句以取得一个对象,以及执行查询语句以取得一个对象的 List. 配置  ...

  4. Ibatis学习总结4--SQL Map XML 映射文件扩展

    SQL Map XML 映射文件除了上文提到的属性还有一些其他重要的属性,下文将详细介绍这些属性. 缓存 Mapped Statement 结果集 通过在查询 statement 中指定 cacheM ...

  5. Ibatis学习总结3--SQL Map XML 映射文件

    在前面的例子中,只使用了 SQL Map 最简单的形式.SQL Map 的结构中还有其他更多 的选项.这里是一个 mapped statement 较复杂的例子,使用了更多的特性. <sqlMa ...

  6. Ibatis学习总结2--SQL Map XML 配置文件

    SQL Map 使用 XML 配置文件统一配置不同的属性,包括 DataSource 的详细配置信息, SQL Map 和其他可选属性,如线程管理等.以下是 SQL Map 配置文件的一个例子: Sq ...

  7. Ibatis学习总结1--ibatis简介和SQL Maps

    最佳维护的一个项目使的是ibatis框架,在闲暇之余将手头的开发手册和平时开发的理解做一下总结,言归正传. 简介 使用 SQL Map,能够大大减少访问关系数据库的代码.SQL Map 使用简单的 X ...

  8. Ibatis学习记录

    几大要素:1.jdbc.properties //数据库连接配置2.SqlMapContext.xml //主配置文件3.user_SqlMap.xml //映射文件4.三层框架 创建Ibatis工程 ...

  9. ibatis学习之道:ibatis的<[CDATA]>dynamic属性跟#$的应用

    ibatis的<![CDATA]>,dynamic属性和#,$的应用 <![CDATA[   ]]>的正确使用 ibatis作为一种半自动化的OR Mapping工具,其灵活性 ...

随机推荐

  1. mybatis resultMap映射学习笔记

    这几天,百度mybatis突然看不到官网了,不知道百度怎么整的.特此贴出mybatis中文官网: http://www.mybatis.org/mybatis-3/zh/index.html 一个学习 ...

  2. springMvc的第一个demo

    1.下载jar包 http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.3.RELEASE/ 2.下载源码 j ...

  3. HIbernate的增删改

    数据库是oracle 以一对多为例:user50一的一方      order50是多的一方 首先是实体类: 这里的实体是双向关系,既通过user50可以找到order50,通过order50可以找到 ...

  4. 自定义CoordinatorLayout Behavior 隐藏Footer View

    在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...

  5. [转]JAVA设计模式之单例模式

    原文地址:http://blog.csdn.net/jason0539/article/details/23297037 概念: java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主 ...

  6. jQuery常用的元素查找方法总结

    $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...

  7. servlet注解@PostConstruct与@PreDestroy

    从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的vo ...

  8. Android UI性能优化实战, 识别View中的性能问题

    出自:[张鸿洋的博客]来源:http://blog.csdn.net/lmj623565791/article/details/45556391 1.概述 2015年初google发布了Android ...

  9. HTTP协议学习--- (十一)理解HTTP幂等性

    在httpcomponent 文档中看到如下段落: 1.4.1. HTTP transport safety It is important to understand that the HTTP p ...

  10. perl push an array to hash

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array=qw /fm1 fm2 fm3 fm4 fm5 fm6/; ...