高校手机签到系统——第一部分Authority权限系统(上)

  序:今天开始写一个算是我第一个系列的文章——高校手机签到系统。本系统结合我们学校自身的一些特点编写。这是我的毕业设计项目,写在这里算是给最后论文的时候一些点滴的记录。另外也想通过这个系列的文章找到一份工作,我知道许多大神都在博客园。我的邮箱:dugukuangshao@Gmail.com,在这个系列文章快完成的时候,我会将该项目开源并附上简历。

      基本思路:根据每堂课唯一的guid标识加上当前时间,生成每5秒刷新一次的图片,再用手机客户端连接到校园网wifi再去扫描这张图片来签到。手机客户端还能从校园网上获得课程表。最后通过签到的数据还能分析课程的到课率、班级出勤率等。

      由于教务处网站没有开放的接口,所以我自己实现了一个选课的系统,根据这个系统来查询课程表。选课网站构成了服务器端,采用.net平台,毕业设计自然要有些难度和挑战,所以手机客户端采用了android平台,也就是java。服务器端使用mvc4模式开发,mybatis.net作为orm的框架。

  第一部分是Authority权限系统。涉及到domain层的Membership和Organization。如图所示:Membership下存在四个类:User用户、Profile详细资料、Role角色、UsersInRoles。Organization下存在三个类:Class班级、Institution机构、UsersInInstitutions。各类之间的关系如图:

代码如下:

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

namespace signin.domain.Membership
{
    public partial class User
    {
        public Guid UserId { get; set; }                            //创建时插入
        public String UserName { get; set; }                        //创建时插入
        public DateTime LastActivityDate { get; set; }              //关闭窗口时插入

        public String Password { get; set; }                        //创建时插入
        public String PasswordSalt { get; set; }                    //创建时插入
        public String Email { get; set; }                           //创建时插入
        public String PasswordQuestion { get; set; }                //创建时插入
        public String PasswordAnswer { get; set; }                  //创建时插入
        public bool IsApproved { get; set; }                        
        public bool IsIsLockedOut { get; set; }                     //密码错误次数超过限度时锁定
        public DateTime CreateDate { get; set; }
        public DateTime LastLoginDate { get; set; }                 //关闭窗口时插入
        public DateTime LastPasswordChangedDate { get; set; }       //修改密码时更新
        public DateTime LastLockoutDate { get; set; }               //密码错误次数超过限度时锁定
        public int FailedPasswordAttemptCount { get; set; }
        public DateTime FailedPasswordAttemptWindowStart { get; set; }
        public int FailedPasswordAnswerAttemptCount { get; set; }
        public DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }
        public String Comment { get; set; }
        public bool IsOnline { get; set; }
    }

    public partial class User
    {
        public string ConfirmPassword { get; set; }
    }
}
User.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using signin.domain.Common;

namespace signin.domain.Membership
{
    public class Profile : User,IPager
    {
        public string No { get; set; }                  //                              创建时插入
        public string Nickname { get; set; }            //                              创建时插入
        public bool Gender { get; set; }                //                              创建时插入
        public int Age { get; set; }                    //                              非数据库字段
        public DateTime Birthday { get; set; }          //生日                          创建时插入    
        public bool Calendar { get; set; }              //历法                          创建时插入
        public string Nation { get; set; }              //民族                          创建时插入
        public string Address { get; set; }
        public string PoliticalStatus { get; set; }     //政治面貌                      创建时插入
        public string CellPhoneNumber { get; set; }     //手机号码                      创建时插入
        public string IMSI { get; set; }                //国际移动用户识别码
        public string ImgLink { get; set; }
        public bool IsBound { get; set; }
        public bool IsUpdated { get; set; }

        #region Pager
        private int pageSize = 20;
        private int pageCount;

        public int CurrentPage { get; set; }
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        public int RecordCount { get; set; }
        public int PageCount
        {
            get
            {
                if (pageSize == 0)
                {
                    return 0;
                }
                pageCount = RecordCount / pageSize;
                return (RecordCount % pageSize == 0) ? pageCount : ++pageCount;
            }
        }
        public int PageIndex { get; set; }
        #endregion
    }
}
Profile.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace signin.domain.Membership
{
    public class Role
    {
        public Guid RoleId { get; set; }
        public string RoleName { get; set; }
        public string Description { get; set; }
    }
}
Role.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace signin.domain.Membership
{
    public class UsersInRoles
    {
        public Guid UserId { get; set; }
        public Guid RoleId { get; set; }
    }
}
UsersInRoles.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace signin.domain.Organization
{
    public class Institution
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public Guid SuperiorId { get; set; }     //上级机构Id
        public bool IsUpdated { get; set; }
    }
}
Institution.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace signin.domain.Organization
{
    public class Class:Institution
    {
        public string ClassCode { get; set; }
        public string Grade { get; set; }       //年级
        public int ClassNo { get; set; }        //班号
        public DateTime StartDate { get; set; }
        public DateTime GraduationDate { get; set; }
        public string Semesters { get; set; }
        public IList<Semester> Sems { get; set; }

        public int getSemesterNo(DateTime date)
        {
            foreach (Semester semester in Sems)
            {
                if (DateTime.Compare(date, semester.StartDate) > 0 && DateTime.Compare(date,semester.EndDate)<0)
                    return semester.No;
            }
            return -1;
        }
    }

    public class Semester
    {
        public int No { get; set; }
        public DateTime StartDate { get; set; }         //开学时间
        public DateTime EndDate { get; set; }
    }
}
Class.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace signin.domain.Organization
{
    public class UsersInInstitutions
    {
        public Guid UserId { get; set; }
        public Guid InstitutionId { get; set; }
    }
}
UsersInInstitutions.cs

 

