Ibatis学习 1

参考博客地址:http://www.cnblogs.com/kissdodog/p/3291760.html 

使用工具Vs2012+SqlServer2008

1、新建控制台程序 SudyDemo 使用NuGet 安装MyBatis.net

安装后系统会多出下面两个引用

  •   IbatisNet.Common.dll
  •   IbatisNet.DataMapper.dll

2 建立数据库表  连接数据库 新建数据库 MVC5 新建用户表BlogContent

Create table BlogContent(
Id int identity (1,1) primary key,
TestNo varchar(50),
TestTitle varchar(100),
TestContent text
);

INSERT INTO BlogContent(TestNo,TestTitle,TestContent) VALUES('01','连数据','打开SQLServer 2008')
INSERT INTO BlogContent(TestNo,TestTitle,TestContent) VALUES('02','建项目','新建一个控制台程序如下')
INSERT INTO BlogContent(TestNo,TestTitle,TestContent) VALUES('03','建表','Create table BlogContent(Id int identity (1,1) primary key,TestNo varchar(50),TestTitle varchar(100),TestContent text);')

 3、构建实体类及DAO数据处理层类

     创建新文件Model 下创建BlogContent类

     

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudyDemo.Model
{
    public class BlogContent
    {
        public int Id { get; set; }
        public string TestNo { get; set; }
        public string TestTitle { get; set; }
        public string TestContent { get; set; }
    }
}
View Code

   创建Dao文件,创建数据库操作类

     

using System.Collections.Generic;
using IBatisNet.DataMapper;
using StudyDemo.Model;

namespace StudyDemo.Dao
{
    public class BlogContentDao
    {
        public IList<BlogContent> GetList()
        {
            ISqlMapper mapper = Mapper.Instance();
            IList<BlogContent> listBlogContent = mapper.QueryForList<BlogContent>("selectAllContent", null);
            return listBlogContent;
        }
    }
}
View Code

   4、 配置首先配置SqlMap 当然所有的配置文件要放到程序的运行目录

    sqlmap配置文件如下

    

<?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="false"/>
</settings>
<providers resource="providers.config"/>
<database>
<provider name="sqlServer2008"/>
<dataSource name="test" connectionString="server=GSHC-LIWC8;uid=sa; pwd=111111;database=MVC5"/>
</database>
<sqlMaps>
<sqlMap resource="BlogContent.xml"/>
</sqlMaps>
</sqlMapConfig>

  providers.config  配置文件如下

   

<?xml version="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"
    default="true"
    description="Microsoft SQL Server, provider V4.0.0.0 in framework .NET V4.0"
    assemblyName="System.Data, Version=4.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"
    />
</providers>

BlogContent.xml

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
<statements>
<select id="SelectAllBlogContenty" resultClass="StudyDemo.Model.BlogContent">
<!--这个Id在程序中会用到,resultClass是类所在的程序位置(带命名空间)-->
select * from BlogContent
</select>
</statements>
</sqlMap>

以上三个配置文件全部放到bin/Debug目录下

 以下测试代码看能否取出数据

     测试代码为

  

    

using System;
using StudyDemo.Dao;

namespace StudyDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var dao = new BlogContentDao();
            var listBlogContent = dao.GetList();
            foreach (var blogContent in listBlogContent)
            {
                Console.WriteLine(blogContent.TestTitle + "---" + blogContent.TestContent);
            }

            Console.ReadKey();
        }
    }
}
View Code

   5、出现以下问题一般是配置问题检查配置是否正确 注意配置文件中的大小写问题

   

  

    

 6、最后会打印出数据库保存的数据

    

原文地址:https://www.cnblogs.com/liwenchaoCode/p/5809747.html