zoukankan      html  css  js  c++  java
  • [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)

    [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)

     

    Date  周六 10 一月 2015 By  钟谢伟

     

    Category website development

     

    Tags asp.net / mvc4

     

     

    相关资源

    1. ibatis manual
    2. pro git
    3. 廖雪峰的官方网站
    4. BookMS-V1.0
    5. 上一篇链接

    任务简介

    开发工具:VS2010 项目框架:MVC4 浏览器:Chrome 数据库ORM框架:iBatis.net 数据库:mysql 后端开发语言:c# 前端开发语言:js,html5,css3 功能需求:1. 帐号登入验证 2. 书籍信息录入 3. 书籍信息查询,列表展示 4. 书籍信息更新修改 项目管理:git

    MVC4开发基本介绍

    见上一篇文章:visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一)

    git版本控制基本介绍

    版本控制采用git,在visual studio上使用git可以参考:blog.csdn.net/leichelle/article/details/8002636

    关于git操作的详细内容可以参见:廖雪峰的官方网站

    常见的git操作

    • 创建版本库

      git init

    • 设定版本库不关心的文件

      新建 .gitignore 然后在文件中添加不需要的文件格式,如 *.exe

    • 将文件放到版本库门口

      git add file.txt or git add *.txt or git add ./ # 添加当前目录下的所有文件

    • 查看当前版本库的状态

      git status # 能够看到,哪些东西离版本库的门口还很远,哪些东西已经放到了版本库的门口

    • 将放在版本库门口的文件,送到版本库里面

      git commit -m "add file.txt" # 引号中的内容,用来说明,这一次你将哪些东西,从门口放到了内部

    • 比较修改的文件和原来放在版本库中的文件有什么区别

      git diff file.txt

    • 查看版本库中已经放了多少个版本

      git log --pretty=oneline # 其中很长的一串数据表示commit id

    • 退回到上个的版本

      git reset --hard HEAD^

    • 退回到特定版本

      git reset --hard 3628164 # 如果忘记了版本号,可以通过git reflog查看

    ibatis设定

    建立与图书管理系统相关的数据库,以及数据表,并且设定ibatis相关的config文件,数据操作映射的xml文件等。

    数据库以及表的建立如下,同时插入示例数据:

    create database if not exists `bookmanagementsystem`;
    
    create table if not exists `BOOK`  (
    `BK_ID` bigint not null auto_increment,
    `BK_TITLE` varchar(255) not null,
    `BK_AUTHOR` varchar(100) not null,
    `BK_IS_READED` bool not null default false,
    `BK_PUBLISHER` varchar(255) not null,
    `BK_PUBLISH_TIME` datetime not null,
    `BK_DESCRIPTION` text not null,
    `BK_ISBN` varchar(100) not null,
    `BK_PRICE` decimal not null,
    primary key(BK_ID)
    );
    
    # second insert example elements
    
    insert into `BOOK` (
    `BK_TITLE`,              # 书籍的标题
    `BK_AUTHOR`,             # 书籍的作者
    `BK_PUBLISHER`,          # 书籍的出版社
    `BK_PUBLISH_TIME`,       # 书籍出版时间
    `BK_DESCRIPTION`,        # 书籍简介
    `BK_ISBN`,               # 书籍ISBN号
    `BK_PRICE`,              # 书籍的价格
    )
    values (
    "中国古代文学作品选简编",
    "袁世硕",
    "中国人民大学出版社",
    "2015-01-01",
    "本书是为配合中国文学史课的敎学,按照歷史的顺序,选注自先秦至二十世纪五四新文学运动发生,歷代主要文体的优秀作品二百多家近千篇。入选篇目以经过歷史选择的传世之作为主,注意突出在文学史上占有重要地位的作家的代表作,兼顾到不同的流派、风格,以便与中国文学史的敎材相呼应,体现出中国古代文学及其演变的风貌。第一版畅销多年,此次修订根据使用师生意见,增加了古代小说选篇,以使作品门类更加完整。",
    "978-7-300-20262-4",
    98.00
    ),(
    "骨与关节影像学",
    "陈克敏,陆勇",
    "上海科学技术出版社",
    "2015-01-01",
    "本书由我国骨放射学领域的著名专家和中靑年专家在总结自身实践经验和近年来新进展的基础上编写而成。本书囊括了几乎所有骨骼肌肉疾病的影像学知识,配有2000余幅黑白照片,重点论述各种骨骼肌肉疾病的病理、X线、CT、MRI、造影等综合影像的诊断和鉴别诊断。本书以近10年影像诊断的进展为主,也有多年积累的少见骨骼肌肉疾病的珍贵资料,图文并茂,可为国内医学影像硏究、敎学及培训中靑年影像医务人员提供全面系统的资料,供放射科、骨科及其他临床科室医务人员参考。",
    "978-7-5478-2309-5",
    248.00
    );
     

    起步

    建立新mvc4项目,命名为BookMS,选择Internet Application模版,并勾选添加Unit test选项。然后,将相应的ibatis配置文件设置好,然后将解决方案中的所有内容添加到git项目中,git commit -m "initial project",其中obj文件夹并不包含在git项目中,即,在.gitignore文件中添加obj,如果还有其他额外的文件不需要包含到项目中,添加到.gitignore文件即可。

    Model建立

    默认的模版中在Models文件夹下会有AccountModels.cs,但是我们这里暂时不考虑关于 用户登入管理相关内容,于是,在此略过。 在后续文章中会介绍相关的内容。

    建立对应的Book.cs用来和数据表中的字段对应,即,数据模型。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    
    namespace BookMS.Models
    {
        public class Book
        {
            public int Id { get; set; }
    
            [Required(ErrorMessage="Title is required")]
            [StringLength(255)]
            public string Title { get; set; }
    
            [Required(ErrorMessage = "Title is required")]
            [StringLength(100)]
            public string Author { get; set; }
    
            [Required]
            public bool IsReaded { get; set; }
    
            [Required(ErrorMessage = "Publisher is required")]
            [StringLength(255)]
            public string Publisher { get; set; }
    
            [Required(ErrorMessage = "Publish Time is required")]
            public DateTime PublishTime { get; set; }
    
            [Required(ErrorMessage = "Description is required")]
            public string Description { get; set; }
    
            [Required(ErrorMessage = "ISBN is required")]
            [StringLength(100)]
            public string ISBN { get; set; }
    
            [Required(ErrorMessage = "Price is required")]
            [DataType(DataType.Currency)]
            public decimal Price { get; set; }
        }
    }

    控制器(Controller)创建

    命名为BooksController.cs。根据最初的功能需求可知,需要完成的有:1. 帐号登入验证 2. 书籍信息录入 3. 书籍信息查询,列表展示 4. 书籍信息更新修改

    除了第一项是和帐号登入相关,其他几项都是和书籍相关的,也就是需要两个控制器。一个是 AccountController.cs,另一个是BooksController.cs。

    关于帐号登入验证的内容将在后续文章中介绍

    从需求中知,BooksController控制器中的方法,需要有:

    • Create [get|post] 书籍信息录入
    • SearchList [get] 书籍信息查询,以及列表展示
    • List [get] 书籍信息列表展示
    • Edit [get|post] 书籍信息更新修改
    • Details [get] 查看书籍信息详细内容
    • Delete [get|post] 删除书籍信息

    我是一个新手,所以一开始在具体实现的时候,最容易选择的方式是,直接在相应的控制器的action中对数据库进行操作。

    首先添加List action, 注意添加引用:using IBatisNet.DataMapper; using BookMS.Models;。

    public ActionResult List()
    {
        IList<Book> books = Mapper.Instance().QueryForList<Book>(
            "Book.BKSelectAll", null);
        return View(books);
    }

    其中BKSelectAll,表示的是Book.xml中与数据库操作相对应的标示符。 注意:BKSelectAll前面的sqlMap的Book表示命名空间,因为在SqlMap.config中将useStateNamespace设置成了true

    <!-- in Book.xml -->
    <select id="BKSelectAll" resultMap="BKSelectResult">
      select * from BOOK
    </select>

    对应的View视图很容易实现,先将项目build一下,然后右键添加View,选择Create a strongly-type view,并选择model class为Book,scaffold template选择为list,确认即可。

    运行后输出的结果如下:

    book list

    接着添加,create action

    [HttpGet]
    public ActionResult Create()
    {
        return View();
    }
    
    [HttpPost]
    public ActionResult Create(Book book)
    {
        if (ModelState.IsValid)
        {
            Mapper.Instance().Insert("Book.BKInsert", book);
            return RedirectToAction("List");
        }
    
        ModelState.AddModelError("", "创建图书信息出现错误");
        return View();
    }
     

    添加delete action,edit action,detail action,具体的代码就不再下面给出了,可以直接从 源代码附件中查看。另外,对应的view可以分别借助scaffold template中的create,delete,edit,detail建立。

    最后一个功能就是书籍信息的查询,在List视图中,添加两个用于输入查找信息的输入框,这里设定能够通过书名(Title)和作者名(Author)查询书籍信息,即:

    <!-- in List.cshtml -->
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    @using (Html.BeginForm("SearchList", "Books", FormMethod.Get))
    {
        <p>
        Title: @Html.TextBox("sTitle")
        Author: @Html.TextBox("sAuthor")
        <input type="submit" value="Search" />
        </p>
    }
     

    另外在BooksController中添加SearchList control,并且修改List control,如下:

    public ActionResult List(IList<Book> books = null)
    {
        if (books == null)
            books = Mapper.Instance().QueryForList<Book>("Book.BKSelectAll", null);
        return View(books);
    }
    
    public ActionResult SearchList(string sTitle, string sAuthor)
    {
        IList<Book> books = Mapper.Instance().QueryForList<Book>("Book.BKSelectByTitleAuthor",
            new Book { Title = "+sTitle+", Author = "+sAuthor+" });
        return View("List", books);
    }

    其中,BKSelectByTitleAuthor对应的statement为:

    <select id="BKSelectByTitleAuthor" resultMap="BKSelectResult" parametersClass="Book">
          select * from BOOK where 
            BK_TITLE like #Title# and 
            BK_AUTHOR like #Author#
    </select> 

    至此,拥有最基本功能(不含有用户帐号控制的),同时也是非常粗略的图书管理系统就此完成了,为了在运行后的网页能够直接调转到:Books/List。需要在HomeController.cs中的Index方法内,将View()改为:RedirectToAction("List", "Books")

    下一篇预告

    对建立的《图书管理系统》尝试进行如下优化:

    • 数据库优化
    • 代码优化
    • 建立单元测试
  • 相关阅读:
    METHODS OF AND APPARATUS FOR USING TEXTURES IN GRAPHICS PROCESSING SYSTEMS
    Display controller
    Graphics processing architecture employing a unified shader
    Graphics-Processing Architecture Based on Approximate Rendering
    Architectures for concurrent graphics processing operations
    Procedural graphics architectures and techniques
    DYNAMIC CONTEXT SWITCHING BETWEEN ARCHITECTURALLY DISTINCT GRAPHICS PROCESSORS
    Thermal zone monitoring in an electronic device
    System and method for dynamically adjusting to CPU performance changes
    Framework for Graphics Animation and Compositing Operations
  • 原文地址:https://www.cnblogs.com/grass-and-moon/p/4215545.html
Copyright © 2011-2022 走看看