ibatis学习

一、新建解决方案,里面包含3个项目:

1.Server-类库--要引用IbatisDemo

ibatis学习

包含类:Mapper.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Server

{

    class Mapper

    {

        private static ISqlMapper sqlMap;

        public Mapper()

        {

            Assembly assembly = Assembly.Load("IbatisDemo");

            Stream stream = assembly.GetManifestResourceStream("IbatisDemo.sqlmap.config");

 

            DomSqlMapBuilder builder = new DomSqlMapBuilder();

            sqlMap = builder.Configure(stream);

        }

        public ISqlMapper SqlMap

        {

            get

            {

                return sqlMap;

            }

        } 

    }

}

 

PersonDB.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using IbatisDomain.Domain;

using Server;

 

namespace IbatisServer.Server

{

    public class PersonDB : Mapper

    {

 

        public IList<Person> GetNewsList()

        {

            IList<Person> list = SqlMap.QueryForList<Person>("FindAll", null);

            return list;

        }

 

        public void Update(Person person)

        {

            SqlMap.Update("UpdatePerson", person);

        }

 

    }

}

 

2.Domain---类库

包含:Person.cs

//==============================================================================

// CAUTION: This file is generated by IBatisNetGen.Entity.cst at 2010-1-26 13:56:34

// Any manual editing will be lost in re-generation.

//==============================================================================

using System;

using System.Collections.Generic;

using System.Text;

 

namespace IbatisDomain.Domain

{

 

    /// <summary><c>Person</c> Business Object.</summary>

    [Serializable]

    public partial class Person

    {

 

        #region PERID

 

        private Int32 m_pERID;

 

        /// <summary>Gets or sets PERID</summary>

        public Int32 PERID

        {

            get { return m_pERID; }

            set { m_pERID = value; }

        }

 

        #endregion

 

        #region PERFIRSTNAME

 

        private String m_pERFIRSTNAME;

 

        /// <summary>Gets or sets PERFIRSTNAME</summary>

        public String PERFIRSTNAME

        {

            get { return m_pERFIRSTNAME; }

            set { m_pERFIRSTNAME = value; }

        }

 

        #endregion

 

        #region PERLastNAME

 

        private String m_pERLastNAME;

 

        /// <summary>Gets or sets PERLastNAME</summary>

        public String PERLastNAME

        {

            get { return m_pERLastNAME; }

            set { m_pERLastNAME = value; }

        }

 

        #endregion

 

        #region PERBIRTHDATE

 

        private DateTime? m_pERBIRTHDATE;

 

        /// <summary>Gets or sets PERBIRTHDATE</summary>

        public DateTime? PERBIRTHDATE

        {

            get { return m_pERBIRTHDATE; }

            set { m_pERBIRTHDATE = value; }

        }

 

        #endregion

 

        #region PERWEIGHTKG

 

        private Double? m_pERWEIGHTKG;

 

        /// <summary>Gets or sets PERWEIGHTKG</summary>

        public Double? PERWEIGHTKG

        {

            get { return m_pERWEIGHTKG; }

            set { m_pERWEIGHTKG = value; }

        }

 

        #endregion

 

        #region PERHEIGHTM

 

        private Double? m_pERHEIGHTM;

 

        /// <summary>Gets or sets PERHEIGHTM</summary>

        public Double? PERHEIGHTM

        {

            get { return m_pERHEIGHTM; }

            set { m_pERHEIGHTM = value; }

        }

 

        #endregion

 

 

    }

 

}

 

 

3.IbatisDemo--web appcation

包含:

ibatis学习

Person.xml:

<?xml version="1.0" encoding="utf-8" ?> 

<!--============================================================================

// CAUTION: This file is generated by IBatisNetGen.BatisMap.cst at 2010-1-26 13:56:34

// Any manual editing will be lost in re-generation.

//===========================================================================-->

<sqlMap namespace="Person"

xmlns="http://ibatis.apache.org/mapping" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

 

<alias>

<typeAlias alias="Person" assembly="" type="IbatisDomain.Domain.Person,IbatisDomain" />

</alias>

 

<resultMaps>

<resultMap id="FullResultMap" class="Person">

<result property="PERID" column="PER_ID" dbType="Int"/>

<result property="PERFIRSTNAME" column="PER_FIRST_NAME" dbType="NVarChar"/>

<result property="PERLastNAME" column="PER_Last_NAME" dbType="NVarChar"/>

<result property="PERBIRTHDATE" column="PER_BIRTH_DATE" dbType="DateTime"/>

<result property="PERWEIGHTKG" column="PER_WEIGHT_KG" dbType="Float"/>

<result property="PERHEIGHTM" column="PER_HEIGHT_M" dbType="Float"/>

</resultMap>

</resultMaps>

 

<statements>

 

<select id="GetCount" resultClass="System.Int32">

SELECT count(*) FROM [dbo].[Person]

</select>

 

<select id="FindAll" resultMap="FullResultMap">

SELECT * 

FROM [dbo].[Person]

</select>

 

