假設咱們如今有這樣的需求,要對學生信息進行管理sql
學生表有要如下要求數據庫
字段名稱apache |
數據類型app |
說明ide |
stuNopost |
字符ui |
學號,該列必填,爲主鍵遞增spa |
stuNamecode |
字符xml |
學生姓名,該列必填,要考慮姓氏多是兩個字的,如歐陽俊雄 |
stuSex |
字符 |
學生性別,該列必填,且只能是「男」或「女」。由於男生較多,默認爲「男」 |
stuAge |
數字 |
學生年齡,該列必填,必須在15~50歲之間 |
stuSeat |
數字 |
學生的座位號 |
stuAddress |
字符 |
學生地址,該列可不填,如沒有填寫,默認爲「地址不詳」 |
1. – 建立表[student_tb]
create table student_tb ( StuNo int identity(1,1) primary key, StuName varchar(10) not null, StuSex varchar(5) check(StuSex in('男','女')) default('男'), StuAge int check (StuAge between 15 and 50) not null, StuSeat int not null, StuAddress varchar (20) default('地址不詳'), );
2. –建立實體
public class StudentInfo { public int StuNo { get; set; } public string StuName { get; set; } public string StuSex { get; set; } public int StuAge { get; set; } public int StuSeat { get; set; } public string StuAddress { get; set; } }
3. – 建立SqlMapper Provider
建立SqlMapper的方式有如下幾種
a. 第一種方式
ISqlMapper _sqlMapper=IBatisNet.DataMapper.Mapper.Instance()
注:此種方式要求SqlMap.config文件位於應用程序根目錄下,且文件名是且僅是」SqlMap.config」。
b. 第二種方式
ISqlMapper _sqlMapper=new DomSqlMapBuilder().Configure()
注:同上
c. 第三種方式——指定SqlMap.config的路徑(使用EmbededResource查找config資源時,要求SqlMap.config生成操做屬性爲嵌入的資源)
XmlDocument sqlMapConfig = Resources.GetEmbeddedResourceAsXmlDocument("Config.SqlMap.config, Persistence");
ISqlMapper _sqlMapper = new DomSqlMapBuilder().Configure(sqlMapConfig); //---第三種
MyBatisProvider代碼以下:
public class MyBatisProvider { private static ISqlMapper _sqlMapper; private static object sysncObj = new object(); public static ISqlMapper GetInstanse() { if (_sqlMapper == null) { lock (sysncObj) { if (_sqlMapper == null) { //_sqlMapper = IBatisNet.DataMapper.Mapper.Instance();//---第一種 //_sqlMapper = new DomSqlMapBuilder().Configure(); //---第二種 XmlDocument sqlMapConfig = Resources.GetEmbeddedResourceAsXmlDocument("MyBatis.SqlMap.config, MyBatis"); _sqlMapper = new DomSqlMapBuilder().Configure(sqlMapConfig); //---第三種 } } } return _sqlMapper; } }
4. –在項目中
添加配置文件
a. provider.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="sqlServer4.0" enabled="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="false" /> </providers>
b. 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"> <settings> <setting useStatementNamespaces="true"/> <setting cacheModelsEnabled="true"/> </settings> <providers embedded="MyBatis.providers.config,MyBatis"/> <database> <provider name="sqlServer4.0"/> <dataSource name="dataSourceName" connectionString="鏈接語句"/> </database> <sqlMaps> <sqlMap embedded="MyBatis.SqlMaps.StudentInfo.xml,MyBatis"/> </sqlMaps> </sqlMapConfig>
按照代碼中的建立實例方式選擇不一樣的位置及名稱
注: <setting useStatementNamespaces="true"/> true表示statementName要使用Namespace ,即實體映射XML中的namespace屬性
sqlMaps節點下爲實體映射XML文件路徑
embedded表示文件的屬性生成操做爲嵌入的資源
5. –建立實體映射文件
<?xml version="1.0" encoding="utf-8" ?> <sqlMap namespace="StudentInfo" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <alias> <typeAlias alias="StudentInfo" type="Model.StudentInfo,Model" /> </alias> <resultMaps> <resultMap id="StudentResult" class="StudentInfo"> <result property="StuNo" column="stuNo"/> <result property="StuName" column="stuName"/> <result property="StuSex" column="stuSex"/> <result property="StuAge" column="stuAge"/> <result property="StuSeat" column="stuSeat"/> <result property="StuAddress" column="stuAddress"/> </resultMap> </resultMaps> <statements> <insert id="Insert" parameterClass="StudentInfo" resultClass="int"> INSERT INTO [student_tb]([stuName],[stuSex],[stuAge],[stuSeat],[stuAddress]) VALUES(#StuName#,#StuSex#,#StuAge#,#StuSeat#,#StuAddress#) <selectKey property="StuNo" resultClass="int" type="post" > SELECT @@identity AS StuNo </selectKey> </insert> <delete id="Delete" parameterClass="Int32"> UPDATE [student_tb] SET [stuName] = #StuName#,[stuSex] = #StuSex#,[stuAge] = #StuAge#,[stuSeat] = #StuSeat#,[stuAddress] = #StuAddress# WHERE [stuNo]=#StuNo# </delete> <update id="Update" parameterClass="StudentInfo"> UPDATE [student_tb] SET [stuName] = #StuName#,[stuSex] = #StuSex#,[stuAge] = #StuAge#,[stuSeat] = #StuSeat#,[stuAddress] = #StuAddress# WHERE [stuNo]=#StuNo# </update> <select id="Get" parameterClass="Int32" resultMap="StudentResult"> select * from [student_tb] where stuNo=#StuNo# </select> <select id="List" parameterClass="map" resultMap="StudentResult"> select * from [student_tb] </select> </statements> </sqlMap>
如上,爲一個簡單的XML實體映射文件。
經過resultMaps節點,將實體屬性與數據庫字段對應起來。statements中再寫增刪改查等相關的操做節點及SQL
6. –DAL操做數據庫
public class StudentDAL { public int Insert(StudentInfo info) { string stmtName = "StudentInfo.Insert";
MyBatisProvider.GetInstanse().Insert(stmtName, info);
return info.StuNo; } public int Update(StudentInfo info) { string stmtName = "StudentInfo.Update"; return MyBatisProvider.GetInstanse().Update(stmtName, info); } public StudentInfo Get(int id) { string stmtName = "StudentInfo.Get"; return MyBatisProvider.GetInstanse().QueryForObject<StudentInfo>(stmtName, id); } public IList<StudentInfo> List() { string stmtName = "StudentInfo.List"; return MyBatisProvider.GetInstanse().QueryForList<StudentInfo>(stmtName, null); } }
示例代碼:
------------------------2014.10.8-----------------------
注:添加的時候MyBatis會將主鍵賦值給傳入的參數實體。因此在代碼中接收的時候應使用此方式。文章中已改正,附件代碼中存在問題。
public int Insert(StudentInfo info) { string stmtName = "StudentInfo.Insert"; MyBatisProvider.GetInstanse().Insert(stmtName, info); return info.StuNo; }