MyBatis.Net 配置
假设我们现在有这样的需求,要对学生信息进行管理
学生表有要以下要求
字段名称 |
数据类型 |
说明 |
stuNo |
字符 |
学号,该列必填,为主键递增 |
stuName |
字符 |
学生姓名,该列必填,要考虑姓氏可能是两个字的,如欧阳俊雄 |
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; }</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> StuName { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> StuSex { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> StuAge { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> StuSeat { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> StuAddress { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; } }</span></pre>
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();//--- 第一种XmlDocument sqlMapConfig = Resources.GetEmbeddedResourceAsXmlDocument("MyBatis.SqlMap.config, MyBatis");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">_sqlMapper = new DomSqlMapBuilder().Configure(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">---第二种</span>
} } } return _sqlMapper; } }_sqlMapper </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> DomSqlMapBuilder().Configure(sqlMapConfig); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">---第三种</span>
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
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; }</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> Update(StudentInfo info) { </span><span style="color: rgba(0, 0, 255, 1)">string</span> stmtName = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StudentInfo.Update</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> MyBatisProvider.GetInstanse().Update(stmtName, info); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> StudentInfo Get(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id) { </span><span style="color: rgba(0, 0, 255, 1)">string</span> stmtName = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StudentInfo.Get</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">return</span> MyBatisProvider.GetInstanse().QueryForObject<StudentInfo><span style="color: rgba(0, 0, 0, 1)">(stmtName, id); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> IList<StudentInfo><span style="color: rgba(0, 0, 0, 1)"> List() { </span><span style="color: rgba(0, 0, 255, 1)">string</span> stmtName = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StudentInfo.List</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">return</span> MyBatisProvider.GetInstanse().QueryForList<StudentInfo>(stmtName, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); } }</span></pre>
示例代码:
------------------------2014.10.8-----------------------
注:添加的时候 MyBatis 会将主键赋值给传入的参数实体。所以在代码中接收的时候应使用此方式。文章中已改正,附件代码中存在问题。
1 2 3 4 5 6 7 8 | public int Insert(StudentInfo info) { string stmtName = "StudentInfo.Insert" ; MyBatisProvider.GetInstanse().Insert(stmtName, info); return info.StuNo; } |