• Ibatis学习3--操作数据库(插入删除更新修改)


    <?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">
      <resultMaps>
        <resultMap id="blogcontent" Class="StudyDemo.Model.Blog">
          <result property="Id" column="Id" />
          <result property="No" column="TestNo"/>
          <result property="Title" column="TestTitle"/>
          <result property="Content" column="TestContent"/>
        </resultMap>
      </resultMaps>
      <statements>
        <sql id="order">
          order by Id desc
        </sql>
        <select id="selectAllBlog" resultMap="blogcontent">
          select * from blogcontent
          <inclued refid="order"/>
        </select>
        <select id="selectAllBlogOrderbyNodesc" resultMap="blogcontent" parameterClass="StudyDemo.Model.Blog" extends="selectAllBlog">
          where TestNo =#No#
          <include refid="order"/>
        </select>
        <insert id="InsertBlogContent" resultMap="blogcontent">
          Insert Into BlogContent (TestNo,TestTitle,TestContent) values(#No#,#Title#,#Content#)
          <selectKey  type="post" resultClass="int" property="Id">
            select CAST(@@IDENTITY as int) as Id
          </selectKey >
        </insert>
        <update id="UpdateBlogContent" resultMap="blogcontent">
          update blogcontent set testNo=#No#,TestTitle=#Title#,TestContent=#Content# where Id=#Id#
        </update>
        <delete id="deleteBlogContent" resultMap="blogcontent">
          delete from blogcontent where id=#Id#
        </delete>
      </statements>
    </sqlMap>

    以上是映射配置文件

     下面是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="../../config/properties.config"/>
      <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=Test"/>
      </database>
      <sqlMaps>
        <sqlMap  resource="../../config/test/BlogContent.xml"/>
      </sqlMaps>
    </sqlMapConfig>

    代码Dao文件

    using System;
    using System.Collections.Generic;
    using IBatisNet.DataMapper;
    using IBatisNet.DataMapper.Configuration;
    using StudyDemo.Model;
    
    namespace StudyDemo.Dao
    {
        public class BlogContentDao
        {
            protected ISqlMapper _Mapper;
            public BlogContentDao(string sqlMappType = "")
            {
    
                if (sqlMappType.Equals("Test",StringComparison.OrdinalIgnoreCase))
                {
                    var builder = new DomSqlMapBuilder();
                    _Mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                }
                else
                {
                    _Mapper = Mapper.Instance();
                }
    
            }
            public IList<BlogContent> GetList()
            {
                ISqlMapper mapper = Mapper.Instance();
                IList<BlogContent> listBlogContent = mapper.QueryForList<BlogContent>("SelectAllBlogContenty", null);
                return listBlogContent;
            }
    
            public IList<BlogContent> GetBlogContents()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/SqlMap.config");
                IList<BlogContent> listBlgContent = mapper.QueryForList<BlogContent>("SelectAllBlogContent", null);
                return listBlgContent;
            }
            public IList<Blog> GetBlogContentlist()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlogOrderbyNodesc", null);
                return listBlgContent;
            }
            public IList<Blog> GetBlogContentsTest()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlog", null);
                return listBlgContent;
            }
    
            public IList<Blog> GetBlogContentsOrderbyNoDesc()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                Blog t = new Blog() { No = "01" };
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlogOrderbyNodesc", t);
                return listBlgContent;
            }
            public IList<Blog> GetBlogContentsOrderbyNoDesc1()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                Blog t = new Blog() { No = "01" };
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlogOrderbyNodesc1", t);
                return listBlgContent;
            }
    
            public int InsertBlogContent(Blog b)
            {
                var builder = new DomSqlMapBuilder();
                var mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                object test = mapper.Insert("InsertBlogContent", b);
                return (int)test;
            }
            public void UpdateBlogContent(Blog b)
            {
                var builder = new DomSqlMapBuilder();
                var mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                var t = mapper.Update("UpdateBlogContent", b);
            }
    
            public void DeleteBlogContent(Blog b)
            {
                _Mapper.Delete("deleteBlogContent", b);
            }
        }
    }

    调用测试代码

    using System;
    using StudyDemo.Dao;
    using StudyDemo.Model;
    
    namespace StudyDemo
    {
        class Program
        {
            private static void Main(string[] args)
            {
                #region test select
                //{
                //    var dao = new BlogContentDao();
                //    var listBlogContent = dao.GetBlogContentsTest();
                //    foreach (var blogContent in listBlogContent)
                //    {
                //        Console.WriteLine(blogContent.Title + "---" + blogContent.Content);
                //    }
                //}
                //{
                //    var dao = new BlogContentDao();
                //    var listBlogContent = dao.GetBlogContents();
                //    foreach (var blogContent in listBlogContent)
                //    {
                //        Console.WriteLine(blogContent.TestTitle + "---" + blogContent.TestContent);
                //    }
                //}
                //{
                //    var dao = new BlogContentDao();
                //    var listBlogContent = dao.GetBlogContentsOrderbyNoDesc();
                //    foreach (var blogContent in listBlogContent)
                //    {
                //        Console.WriteLine(blogContent.Title + "---" + blogContent.Content);
                //    }
                //    Console.ReadKey();
                //}
                #endregion
    
                #region test insert
    
                //{
                //    var dao = new BlogContentDao();
                //    var insertblog = new Blog()
                //    {
                //        No = "04",
                //        Title = "testInsert",
                //        Content = "testContent"
                //    };
                //    var r = dao.InsertBlogContent(insertblog);
                //    Console.WriteLine(r);
                //    Console.ReadKey();
                //}
    
                #endregion
    
                #region test update
                //{
                //    var dao = new BlogContentDao();
                //    var insertblog = new Blog()
                //    {
                //        Id = 5,
                //        No = "05",
                //        Title = "testInsert5",
                //        Content = "testContent5"
                //    };
                //    dao.UpdateBlogContent(insertblog);
                //    Console.ReadKey();
                //}
                #endregion
    
                #region test delete
    
                {
                    var dao = new BlogContentDao("test");
                    var deleteBlog = new Blog() {Id=4};
                    dao.DeleteBlogContent(deleteBlog);
                }
                #endregion
            }
        }
    }
  • 相关阅读:
    memcached使用入门
    winform代码生成器(三)
    Spark + sbt + IDEA + HelloWorld + MacOS
    CentOS下Hive搭建
    36. 有效的数独
    HADOOP依赖
    判别数字图片能否「一笔完成」
    【网易微专业】图表绘制工具Matplotlib
    【18.065】Lecture2
    【18.065】Lecture1
  • 原文地址:https://www.cnblogs.com/liwenchaoCode/p/5811330.html
Copyright © 2020-2023  润新知