MVC5+Ibatis 入门demo

本文介绍如何通过iBatis框架配置数据库连接,并在ASP.NET MVC应用中实现基于SQL映射的用户登录验证功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建项目

一、创建空的解决方案

文件→新建→项目


如上图选择空白解决方案。

二、创建mvc5网站项目

右键解决方案→添加→新建项目


如上图,创建mvc5项目

三、创建数据操作项目(类库)

右键解决方案→添加→新建项目→类库

四、创建实体项目(类库)

右键解决方案→添加→新建项目→类库

 

项目创建完成之后如下图:


创建实体

右键IbatisLearn.Entity项目,添加类User



类内容:

namespace IbatisLearn.Entity

{

    publicclassUser

    {

        publicint UserID {get;set; }

        publicstring UserName{get; set; }

        publicstring UserPwd{get; set; }

    }

}

连接数据库

一、右键IbatisLearn.WebUI项目,添加providers.config文件。

内容如下:

<?xmlversion="1.0"encoding="utf-8"?>

<providers

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

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

  <clear/>

  <provider

     name="sqlServer2008"

     enabled="true"

     description="Microsoft SQLServer, provider V2.0.0.0 in framework .NET V2.0"

     assemblyName="System.Data,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

     connectionClass="System.Data.SqlClient.SqlConnection"

     commandClass="System.Data.SqlClient.SqlCommand"

     parameterClass="System.Data.SqlClient.SqlParameter"

     parameterDbTypeClass="System.Data.SqlDbType"

     parameterDbTypeProperty="SqlDbType"

     dataAdapterClass="System.Data.SqlClient.SqlDataAdapter"

     commandBuilderClass="System.Data.SqlClient.SqlCommandBuilder"

     usePositionalParameters = "false"

     useParameterPrefixInSql = "true"

     useParameterPrefixInParameter = "true"

     parameterPrefix="@"

     allowMARS="true"

     bulkCopyClass="System.Data.SqlClient.SqlBulkCopy"

    />

</providers>

二、右键IbatisLearn.WebUI项目,添加SqlMap.config文件。内容如下:

<?xmlversion="1.0" encoding="utf-8"?>

<sqlMapConfigxmlns="http://ibatis.apache.org/dataMapper"

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

  <!--BAsic Setting About Configuration-->

  <settings>

    <setting useStatementNamespaces="false"/>

    <setting cacheModelsEnabled="true"/>

  </settings>

  <providers resource="providers.config"/>

  <!--DataBase Connection Configuration-->

  <database>

    <provider name="sqlServer2008"/>

    <dataSource name="iBatisNet" connectionString="DataSource =.;Initial Catalog =MovieDB;Min Pool Size=0;Max PoolSize=100;Pooling=true;User ID=sa;Password=aaa4685128;"/>

  </database>

  <sqlMaps>

    <!--登录验证-->

    <sqlMap resource="SQLMap/User.xml"/>

  </sqlMaps>

</sqlMapConfig>

三、右键IbatisLearn.WebUI项目,添加SQLMap文件夹,并在该文件夹中添加User.xml文件。内容如下:

 

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

 

<sqlMap namespace="User"xmlns="http://ibatis.apache.org/mapping"

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

 <alias>

   <typeAlias alias="User" type="IbatisLearn.Entity.User,IbatisLearn.Entity"/>

 </alias>

 

 <resultMaps>

   <resultMap id="UserMap" class="User">

      <result property="UserID" column="UserID"/>

      <result property="UserName" column="UserName"/>

      <result property="UserPwd" column="UserPwd"/>

    </resultMap>

 </resultMaps>

 

 <statements>

   <!--登录验证-->

   <select id="LoginVerify" parameterClass="Hashtable"resultMap="UserMap">

      SELECT UserID, UserName, UserPwd FROM[User] where UserName = #UserName# and UserPwd = #UserPwd#

    </select>

 </statements>

</sqlMap>

 

注: ①elect id="LoginVerify"中的LoginVerify必须是所有系统中唯一的名称,否则ibatis不能解析并报错。

MovieDB数据库中必须已经存在User



创建登录方法

右键IbatisLearn.Data项目,添加类Login类:

using IbatisLearn.Entity;

using System.Collections;

 

namespace IbatisLearn.Data

{

    publicclassLogin

    {

        publicintLoginOpt(string pUserName,string pUserPwd)

        {

            Hashtablehashtable=newHashtable();

            hashtable["UserName"]= pUserName;

            hashtable["UserPwd"]= pUserPwd;

            User user=Mapper.Instance().QueryForObject<User>("LoginVerify", hashtable);

            if (user!=null)

                return user.UserID;

            else

            return-1;

        }

    }

}

添加logincontroller

右键IbatisLearn.WebUI项目中的Controllers文件夹,添加LoginController

using IbatisLearn.Data;

using IbatisLearn.Entity;

 

namespace IbatisLearn.WebUI.Controllers

{

    publicclassLoginController :Controller

    {

        publicstring  Index(User pUser)

        {

            Login login=newLogin();

            int ret= login.LoginOpt("张三","123456");

 

            if (ret>=0)

            {

                return"登录成功";

            }

            else

            {

                return"登录失败";

            }

        }

       }

 

调试验证

点击按钮,运行网站,在浏览器中将url修改为

查看结果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值