7.我们在项目根目录下建Maps文件夹,并在文件夹中创建PersonMap.xml映射文件,如下
<sqlMap namespace="PratiseMyBatis" xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<alias>
<!--类的别名-->
<typeAlias alias="Person" type="PratiseMyBatis.Models.Person,PratiseMyBatis"/>
</alias>
<resultMaps>
<!--Person类与db表的映射-->
<resultMap id="PersonResult" class="Person">
<result property="ID" column="ID"/>
<result property="Name" column="Name"/>
<result property="Age" column="Age" />
<result property="Sex" column="Sex" />
</resultMap>
</resultMaps>
<statements>
<!--插入Sql语句-->
<insert id="Add" parameterClass="Person" resultClass="Int32">
insert into Person(Name,Age,Sex)values(#Name#,#Age#,#Sex#)
<selectKey property="ID" resultClass="int" type="post" >
SELECT @@identity AS ID
</selectKey>
</insert>
<!--修改Sql语句-->
<update id="Update" parameterClass="Person">
update Person set Name=#Name#,Age=#Age#,Sex=#Sex# where ID=#ID#
</update>
<!--根据主键删除单条记录-->
<delete id="Delete" parameterClass="Int32">
delete Person where ID=#ID#
</delete>
<!--查询单个实体Sql语句-->
<select id="Get" parameterClass="Int32" resultMap="PersonResult">
select * from Person where ID=#ID#
</select>
<!--查询所有记录-->
<select id="GetList" resultMap="PersonResult">
<![CDATA[select * from Person where ID<4]]>
</select>
</statements>
</sqlMap>
我们可以看到这段代码里有一个命名空间,如下:
这个命名空间与此文件中的 statements标签中配置的SQL语句的id有关,在大型项目中,可能存在大量的 SQL 语句,这时候,为每个SQL 语句起一个唯一的标识id 就变得并不容易了。为了解决这个问题,在 mybatis 中,可以为每个映射文件起一个唯一的命名空间,这样,定义在这个映射文件中的每个 SQL 语句就成了定义在这个命名空间中的一个 id。只要我们能够保证每个命名空间是唯一的,即使在不同映射文件中的语句的 id 相同,也就不会冲突了。(这段是网上复制的),这个是在SqlMap.config配置文件中控制的,就是这个标签 。
8.接下来我们就要在SqlMap.config文件中配置这个映射文件的路径了,上面的代码里面我们已经配好了
9.在XML映射文件中我们已经写好的基础的增、删、改、查SQL语句,接下来我们在项目中建一个Common文件夹,里面写一个通用的BaseDA类,里面进行ISqlMapper的实例化,以及对MyBatis.Net做些基本的封装(这个是网上复制的)
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PratiseMyBatis.Common
{
public class BaseDA
{
//添加
public static int Insert<T>(string statementName, T t)
{
ISqlMapper iSqlMapper = Mapper.Instance();
if (iSqlMapper != null)
{
return (int)iSqlMapper.Insert(statementName, t);
}
return 0;
}
//删除
public static int Delete<T>(string statementName, T t)
{
ISqlMapper iSqlMapper = Mapper.Instance();
if (iSqlMapper != null)
{
return (int)iSqlMapper.Delete(statementName,t);
}
return 0;
}
//修改
public static int Update<T>(string statementName, T t)
{
ISqlMapper iSqlMapper = Mapper.Instance();
if (iSqlMapper != null)
{
return (int)iSqlMapper.Update(statementName,t);
}
return 0;
}
//查询
public static T Get<T>(string statementName, int PrimaryKeyId)
{
ISqlMapper iSqlMapper = Mapper.Instance();
if (iSqlMapper != null)
{
return iSqlMapper.QueryForObject<T>(statementName,PrimaryKeyId);
}
return default(T);
}
//查询全部
public static IList<T> QueryForList<T>(string statementName, object parameterObject = null)
{
ISqlMapper iSqlMapper = Mapper.Instance();
if (iSqlMapper != null)
{
return iSqlMapper.QueryForList<T>(statementName,parameterObject);
}
return null;
}
}
}
9 现在我们就可以控制器中进行操作了
Person p1 = new Person();
p1.Name = "亚瑟";
p1.Age = 25;
p1.Sex = "男";
//插入一条信息
int PID = BaseDA.Insert<Person>("Add", p1);
//查询刚插入的信息
Person per = BaseDA.Get<Person>("Get", PID);
//对ID为2的信息进行修改
Person p2 = new Person();
p2.ID = 2;
p2.Name = "葛二蛋";
p2.Age = 20;
p2.Sex = "男";
int updateResult = BaseDA.Update<Person>("Update", p2);
List<Person> list = (List<Person>)Common.BaseDA.QueryForList<Models.Person>("GetList", null);
view页面
@model List<PratiseMyBatis.Models.Person>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table border="1" style="color:green">
<tr>
<td>名字</td>
<td>年龄</td>
<td>性别</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.Age</td>
<td>@item.Sex</td>
</tr>
}
</table>
</div>
</body>
</html>
因为在PersonMap.xml里查询所有信息的SQL语句里有一个 ID<4。
所以这就是查询到的信息了,这里方法中的第一个参数就是我们映射文件中SQL语句标签中的id名称,其他的增、删、改都一样,就不多说了