Profile里的IMSI字段是手机的唯一标识,用来绑定学生的客户端,也就是一部手机只能为一个人签到。Profile继承自User,为什么是继承?Profile和User难道不是Has-A的关系,怎么是IS-A?从User的属性看,User只是一个Account,用户在这个网站上的账户。而,Profile具有完整的一个Person的属性,所以谁属于谁还不好说。这里采用继承,是因为我在查询User的sql语句中采用了联合查询,User和Profile一起返回,在Dao层的方法里使用了泛型,sqlMapper即可根据需要返回我要的User或者Profile。这样做在性能上有多大的影响我也不是太清楚,只是图省事儿,学校的学生老师也不是太多。下面是Mapper的代码:

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="signin.domain" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="User" type="signin.domain.Membership.User,signin.domain"/>
  </alias>
  <statements>
    <insert id="InsertUser" parameterClass="User">
      INSERT INTO [signin].[dbo].[User] ([User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment]) VALUES (#UserId#,#UserName#,getdate(),#Password#,#PasswordSalt#,#Email#,#PasswordQuestion#,#PasswordAnswer#,1,0,getdate(),getdate(),getdate(),CONVERT( datetime, '17540101', 112 ),0,CONVERT( datetime, '17540101', 112 ),0,CONVERT( datetime, '17540101', 112 ),#Comment#)
    </insert>
    <delete id="DeleteUser" parameterClass="User" resultClass="Int">
      DELETE FROM [signin].[dbo].[User] WHERE     [UserId] LIKE '%$UserId$%'
      <isNotEmpty prepend="OR" property="UserName">[UserName] = #UserName#</isNotEmpty>
    </delete>
    <select id="SelectUserById" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[UserId] LIKE '%$UserId$%'
    </select>
    <select id="SelectUserByNo" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[No] = #No#
    </select>
    <select id="SelectUserByUserName" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[UserName] LIKE '%'+#UserName#+'%'
    </select>
    <select id="SelectUserByNickname" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [Profile].[Nickname] LIKE '%'+#Nickname#+'%'
    </select>
    <select id="SelectAllUsers" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId]
    </select>
    <select id="SelectAllStuffs" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[UserId] IN (SELECT [UserId] FROM [signin].[dbo].[UsersInRoles] JOIN [signin].[dbo].[Role] ON [UsersInRoles].[RoleId]=[Role].[RoleId] WHERE [Role].[RoleName]='教职工')
    </select>
    <select id="SelectStuffsByPage" parameterClass="Profile" resultClass="signin.domain.Membership.Profile">
      WITH LIST AS (SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound],ROW_NUMBER() OVER(ORDER BY [No] asc) AS PageIndex,RecordCount = count(1) over() FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[UserId] IN (SELECT [UserId] FROM [signin].[dbo].[UsersInRoles] JOIN [signin].[dbo].[Role] ON [UsersInRoles].[RoleId]=[Role].[RoleId] WHERE [Role].[RoleName]='教职工'))
      SELECT [UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound],[PageIndex],[RecordCount] FROM LIST WHERE PageIndex BETWEEN (#CurrentPage#-1)*#PageSize#+1 AND #CurrentPage#*#PageSize#
    </select>
    <select id="SelectAllStudents" patameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound] FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[UserId] IN (SELECT [UserId] FROM [signin].[dbo].[UsersInRoles] JOIN [signin].[dbo].[Role] ON [UsersInRoles].[RoleId]=[Role].[RoleId] WHERE [Role].[RoleName]='学生')
    </select>
    <select id="SelectStudentsByPage" parameterClass="Profile" resultClass="signin.domain.Membership.Profile">
      WITH LIST AS (SELECT [User].[UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound],ROW_NUMBER() OVER(ORDER BY [No] asc) AS PageIndex,RecordCount = count(1) over() FROM [signin].[dbo].[User] LEFT JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] = [User].[UserId] WHERE [User].[UserId] IN (SELECT [UserId] FROM [signin].[dbo].[UsersInRoles] JOIN [signin].[dbo].[Role] ON [UsersInRoles].[RoleId]=[Role].[RoleId] WHERE [Role].[RoleName]='学生'))
      SELECT [UserId],[UserName],[LastActivityDate],[Password],[PasswordSalt],[Email],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockoutDate],[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment],[IsOnline],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound],[PageIndex],[RecordCount] FROM LIST WHERE PageIndex BETWEEN (#CurrentPage#-1)*#PageSize#+1 AND #CurrentPage#*#PageSize#
    </select>
    <update id="ChangePassword" parameterClass="User" resultClass="Int">
      UPDATE [signin].[dbo].[User] SET [Password] = #Password#,[PasswordSalt] = #PasswordSalt#,[LastPasswordChangedDate] = getdate() WHERE [UserId] LIKE '%$UserId$%'
      <isNotEmpty prepend="OR" property="UserName">[UserName] = #UserName#</isNotEmpty>
    </update>
    <update id="UpdateLoginRecord" parameterClass="User" resultClass="Int">
      UPDATE [signin].[dbo].[User] SET [LastActivityDate] = getdate(),[LastLoginDate] = getdate() WHERE [UserId] LIKE '%$UserId$%'
      <isNotEmpty prepend="OR" property="UserName">[UserName] = #UserName#</isNotEmpty>
    </update>
    <update id="UpdateFailedPasswordRecord" parameterClass="User" resultClass="Int">
      UPDATE [signin].[dbo].[User] SET [FailedPasswordAttemptCount] = [FailedPasswordAttemptCount]+1,[FailedPasswordAttemptWindowStart] = getdate() WHERE [UserId] LIKE '%$UserId$%'
      <isNotEmpty prepend="OR" property="UserName">[UserName] = #UserName#</isNotEmpty>
    </update>
    <update id="UpdateFailedPasswordAnswerRecord" parameterClass="User" resultClass="Int">
      UPDATE [signin].[dbo].[User] SET [FailedPasswordAnswerAttemptCount] = [FailedPasswordAnswerAttemptCount]+1,[FailedPasswordAnswerAttemptWindowStart] = getdate() WHERE [UserId] LIKE '%$UserId$%'
      <isNotEmpty prepend="OR" property="UserName">[UserName] = #UserName#</isNotEmpty>
    </update>
    <select id="IsUserNameExists" patameterClass="String" resultClass="Int">
      SELECT COUNT([UserName]) FROM [signin].[dbo].[User] WHERE [UserName] = #UserName#
    </select>
    <select id="IsEmailExists" patameterClass="String" resultClass="Int">
      SELECT COUNT([Email]) FROM [signin].[dbo].[User] WHERE [Email] LIKE '%'+#Email#+'%'
    </select>
    <select id="GetInstitutionsForUser" parameterClass="String" resultClass="signin.domain.Organization.Institution">
      SELECT Institution.* FROM [Signin].[dbo].[Institution]  JOIN [Signin].[dbo].[PeopleInInstitutions] ON [PeopleInInstitutions].InstitutionId = [Institution].Id   JOIN   [Signin].[dbo].[User] ON [User].UserId = [PeopleInInstitutions].UserId WHERE [User].UserName = #UserName#
    </select>
    <select id="GetClassesForUser" parameterClass="String" resultClass="signin.domain.Organization.Institution">
      SELECT [Institution].[Id],[ClassCode],[Nickname],[SuperiorId],[Grade],[ClassNo],[StartDate],[GraduationDate],[Semesters] FROM [signin].[dbo].[Class] JOIN [signin].[dbo].[Institution] ON [Institution].Id = [Class].Id  JOIN [Signin].[dbo].[PeopleInInstitutions] ON [PeopleInInstitutions].InstitutionId = [Institution].Id   JOIN   [Signin].[dbo].[User] ON [User].UserId = [PeopleInInstitutions].UserId WHERE [User].UserName = #UserName#
    </select>
  </statements>
</sqlMap>
User.Xml
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="signin.domain" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="Profile" type="signin.domain.Membership.Profile,signin.domain"/>
  </alias>
  <statements>
    <insert id="AddProfileToUser" parameterClass="Profile">
      INSERT IntO [signin].[dbo].[Profile] ([UserId],[No],[Nickname],[Gender],[Birthday],[Calendar],[Nation],[Address],[PoliticalStatus],[CellPhoneNumber],[IMSI],[ImgLink],[IsBound]) VALUES (#UserId#,#No#,#Nickname#,#Gender#,#Birthday#,#Calendar#,#Nation#,#Address#,#PoliticalStatus#,#CellPhoneNumber#,#IMSI#,#ImgLink#,0)
    </insert>
    <delete id="RemoveProfileForUser" parameterClass="Profile" resultClass="Int">
      DELETE FROM [signin].[dbo].[Profile] WHERE [UserId] LIKE '%$UserId$%'
      <isNotEmpty prepend="OR" property="No">[No] = #No#</isNotEmpty>
    </delete>
    <update id="UpdateProfileForUser" parameterClass="Profile" resultClass="Int">
      UPDATE [signin].[dbo].[Profile]
      SET [IsUpdated] = 1
      <isNotEmpty prepend="," property="No">[No] = #No# </isNotEmpty>
      <isNotEmpty prepend="," property="Nickname">[Nickname] = #Nickname# </isNotEmpty>
      <isEqual prepend="," property="Gender" compareValue="true">[Gender] = #Gender#</isEqual>
      <isEqual prepend="," property="Gender" compareValue="false">[Gender] = #Gender#</isEqual>
      <isNotNull prepend="," property="Birthday">[Birthday] = #Birthday#</isNotNull>
      <isEqual prepend="," property="Calendar" compareValue="true">[Calendar] = #Calendar#</isEqual>
      <isEqual prepend="," property="Calendar" compareValue="false">[Calendar] = #Calendar#</isEqual>
      <isNotEmpty prepend="," property="Nation">[Nation] = #Nation#</isNotEmpty>
      <isNotEmpty prepend="," property="Address">[Address] = #Address#</isNotEmpty>
      <isNotEmpty prepend="," property="PoliticalStatus">[PoliticalStatus] = #PoliticalStatus#</isNotEmpty>
      <isNotEmpty prepend="," property="CellPhoneNumber">[CellPhoneNumber] = #CellPhoneNumber#</isNotEmpty>
      <isNotEmpty prepend="," property="IMSI">[IMSI] = #IMSI#</isNotEmpty>
      <isNotEmpty prepend="," property="ImgLink">[ImgLink] = #ImgLink#</isNotEmpty>
      <isEqual prepend="," property="IsBound" compareValue="true">[IsBound] = #IsBound#</isEqual>
      <isEqual prepend="," property="IsBound" compareValue="false">[IsBound] = #IsBound#</isEqual> WHERE [UserId] LIKE '%$UserId$%'
    </update>
    <select id="IsProfileNoExists" patameterClass="String" resultClass="Boolean">
      SELECT COUNT([No]) FROM [signin].[dbo].[Profile] WHERE [No] = #No#
    </select>
    <select id="IsProfileIMSIExists" patameterClass="String" resultClass="Boolean">
      SELECT COUNT([IMSI]) FROM [signin].[dbo].[Profile] WHERE [IMSI] LIKE = #IMSI#
    </select>    
  </statements>
</sqlMap>
Profile.xml
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="signin.domain" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="Role" type="signin.domain.Membership.Role,signin.domain"/>
  </alias>
  <statements>
    <insert id="CreateRole" parameterClass="Role">
      INSERT IntO [signin].[dbo].[Role] ([RoleId],[RoleName],[Description]) VALUES (#RoleId#,#RoleName#,#Description#)
    </insert>
    <delete id="DeleteRole" parameterClass="Role" resultClass="Int">
      DELETE FROM [signin].[dbo].[Role] WHERE [RoleId] LIKE '%$RoleId$%'
      <isNotEmpty prepend="OR" property="RoleName">[RoleName] = #RoleName#</isNotEmpty>
    </delete>
    <insert id="AddUserToRole" parameterClass="System.Collections.IDictionary">
      INSERT IntO [signin].[dbo].[UsersInRoles] ([UserId],[RoleId]) SELECT [UserId],[RoleId] FROM [signin].[dbo].[User],[signin].[dbo].[Role] WHERE [UserName] = #UserName# AND [RoleName] = #RoleName#
    </insert>
    <select id="GetUsersInRole" parameterClass="String" resultClass="signin.domain.Membership.User">
      SELECT [signin].[dbo].[User].* FROM [signin].[dbo].[Role] JOIN [signin].[dbo].[UsersInRoles] ON [Role].RoleId = [UsersInRoles].RoleId  JOIN [signin].[dbo].[User] ON [UsersInRoles].UserId = [User].UserId WHERE [Role].RoleName = #RoleName#
    </select>
    <select id="GetRolesForUser" parameterClass="String" resultClass="Role">
      SELECT [signin].[dbo].[Role].* FROM [signin].[dbo].[Role] JOIN [signin].[dbo].[UsersInRoles] ON [Role].RoleId = [UsersInRoles].RoleId  JOIN [signin].[dbo].[User] ON [UsersInRoles].UserId = [User].UserId WHERE [User].UserName = #UserName#
    </select>
    <select id="IsUserInRole" parameterClass="System.Collections.IDictionary" resultClass="Boolean">
      SELECT COUNT([UserName]) FROM [signin].[dbo].[Role] JOIN [signin].[dbo].[UsersInRoles] ON [Role].RoleId = [UsersInRoles].RoleId  JOIN [signin].[dbo].[User] ON [UsersInRoles].UserId = [User].UserId WHERE [Role].RoleName = #RoleName# AND [User].[UserName] = #UserName#
    </select>
    <delete id="RemoveUserFromRole" parameterClass="System.Collections.IDictionary" resultClass="Int">
      DELETE FROM [signin].[dbo].[UsersInRoles] FROM [signin].[dbo].[Role] JOIN [signin].[dbo].[UsersInRoles] ON [Role].RoleId = [UsersInRoles].RoleId JOIN [signin].[dbo].[User] ON [UsersInRoles].UserId = [User].UserId WHERE [Role].RoleName = #RoleName# AND [User].[UserName] = #UserName#
    </delete>
    <select id="RoleExists" parameterClass="String" resultClass="Int">
      SELECT COUNT([RoleName]) FROM [signin].[dbo].[Role] WHERE [RoleName] = #RoleName#
    </select>
    <select id="GetAllRoles" resultClass="Role">
      SELECT * FROM [signin].[dbo].[Role]
    </select>
  </statements>
</sqlMap>
Role.xml
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="signin.domain" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="Institution" type="signin.domain.Organization.Institution,signin.domain"/>
    <typeAlias alias="UsersInInstitutions" type="signin.domain.Organization.UsersInInstitutions,signin.domain"/>
  </alias>
  <statements>
    <insert id="InsertInstitution" parameterClass="Institution">
      INSERT INTO [signin].[dbo].[Institution] ([Id],[Name],[Type]
      <isNotEqual prepend="," property="SuperiorId" compareValue="00000000-0000-0000-0000-000000000000">[SuperiorId]</isNotEqual>)
         VALUES (#Id#,#Name#,#Type#
      <isNotEqual prepend="," property="SuperiorId" compareValue="00000000-0000-0000-0000-000000000000">#SuperiorId#</isNotEqual>)
    </insert>
    <delete id="DeleteInstitution" parameterClass="Institution" resultClass="Int">
      DELETE FROM [signin].[dbo].[Institution] WHERE [Id] LIKE '%$Id$%'
      <isNotEmpty prepend="OR" property="Name">[Name] = #Name#</isNotEmpty>
    </delete>
    <update id="UpdateInstitution" parameterClass="Institution" resultClass="Int">
      UPDATE [signin].[dbo].[Institution] SET [IsUpdated] = 1
      <isNotEmpty prepend="," property="Name">[Name] = #Name#</isNotEmpty>
      <isNotEmpty prepend="," property="Type">[Type] = #Type#</isNotEmpty>
      <isNotEqual prepend="," property="SuperiorId" compareValue="00000000-0000-0000-0000-000000000000">SuperiorId = #SuperiorId#</isNotEqual>WHERE [Id] LIKE '%$Id$%'
    </update>
    <select id="SelectInstitutionById" parameterClass="string" resultClass="Institution">
      SELECT [Id],[Name],[Type],[SuperiorId] FROM [signin].[dbo].[Institution] WHERE [Id] LIKE '%$Id$%'
    </select>
    <select id="SelectInstitutionByName" parameterClass="string" resultClass="Institution">
      SELECT [Id],[Name],[Type],[SuperiorId] FROM [signin].[dbo].[Institution] WHERE [Name] LIKE '%'+#Name#+'%'
    </select>
    <select id="SelectInstitutionsByType" parameterClass="string" resultClass="Institution">
      SELECT [Id],[Name],[Type],[SuperiorId] FROM [signin].[dbo].[Institution] WHERE [Type] LIKE '%'+#Type#+'%'
    </select>
    <select id="SelectInstitutionsBySuperiorId" parameterClass="string" resultClass="Institution">
      SELECT [Id],[Name],[Type],[SuperiorId] FROM [signin].[dbo].[Institution] WHERE [SuperiorId] LIKE '%$SuperiorId$%'
    </select>
    <select id="SelectAllInstitutions" resultClass="Institution">
      SELECT [Id],[Name],[Type],[SuperiorId] FROM [signin].[dbo].[Institution]
    </select>
    <select id="IsInstitutionNameExists" parameterClass="string" resultClass="Int">
      SELECT COUNT([Name]) FROM [signin].[dbo].[Institution] WHERE [Name] = #Name#
    </select>
    
    <insert id="AddUserToInstitution" parameterClass="System.Collections.IDictionary">
      INSERT INTO [signin].[dbo].[UsersInInstitutions] ([UserId],[InstitutionId]) SELECT [UserId],[Id] FROM [signin].[dbo].[Profile],[signin].[dbo].[Institution] WHERE [signin].[dbo].[Profile].[No] = #UserNo# AND [signin].[dbo].[Institution].[Name] = #InstitutionName#
    </insert>
    <select id="GetStuffsInInstitution" parameterClass="string" resultClass="signin.domain.Membership.Profile">
      SELECT [signin].[dbo].[Profile].* FROM [signin].[dbo].[Institution] JOIN [signin].[dbo].[UsersInInstitutions] ON [UsersInInstitutions].[InstitutionId] = [Institution].[Id] JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] =[UsersInInstitutions].[UserId] JOIN [signin].[dbo].[UsersInRoles] ON [UsersInRoles].[UserId] = [Profile].[UserId] JOIN [signin].[dbo].[Role] ON [Role].[RoleId] = [UsersInRoles].[RoleId] WHERE [Institution].[Name] = #Name# AND [Role].[RoleName] = '教职工'
    </select>
    <select id="GetStudentsInClass" parameterClass="String" resultClass="signin.domain.Membership.Profile">
      SELECT [signin].[dbo].[Profile].* FROM [signin].[dbo].[Institution] JOIN [signin].[dbo].[UsersInInstitutions] ON [UsersInInstitutions].[InstitutionId] = [Institution].[Id] JOIN [signin].[dbo].[Profile] ON [Profile].[UserId] =[UsersInInstitutions].[UserId] JOIN [signin].[dbo].[UsersInRoles] ON [UsersInRoles].[UserId] = [Profile].[UserId] JOIN [signin].[dbo].[Role] ON [Role].[RoleId] = [UsersInRoles].[RoleId] WHERE [Institution].[Name] = #Name# AND [Role].[RoleName] = '学生'
    </select>
    <select id="IsUserInInstitution" parameterClass="System.Collections.IDictionary" resultClass="Boolean">
      SELECT COUNT([signin].[dbo].[Profile].[Nickname]) FROM [signin].[dbo].[Institution] JOIN [signin].[dbo].[StuffsInInstitutions] ON [Institution].Id = [StuffsInInstitutions].InstitutionId  JOIN [signin].[dbo].[Profile] ON [StuffsInInstitutions].UserId = [Profile].UserId WHERE [Institution].Name = #InstitutionName# AND [Profile].[No] = #UserNo#
    </select>
    <delete id="RemoveUserFromInstitution" parameterClass="System.Collections.IDictionary" resultClass="Int">
      DELETE FROM [signin].[dbo].[StuffsInInstitutions] FROM [signin].[dbo].[Institution] JOIN [signin].[dbo].[StuffsInInstitutions] ON [Institution].Id = [StuffsInInstitutions].InstitutionId  JOIN [signin].[dbo].[Profile] ON [StuffsInInstitutions].UserId = [Profile].UserId WHERE [Institution].Name = #InstitutionName# AND [Profile].[No] = #UserNo#
    </delete>
  </statements>
</sqlMap>
Institution.xml
<?xml version="1.0" encoding="utf-8" ?> 
<sqlMap namespace="signin.domain" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <alias>
    <typeAlias alias="Class" type="signin.domain.Organization.Class,signin.domain"/>
  </alias>
  <statements>
    <insert id="InsertClass" parameterClass="Class">
      INSERT INTO [signin].[dbo].[Institution] ([Id],[Name],[Type]
      <isNotEqual prepend="," property="SuperiorId" compareValue="00000000-0000-0000-0000-000000000000">[SuperiorId]</isNotEqual>)
      VALUES (#Id#,#Name#,'班级'
      <isNotEqual prepend="," property="SuperiorId" compareValue="00000000-0000-0000-0000-000000000000">#SuperiorId#</isNotEqual>)
      INSERT INTO [signin].[dbo].[Class] ([Id],[ClassCode],[Grade],[ClassNo]
      <isNotEqual prepend="," property="StartDate" compareValue="0001/1/1 0:00:00">[StartDate]</isNotEqual>
      <isNotEqual prepend="," property="GraduationDate" compareValue="0001/1/1 0:00:00">[GraduationDate]</isNotEqual>
      <isNotEmpty prepend="," property="Semester">[Semesters]</isNotEmpty>) VALUES (#Id#,#ClassCode#,#Grade#,#ClassNo#
      <isNotEqual prepend="," property="StartDate" compareValue="0001/1/1 0:00:00">#StartDate#</isNotEqual>
      <isNotEqual prepend="," property="GraduationDate" compareValue="0001/1/1 0:00:00">#GraduationDate#</isNotEqual>
      <isNotEmpty prepend="," property="Semester">#Semesters#</isNotEmpty>)
    </insert>
    <delete id="DeleteClass" parameterClass="Class" resultClass="Int">
      DELETE FROM [signin].[dbo].[Institution],[signin].[dbo].[Class] JOIN [signin].[dbo].[Class] ON [Institution].Id = [Class].Id WHERE [Institution].Id  LIKE '%$Id$%'
      <isNotEmpty prepend="OR" property="Name">[Institution].[Name] = #Name#</isNotEmpty>
      <isNotEmpty prepend="OR" property="No">[Class].[ClassCode] = #ClassCode#</isNotEmpty>
    </delete>
    <update id="UpdateClass" parameterClass="Class" resultClass="Int">
      UPDATE [signin].[dbo].[Institution] SET [IsUpdated] = 1
      <isNotEmpty prepend="," property="Name">[Name] = #Name#</isNotEmpty>
      <isNotEmpty prepend="," property="Type">[Type] = #Type#</isNotEmpty>
      <isNotEqual prepend="," property="SuperiorId" compareValue="00000000-0000-0000-0000-000000000000">SuperiorId = #SuperiorId#</isNotEqual>WHERE [Id] LIKE '%$Id$%'
      UPDATE [signin].[dbo].[Class] SET [IsUpdated] = 1
      <isNotEmpty prepend="," property="No">[ClassCode] = #ClassCode#</isNotEmpty>
      <isNotEmpty prepend="," property="Grade">[Grade] = #Grade#</isNotEmpty>
      <isNotNull prepend="," property="ClassNo">[ClassNo] = #ClassNo#</isNotNull>
      <isNotEqual prepend="," property="StartDate" compareValue="0001/1/1 0:00:00">[StartDate] = #StartDate#</isNotEqual>
      <isNotEqual prepend="," property="GraduationDate" compareValue="0001/1/1 0:00:00">[GraduationDate] = #GraduationDate#</isNotEqual>
      <isNotEmpty prepend="," property="Semester">[Semesters] = #Semesters#</isNotEmpty>
      WHERE [Id] LIKE '%$Id$%'
    </update>
    <select id="SelectClassById" parameterClass="string" resultClass="Class">
      SELECT [Institution].[Id],[ClassCode],[Name],[SuperiorId],[Grade],[ClassNo],[StartDate],[GraduationDate],[Semesters] FROM [signin].[dbo].[Class] JOIN [signin].[dbo].[Institution] ON [Institution].Id = [Class].Id WHERE [Institution].[Id] LIKE '%$Id$%'
    </select>
    <select id="SelectClassByNo" parameterClass="string" resultClass="Class">
      SELECT [Institution].[Id],[ClassCode],[Name],[SuperiorId],[Grade],[ClassNo],[StartDate],[GraduationDate],[Semesters] FROM [signin].[dbo].[Class] JOIN [signin].[dbo].[Institution] ON [Institution].Id = [Class].Id WHERE [Class].[ClassCode] = #ClassCode#
    </select>
    <select id="SelectClassByName" parameterClass="string" resultClass="Class">
      SELECT [Institution].[Id],[ClassCode],[Name],[SuperiorId],[Grade],[ClassNo],[StartDate],[GraduationDate],[Semesters] FROM [signin].[dbo].[Class] JOIN [signin].[dbo].[Institution] ON [Institution].Id = [Class].Id WHERE [Institution].[Name] LIKE '%'+#Name#+'%'
    </select>
    <select id="SelectClassesBySuperiorId" parameterClass="string" resultClass="Class">
      SELECT [Institution].[Id],[ClassCode],[Name],[SuperiorId],[Grade],[ClassNo],[StartDate],[GraduationDate],[Semesters] FROM [signin].[dbo].[Class] JOIN [signin].[dbo].[Institution] ON [Institution].Id = [Class].Id WHERE [Institution].[SuperiorId] LIKE '%$SuperiorId$%'
    </select>
    <select id="SelectAllClasses" resultClass="Class">
      SELECT [Id],[ClassCode],[Name],[SuperiorId],[Grade],[ClassNo],[StartDate],[GraduationDate],[Semesters] FROM [signin].[dbo].[Class] JOIN [signin].[dbo].[Institution] ON [Institution].Id = [Class].Id
    </select>
    <select id="IsClassCodeExists" parameterClass="string" resultClass="Int">
      SELECT COUNT([ClassCode]) FROM [signin].[dbo].[Class] WHERE [ClassCode] = #ClassCode#
    </select>
    <select id="IsClassNameExists" parameterClass="string" resultClass="Int">
      SELECT COUNT([Name]) FROM [signin].[dbo].[Institution] WHERE [Name] = #Name#
    </select>
  </statements>
</sqlMap>
Class.xml

  Dao层,先说说Mybatis怎么回事儿。做Jsp的时候接触到了Mybatis,原来开发.net的时候orm都是自带的ef,后来知道了Mybatis也有.net的版本才开始使用。两者性能方面没有比较过,只是ef的linq查询个人觉得还是没sql方便。Mybatis配置:

<?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 embedded="signin.dao.providers.config,signin.dao"/>
  
    <!-- Database connection information -->
    <database>
        <provider name="sqlServer2008"/>
    <dataSource name="iBatisNet" connectionString="data source=.;database=signin;user id=root;password=123456;Connect Timeout=2000"/>
  </database>

    <sqlMaps>
      <sqlMap embedded="signin.domain.Membership.Mapper.Profile.xml,signin.domain" />
    <sqlMap embedded="signin.domain.Membership.Mapper.Role.xml,signin.domain" />
    <sqlMap embedded="signin.domain.Membership.Mapper.User.xml,signin.domain" />
    <sqlMap embedded="signin.domain.Organization.Mapper.Class.xml,signin.domain" />
    <sqlMap embedded="signin.domain.Organization.Mapper.Institution.xml,signin.domain" />
    <sqlMap embedded="signin.domain.CurriculumSchedule.Mapper.Course.xml,signin.domain" />
    <sqlMap embedded="signin.domain.CurriculumSchedule.Mapper.Selection.xml,signin.domain" />
    <sqlMap embedded="signin.domain.CurriculumSchedule.Mapper.Schedule.xml,signin.domain" />
    <sqlMap embedded="signin.domain.Signin.Mapper.Sign.xml,signin.domain" />
    </sqlMaps>

</sqlMapConfig>
SqlMapper.config

MybatisUtil:

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

using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace signin.dao
{
    public abstract class MybatisUtil
    {
        private static ISqlMapper sqlMapper;

        public static ISqlMapper SqlMapper
        {
            get { return MybatisUtil.sqlMapper; }
            set { MybatisUtil.sqlMapper = value; }
        }

        static MybatisUtil()
        {

            string path = "";
            String AssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            String AssemblyDir = System.IO.Path.GetDirectoryName(AssemblyPath);
            AssemblyDir = AssemblyDir.Replace("file:\\", "");
            path = AssemblyDir + "\\";


            try
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                String filePath=path+ @"SqlMap.config";
                sqlMapper = builder.Configure(filePath);
            }
            catch (Exception ex)
            {
                throw new Exception("SqlMap.config文件错误", ex);
            }
        }
    }
}
MybatisUtil.cs

数据访问接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using signin.domain.Membership;
using Web.Util;
using signin.domain.Organization;
using System.Web.Script.Serialization;

namespace signin.dao.Membership
{
    public class UserDao:MybatisUtil
    {
        #region/// <summary>
        /// 添加一个用户
        /// </summary>
        /// <param name="user">用户实例</param>
        /// <returns>返回用户Id</returns>
        public Guid Insert(User user)
        {
            user.UserId = Guid.NewGuid();
            string userName = user.UserName;
            SecUtility.CheckParameter(ref userName, true, true, true, 0, "user.UserName");
            if (IsUserNameExists(userName))
                throw new Exception("there is a user named " + userName);
            if (IsEmailExists(user.Email))
                throw new Exception("there is a user whose email is:" + user.Email);
            SqlMapper.Insert("InsertUser", user);
            return user.UserId;
        }
        #endregion

        #region/// <summary>
        /// 删除一个用户
        /// </summary>
        /// <param name="user">用户实例</param>
        /// <returns>返回受影响行数</returns>
        public int Delete(User user)
        {
            if (user.UserId == Guid.Empty)
            {
                if (user.UserName == null)
                {
                    throw new ArgumentNullException("user.UserId and user.UserName");
                }
                else
                {
                    user.UserName = user.UserName.Trim();
                    if (user.UserName.Length < 1)
                    {throw new ArgumentException("user.UserId and user.UserName_can_not_be_empty");
                    }
                }
            }
            return (int)SqlMapper.Delete("DeleteUser", user);
        }

        /// <summary>
        /// 删除一个用户
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>返回受影响行数</returns>
        public int Delete(Guid userId)
        {
            if (userId == Guid.Empty)
                throw new ArgumentNullException("userId");
            User user = new User { UserId = userId };
            return Delete(user);
        }

        /// <summary>
        /// 删除一个用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>返回受影响行数</returns>
        public int Delete(string userName)
        {
            SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName");
            User user = new User { UserName = userName };
            return Delete(user);
        }
        #endregion

        #region/// <summary>
        /// 通过用户Id得到一个用户实例
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>User</returns>
        public T SelectById<T>(Guid userId) where T : User
        {
            if (userId == Guid.Empty)
                throw new ArgumentNullException("userId");
            return SqlMapper.QueryForObject<T>("SelectUserById", userId.ToString());
        }

        /// <summary>
        /// 通过编号获得用户实例
        /// </summary>
        /// <param name="no">编号</param>
        /// <returns></returns>
        public IList<T> SelectByNo<T>(string no) where T : User
        {
            SecUtility.CheckParameter(ref no, true, true, true, 0, "no");
            return SqlMapper.QueryForList<T>("SelectUserByUserNo", no);
        }

        /// <summary>
        /// 通过用户名获得用户实例
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public IList<T> SelectByName<T>(string userName) where T : User
        {
            SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName");
            return SqlMapper.QueryForList<T>("SelectUserByUserName", userName);
        }

        /// <summary>
        /// 通过昵称获得用户实例
        /// </summary>
        /// <param name="nickname">昵称</param>
        /// <returns></returns>
        public IList<T> SelectByNickname<T>(string nickname) where T : User
        {
            SecUtility.CheckParameter(ref nickname, true, true, true, 0, "nickname");
            return SqlMapper.QueryForList<T>("SelectUserByNickname", nickname);
        }

        /// <summary>
        /// 得到所用的用户实例
        /// </summary>
        /// <returns></returns>
        public IList<T> SelectAll<T>() where T : User
        {
            return SqlMapper.QueryForList<T>("SelectAllUsers", null);
        }

        /// <summary>
        /// SelectAllStuffs
        /// </summary>
        /// <returns></returns>
        public IList<T> SelectAllStuffs<T>() where T : User
        {
            return SqlMapper.QueryForList<T>("SelectAllStuffs", null);
        }

        /// <summary>
        /// SelectStuffsByPage
        /// </summary>
        /// <returns></returns>
        public IList<T> SelectStuffsByPage<T>(Profile profile) where T : User
        {
            return SqlMapper.QueryForList<T>("SelectStuffsByPage", profile);
        }

        /// <summary>
        /// SelectAllStudents
        /// </summary>
        /// <returns></returns>
        public IList<T> SelectAllStudents<T>() where T : User
        {
            return SqlMapper.QueryForList<T>("SelectAllStudents", null);
        }

        /// <summary>
        /// SelectStudentsByPage
        /// </summary>
        /// <returns></returns>
        public IList<T> SelectStudentsByPage<T>(Profile profile) where T : User
        {
            return SqlMapper.QueryForList<T>("SelectStudentsByPage", profile);
        }
        #endregion

        #region/// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="user">用户实例</param>
        /// <returns>返回受影响行数</returns>
        public int ChangePassword(User user)
        {
            if (user.UserId == Guid.Empty)
            {
                if (user.UserName == null)
                {
                    throw new ArgumentNullException("user.UserId and user.UserName");
                }
                else
                {
                    user.UserName = user.UserName.Trim();
                    if (user.UserName.Length < 1)
                    {
              throw new ArgumentException("user.UserId and user.UserName_can_not_be_empty"); } } } return (int)SqlMapper.Update("ChangePassword", user); } /// <summary> /// 修改密码 /// </summary> /// <param name="userId">用户Id</param> /// <param name="password">密码</param> /// <param name="passwordSalt">密钥</param> /// <returns>返回受影响行数</returns> public int ChangePassword(Guid userId,string password,string passwordSalt) { if (userId == Guid.Empty) throw new ArgumentNullException("userId"); SecUtility.CheckPasswordParameter(ref password, 0, "password"); SecUtility.CheckPasswordParameter(ref passwordSalt, 0, "passwordSalt"); User user = new User { UserId = userId, Password = password, PasswordSalt = passwordSalt }; return ChangePassword(user); } /// <summary> /// 修改密码 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <param name="passwordSalt">密钥</param> /// <returns>返回受影响行数</returns> public int ChangePassword(string userName, string password, string passwordSalt) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); SecUtility.CheckPasswordParameter(ref password, 0, "password"); SecUtility.CheckPasswordParameter(ref passwordSalt, 0, "passwordSalt"); User user = new User { UserName=userName, Password = password, PasswordSalt = passwordSalt }; return ChangePassword(user); } /// <summary> /// 登录记录 /// </summary> /// <param name="user">用户实例</param> /// <returns>返回受影响行数</returns> public int UpdateLoginRecord(User user) { if (user.UserId == Guid.Empty) { if (user.UserName == null) {throw new ArgumentNullException("user.UserId and user.UserName"); } else { user.UserName = user.UserName.Trim(); if (user.UserName.Length < 1) {throw new ArgumentException("user.UserId and user.UserName_can_not_be_empty"); } } } return (int)SqlMapper.Update("UpdateLoginRecord", user); } /// <summary> /// 登录记录 /// </summary> /// <param name="userId">用户Id</param> /// <returns>返回受影响行数</returns> public int UpdateLoginRecord(Guid userId) { if (userId == Guid.Empty) throw new ArgumentNullException("userId"); User user = new User { UserId = userId }; return UpdateLoginRecord(user); } /// <summary> /// 登录记录 /// </summary> /// <param name="userName">用户名</param> /// <returns>返回受影响行数</returns> public int UpdateLoginRecord(string userName) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); User user = new User { UserName=userName }; return UpdateLoginRecord(user); } /// <summary> /// 密码错误记录 /// </summary> /// <param name="user">用户实例</param> /// <returns>返回受影响行数</returns> public int UpdateFailedPasswordRecord(User user) { if (user.UserId == Guid.Empty) { if (user.UserName == null) { throw new ArgumentNullException("user.UserId and user.UserName"); } else { user.UserName = user.UserName.Trim(); if (user.UserName.Length < 1) {throw new ArgumentException("user.UserId and user.UserName_can_not_be_empty"); } } } return (int)SqlMapper.Update("UpdateFailedPasswordRecord", user); } /// <summary> /// 密码错误记录 /// </summary> /// <param name="userId">用户Id</param> /// <returns>返回受影响行数</returns> public int UpdateFailedPasswordRecord(Guid userId) { if (userId == Guid.Empty) throw new ArgumentNullException("userId"); User user = new User { UserId = userId }; return (int)UpdateFailedPasswordRecord(user); } /// <summary> /// 密码错误记录 /// </summary> /// <param name="userId">用户名</param> /// <returns>返回受影响行数</returns> public int UpdateFailedPasswordRecord(string userName) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); User user = new User { UserName=userName }; return (int)UpdateFailedPasswordRecord(user); } /// <summary> /// 密保答案错误记录 /// </summary> /// <param name="user">用户实例</param> /// <returns>返回受影响行数</returns> public int UpdateFailedPasswordAnswerRecord(User user) { if (user.UserId == Guid.Empty) { if (user.UserName == null) {throw new ArgumentNullException("user.UserId and user.UserName"); } else { user.UserName = user.UserName.Trim(); if (user.UserName.Length < 1) {throw new ArgumentException("user.UserId and user.UserName_can_not_be_empty"); } } } return (int)SqlMapper.Update("UpdateFailedPasswordAnswerRecord", user); } /// <summary> /// 密保答案错误记录 /// </summary> /// <param name="userId">用户Id</param> /// <returns>返回受影响行数</returns> public int UpdateFailedPasswordAnswerRecord(Guid userId) { if (userId == Guid.Empty) throw new ArgumentNullException("userId"); User user = new User { UserId = userId }; return (int)UpdateFailedPasswordAnswerRecord(user); } /// <summary> /// 密保答案错误记录 /// </summary> /// <param name="userId">用户名</param> /// <returns>返回受影响行数</returns> public int UpdateFailedPasswordAnswerRecord(string userName) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); User user = new User { UserName=userName }; return (int)UpdateFailedPasswordAnswerRecord(user); } #endregion #region 唯一性判断 /// <summary> /// 用户名是否存在 /// </summary> /// <param name="userName">用户名</param> /// <returns>存在返回true,否则返回false</returns> public bool IsUserNameExists(string userName) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); if (SqlMapper.QueryForObject<int>("IsUserNameExists", userName) > 0) return true; else return false; } /// <summary> /// 电子邮箱是否存在 /// </summary> /// <param name="userName">电子邮箱</param> /// <returns>存在返回true,否则返回false</returns> public bool IsEmailExists(string email) { SecUtility.CheckParameter(ref email, true, true, true, 0, "email"); if (SqlMapper.QueryForObject<int>("IsEmailExists", email) > 0) return true; else return false; } #endregion #region 机构查询 public IList<Institution> GetInstitutionsForUser(string userName) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); return SqlMapper.QueryForList<Institution>("GetInstitutionsForUser", userName); } public IList<Class> GetClassesForUser(string userName) { SecUtility.CheckParameter(ref userName, true, true, true, 0, "userName"); IList<Class> classes = SqlMapper.QueryForList<Class>("GetClassesForUser", userName); JavaScriptSerializer ser = new JavaScriptSerializer(); foreach (Class cls in classes) { if (cls.Semesters != null) cls.Sems = ser.Deserialize<IList<Semester>>(cls.Semesters); else { cls.Sems = new List<Semester>(); for (int i = 0; i < 8; i++) { Semester semester = new Semester(); semester.No = i + 1; cls.Sems.Add(semester); } } } return classes; } #endregion } }

如上public T SelectById<T>(Guid userId) where T : User等查询方法使用了泛型来在我需要的时候返回User或者Profile。

 

  限于时间和篇幅第一篇就在这里结束了,权限系统的后台部分就是如此。下次更新前台部分的内容。如前所述,完整的项目稍后才能开源,在这之前有需要的请邮箱联系,欢迎大家和我来一起完成这个签到系统。

posted @ 2014-02-14 01:10  Scott Lewis  阅读(3095)  评论(21编辑  收藏  举报