用到的几个DLL按理说应该到官网下载,但这个官网是谷大哥的,不知道是不是被屏蔽,总打不开,幸好从别人的程序里拷过来一份,直接放在自己的程序里就行!
程序结构如下:
Providers.config,SqlMap.config,TUser.xml是必须的。TUser.xml为对应TUser.cs实体类的配置文件(请注意文件名,类名最好要一样,不一样的情况貌似会出错)
providers.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="sqlServer2.0" description="Microsoft SQL Server 7.0/2000/2005, provider V2.0.0.0 in framework .NET V2.0" enabled="true" default="true" assemblyName="System.Data, Version=2.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="@"/> </providers>
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="false"/> <setting cacheModelsEnabled="true"/> </settings> <!--db provider配置文件路径--> <providers resource="providers.config"/> <!--db provider类型及连接串--> <database> <provider name="sqlServer2.0" /> <!--<dataSource name="SQLSERVER" connectionString="Data Source=ORCL;Persist Security Info=True;User ID=scott;Password=tiger;Unicode=True" />--> <dataSource name="SQLSERVER" connectionString="Data Source=(local);Initial Catalog=myBatis;User Id=sa;Password=anyqu989125;" /> </database> <!--db与Entity的映射文件--> <sqlMaps> <sqlMap resource="TUser.xml"/> </sqlMaps> </sqlMapConfig>
TUser.xml 如下:
<?xml version="1.0" encoding="utf-8" ?> <!--============================================================================ // CAUTION: This file is generated by IBatisNetGen.BatisMap.cst at 2014-10-22 13:59:11 // Any manual editing will be lost in re-generation. //===========================================================================--> <sqlMap namespace="MyBatisDemo" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <alias> <typeAlias alias="TUser" type="MyBatisDemo.TUser" /> </alias> <resultMaps> <resultMap id="FullResultMap" class="TUser"> <result property="UID" column="U_ID" dbType="Int"/> <result property="UName" column="U_Name" dbType="VarChar"/> <result property="UDept" column="U_Dept" dbType="VarChar"/> </resultMap> </resultMaps> <statements> <insert id="Insert" parameterClass="TUSER"> INSERT INTO [dbo].[T_USER] ( [U_ID] , [U_Name] , [U_Dept] ) VALUES ( #UID,dbType=Int# , #UName,dbType=VarChar# , #UDept,dbType=VarChar# ) </insert> </statements> </sqlMap>
实体类如下:
using System; using System.Collections.Generic; using System.Text; namespace MyBatisDemo { //============================================================================== /// <summary><c>TUSER</c> Business Object.</summary> [Serializable] public partial class TUSER { #region UID private Int32 m_uID; /// <summary>Gets or sets UID</summary> public Int32 UID { get { return m_uID; } set { m_uID = value; } } #endregion #region UName private String m_uName; /// <summary>Gets or sets UName</summary> public String UName { get { return m_uName; } set { m_uName = value; } } #endregion #region UDept private String m_uDept; /// <summary>Gets or sets UDept</summary> public String UDept { get { return m_uDept; } set { m_uDept = value; } } #endregion } }
上面提到的三个配置文件均须放在DEBUG下。同时请注意 实体类的文件名,类名,配置文件内所有涉及到类名的地方,均应一致,否则会提示无法创建SQLMAP对象,原因是找不到ProductMap.xml,错误如下:
找了一天的原因,一直以为是程序里的ProductMap.xml有问题,却查不到这个文件,后来才明白这个文件是框架中定义的实体配置类的一个代名词。查看上图中错的详细信息如下:
时间: 2024-10-15 03:20:17