假设我们现在有这样的需求,要对学生信息进行管理

学生表有要以下要求

字段名称

数据类型

说明

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; } 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;
}

MyBatis.Net 配置的更多相关文章

  1. Mybatis XML配置

    Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  2. MyBatis Cache配置

    @(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...

  3. spring和mybatis整合配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  5. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  6. MyBatis 实践 -配置

    MyBatis 实践 标签: Java与存储 Configuration mybatis-configuration.xml是MyBatis的全局配置文件(文件名任意),其配置内容和顺序如下: pro ...

  7. SpringMVC+Mybatis+MySQL配置Redis缓存

    SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...

  8. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  9. 笔记:MyBatis XML配置详解

    MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 properties ...

  10. mybatis的配置和使用

    mybatis的配置和使用 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

随机推荐

  1. Odoo模块添加logo

    模块的static目录是专门用于存放静态文件内容如:图片.css等的.其中,description目录是存放关于模块的一些描述性的文件的,其中,模块的图标也放在下面,以icon命名.

  2. USDT/BTC/ETC/HT的解释

    USDT 泰达币 泰达币(USDT)是Tether公司推出的基于稳定价值货币美元(USD)的代币Tether USD(下称USDT),1USDT=1美元,用户可以随时使用USDT与USD进行1:1兑换 ...

  3. 通讯协议(一)HTTP协议

    协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器.目前我们使 ...

  4. SDN测量论文粗读(三)9.24

    Jaal: Towards Network Intrusion Detection at ISP Scale 论文来源:CoNext 发表时间:2015 解决问题及所做贡献:Jaal:大规模精细网络入 ...

  5. js尾巴

    js中根据id获取标签: /** * 根据id获取标签 * @param {string}id * @returns {object} */ function $(id) { return typeo ...

  6. Ionic3项目实践记录

    Ionic3首次项目实践记录 标签(空格分隔): Angular Ionic Ionic3踩坑 1. 路由懒加载(lazy load) 如果设置了懒加载,就必须全部懒加载(包括TabsPage),否则 ...

  7. 用Google Cloud Plateform使用ansible创建新实例

    谷歌大法好,退aws保平安 ---假设自己现在有一个电脑(本机),先用谷歌云创一个服务器(主机) ,再通过ansible在谷歌云上创建一个服务器(节点)并进行控制 先把自己的公钥加到元数据里面,在go ...

  8. shell习题第10题:打印每个单词的字数

    [题目要求] 用shell打印下面这句话中字母数小于6的单词. Bash also interprets a number of multi-character options. [核心要点] for ...

  9. PHP 扩展 trie-tree, swoole过滤敏感词方案

    在一些app,web中评论以及一些文章会看到一些*等,除了特定的不显示外,我们会把用户输入的一些敏感字符做处理,具体显示为*还是其他字符按照业务区实现. 下面简单介绍下业务处理. 原文地址:小时刻个人 ...

  10. Hive(10)-文件存储格式

    Hive支持的存储数据的格式主要有:TEXTFILE .SEQUENCEFILE.ORC.PARQUET 一. 列式存储和行式存储 左边为逻辑表,右边第一个为行式存储,第二个为列式存储 1. 行式存储 ...