假設我們現(xiàn)在有這樣的需求,要對學生信息進行管理
學生表有要以下要求
字段名稱 | 數據類型 | 說明 |
stuNo | 字符 | 學號,該列必填,為主鍵遞增 |
stuName | 字符 | 學生姓名,該列必填,要考慮姓氏可能是兩個字的,如歐陽俊雄 |
stuSex | 字符 | 學生性別,該列必填,且只能是“男”或“女”。因為男生較多,默認為“男” |
stuAge | 數字 | 學生年齡,該列必填,必須在15~50歲之間 |
stuSeat | 數字 | 學生的座位號 |
stuAddress | 字符 | 學生地址,該列可不填,如沒有填寫,默認為“地址不詳” |
1. – 創(chuàng)建表[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. –創(chuàng)建實體
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. – 創(chuàng)建SqlMapper Provider
創(chuàng)建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"?><providersxmlns="http://ibatis.apache.org/providers"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <clear/> <provider name="sqlServer4.0" enabled="true" descrb. 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>按照代碼中的創(chuàng)建實例方式選擇不同的位置及名稱
注: <setting useStatementNamespaces="true"/> true表示statementName要使用Namespace ,即實體映射XML中的namespace屬性
sqlMaps節(jié)點下為實體映射XML文件路徑
embedded表示文件的屬性生成操作為嵌入的資源
5. –創(chuàng)建實體映射文件
<?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]
新聞熱點
疑難解答