<select id="Find" parameterClass="Person" resultMap="FullResultMap" extends="FindAll">

WHERE

([Person].[PER_ID] = #PERID,dbType=Int#)

</select>

 

<select id="FindByPERFIRSTNAME" parameterClass="String" resultMap="FullResultMap" extends="FindAll">

WHERE ([Person].[PER_FIRST_NAME] = #value#)

</select>

    

<select id="FindByPERLastNAME" parameterClass="String" resultMap="FullResultMap" extends="FindAll">

WHERE ([Person].[PER_Last_NAME] = #value#)

</select>

    

<select id="FindByPERBIRTHDATE" parameterClass="DateTime" resultMap="FullResultMap" extends="FindAll">

WHERE ([Person].[PER_BIRTH_DATE] = #value#)

</select>

    

<select id="FindByPERWEIGHTKG" parameterClass="Double" resultMap="FullResultMap" extends="FindAll">

WHERE ([Person].[PER_WEIGHT_KG] = #value#)

</select>

    

<select id="FindByPERHEIGHTM" parameterClass="Double" resultMap="FullResultMap" extends="FindAll">

WHERE ([Person].[PER_HEIGHT_M] = #value#)

</select>

 

<insert id="Insert" parameterClass="Person">

INSERT INTO [dbo].[Person] (

 [PER_ID]

, [PER_FIRST_NAME]

, [PER_Last_NAME]

, [PER_BIRTH_DATE]

, [PER_WEIGHT_KG]

, [PER_HEIGHT_M]

) VALUES (

 #PERID,dbType=Int#

, #PERFIRSTNAME,dbType=NVarChar#

, #PERLastNAME,dbType=NVarChar#

, #PERBIRTHDATE,dbType=DateTime#

, #PERWEIGHTKG,dbType=Float#

, #PERHEIGHTM,dbType=Float#

)

</insert>

 

<update id="Update" parameterClass="Person">

UPDATE [dbo].[Person] SET

 [PER_FIRST_NAME] = #PERFIRSTNAME,dbType=NVarChar#

, [PER_Last_NAME] = #PERLastNAME,dbType=NVarChar#

, [PER_BIRTH_DATE] = #PERBIRTHDATE,dbType=DateTime#

, [PER_WEIGHT_KG] = #PERWEIGHTKG,dbType=Float#

, [PER_HEIGHT_M] = #PERHEIGHTM,dbType=Float#

WHERE

([PER_ID] = #PERID,dbType=Int#)

</update>

 

<delete id="Delete" parameterClass="Person">

DELETE FROM [dbo].[Person]

WHERE

([PER_ID] = #PERID,dbType=Int#)

</delete>

 

<delete id="DeleteByPERFIRSTNAME" parameterClass="String">

DELETE FROM [dbo].[Person] WHERE ([PER_FIRST_NAME] = #value#)

</delete>

<delete id="DeleteByPERLastNAME" parameterClass="String">

DELETE FROM [dbo].[Person] WHERE ([PER_Last_NAME] = #value#)

</delete>

<delete id="DeleteByPERBIRTHDATE" parameterClass="DateTime">

DELETE FROM [dbo].[Person] WHERE ([PER_BIRTH_DATE] = #value#)

</delete>

<delete id="DeleteByPERWEIGHTKG" parameterClass="Double">

DELETE FROM [dbo].[Person] WHERE ([PER_WEIGHT_KG] = #value#)

</delete>

<delete id="DeleteByPERHEIGHTM" parameterClass="Double">

DELETE FROM [dbo].[Person] WHERE ([PER_HEIGHT_M] = #value#)

</delete>

 

<select id="Reload" parameterClass="Person" resultMap="FullResultMap" extends="FindAll">

WHERE ([Person].[PER_ID] = #PERID,dbType=Int#)

</select>

 

    <update id="UpdatePerson" parameterclass="Person">

      <![CDATA[ update Person set

      PER_FIRST_NAME =#PERFIRSTNAME#,

      PER_LAST_NAME =#PERLastNAME#,

      PER_BIRTH_DATE =#PERBIRTHDATE#,

      PER_WEIGHT_KG=#PERWEIGHTKG#,

      PER_HEIGHT_M=#PERHEIGHTM#

      where

      PER_ID = #PERID# ?]]>

    </update>

    

</statements>

</sqlMap>

 

Database.config:

<?xml version="1.0" encoding="utf-8" ?> 

<settings>

<add key="userid" value="sa" />

<add key="password" value="qs@123456" />

<add key="database" value="TestDemo" />

<add key="datasource" value="localhost" />

<add key="selectKey" value="select @@IDENTITY as value" />

<add key="directory" value="Maps" />

<add key="useStatementNamespaces" value="false" />

</settings>

 

 

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IbatisDemo.Domain._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:ScriptManager runat="server" ID="SRITSMANGE"></asp:ScriptManager>

    <asp:Panel ID="Panel4" runat="server" HorizontalAlign="Center">

        <asp:Button ID="AddPackage" runat="server" CssClass="Button" 

             Text="新?增?发ぁ?展1人?" Width="80px"  Visible="false"/>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >

          <ContentTemplate>

              <asp:GridView ID="GV_PackageList" runat="server" AllowPaging="True" 

                  AutoGenerateColumns="False" 

                  onpageindexchanging="GV_PackageList_PageIndexChanging" 

                   PageSize="15" Width="95%" DataSourceID="ObjectDataSource1" 

                  onrowupdating="GV_PackageList_RowUpdating" 

                  onrowediting="GV_PackageList_RowEditing" >

                  <Columns>

                      <asp:BoundField DataField="PERID" HeaderText="PERID" SortExpression="PERID" />

                      <asp:BoundField DataField="PERFIRSTNAME" HeaderText="PERFIRSTNAME" 

                          SortExpression="PERFIRSTNAME" />

                      <asp:BoundField DataField="PERLastNAME" 

                          HeaderText="PERLastNAME" SortExpression="PERLastNAME" />

                      <asp:BoundField DataField="PERBIRTHDATE" HeaderText="PERBIRTHDATE" 

                          SortExpression="PERBIRTHDATE" />

                      <asp:BoundField DataField="PERWEIGHTKG" HeaderText="PERWEIGHTKG" 

                          SortExpression="PERWEIGHTKG" />

                      <asp:BoundField DataField="PERHEIGHTM" HeaderText="PERHEIGHTM" 

                          SortExpression="PERHEIGHTM" />

                          <asp:CommandField ShowEditButton="True" />

                  </Columns>

              </asp:GridView>

        

              <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="IbatisDomain.Domain.Person"

                  SelectMethod="GetNewsList" TypeName="IbatisServer.Server.PersonDB" UpdateMethod ="Update">

              </asp:ObjectDataSource>

        

                <br />

          </ContentTemplate>

        </asp:UpdatePanel>

        

        

    </asp:Panel>

    </div>

    </form>

</body>

</html>

 

Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using IbatisServer.Server;

using IbatisDomain.Domain;

 

 

namespace IbatisDemo.Domain

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            //GV_PackageListBind();

        }

 

        protected void GV_PackageList_PageIndexChanging(object sender, GridViewPageEventArgs e)

        {

            GV_PackageList.PageIndex = e.NewPageIndex;

            GV_PackageListBind();

        }

 

        private void GV_PackageListBind()

        {

            PersonDB perDB = new PersonDB();

            IList<Person> list = perDB.GetNewsList();

            GV_PackageList.DataSource = list;

            GV_PackageList.DataBind();

        }

 

        protected void GV_PackageList_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

 

        }

 

        protected void GV_PackageList_RowEditing(object sender, GridViewEditEventArgs e)

        {

 

        }

    }

}

 

 

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

 

  <!-- Rem : If used via a DataAccess context, properties tag will be ignored 

  <properties resource="http://www.cnblogs.com/database.config"/> -->

  <properties resource="database.config"/>

  <settings>

    <setting useStatementNamespaces="${useStatementNamespaces}"/>

    <setting cacheModelsEnabled="true"/>

    <setting validateSqlMap="false"/>

  </settings>

 

  <!-- Optional if resource -->

  <providers resource="providers.config"/>

  <!-- ==== SqlClient configuration ========= -->

  <!-- Rem : If used via a DataAccess context, database tag will be ignored -->

  <database>

    <!-- Optional ( default ) -->

    <provider name="sqlServer1.1"/>

    <dataSource name="iBatisNet" connectionString="data source=${datasource};database=${database};user id=${userid};password=${password};connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/>

  </database>

 

  <sqlMaps>

    <!-- user via embedded-->

    <!--<sqlMap embedded="Map.SqlClient.Person.xml,IbatisServer"/>-->

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

  </sqlMaps>

</sqlMapConfig>

 

Web.config:

 

<?xml version="1.0"?>

<configuration>

<appSettings/>

<connectionStrings/>

<system.web>

<!-- 

            设置 compilation debug="true" 可将调试符号插入

            已编译的页面中。但由于这会 

            影响性能,因此只在开发过程中将此值 

            设置为 true

        -->

<compilation debug="true" targetFramework="4.0">

</compilation>

<!--

            通过 <authentication> 节可以配置 ASP.NET 用来 

            识别进入用户的

            安全身份验证模式。 

        -->

<authentication mode="Windows"/>

<!--

            如果在执行请求的过程中出现未处理的错误,

            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,

            开发人员通过该节可以配置

            要显示的 html 错误页

            以代替错误堆栈跟踪。

 

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

            <error statusCode="403" redirect="NoAccess.htm" />

            <error statusCode="404" redirect="FileNotFound.htm" />

        </customErrors>

        -->

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>

<!-- 

        在 Internet 信息服务 7.0 下运行 ASP.NET AJAX 需要 system.webServer

        节。对早期版本的 IIS 来说则不需要此节。

    -->

</configuration>

 

引用:

ibatis学习

数据库:

TestDemo

ibatis学习

 

 

 

 

 

 

 

 

你可能感兴趣的:(ibatis)