ibatis.net:第二天,Hello,World ?
背景
本文的内容全部来自于官方的文档,此处仅仅为了强化记忆。
项目结构
Properties.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <settings> 3 <add key="root" value="E:\Coding\HappyStudy\BatisNetStudy\BatisNetStudy\bin\Debug\" /> 4 </settings>
Providers.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <providers 3 xmlns="http://ibatis.apache.org/providers" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 6 <clear/> 7 <provider 8 name="sqlServer1.0" 9 description="Microsoft SQL Server, provider V1.0.3300.0 in framework .NET V1.0" 10 enabled="false" 11 assemblyName="System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.SqlClient.SqlConnection" 12 commandClass="System.Data.SqlClient.SqlCommand" 13 parameterClass="System.Data.SqlClient.SqlParameter" 14 parameterDbTypeClass="System.Data.SqlDbType" 15 parameterDbTypeProperty="SqlDbType" 16 dataAdapterClass="System.Data.SqlClient.SqlDataAdapter" 17 commandBuilderClass="System.Data.SqlClient.SqlCommandBuilder" 18 usePositionalParameters="false" 19 useParameterPrefixInSql="true" 20 useParameterPrefixInParameter="true" 21 parameterPrefix="@" 22 allowMARS="false" 23 /> 24 <provider 25 name="sqlServer1.1" 26 description="Microsoft SQL Server, provider V1.0.5000.0 in framework .NET V1.1" 27 enabled="true" 28 default="true" 29 assemblyName="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 30 connectionClass="System.Data.SqlClient.SqlConnection" 31 commandClass="System.Data.SqlClient.SqlCommand" 32 parameterClass="System.Data.SqlClient.SqlParameter" 33 parameterDbTypeClass="System.Data.SqlDbType" 34 parameterDbTypeProperty="SqlDbType" 35 dataAdapterClass="System.Data.SqlClient.SqlDataAdapter" 36 commandBuilderClass="System.Data.SqlClient.SqlCommandBuilder" 37 usePositionalParameters="false" 38 useParameterPrefixInSql="true" 39 useParameterPrefixInParameter="true" 40 parameterPrefix="@" 41 allowMARS="false" 42 /> 43 <provider 44 name="sqlServer2.0" 45 enabled="false" 46 description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0" 47 assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 48 connectionClass="System.Data.SqlClient.SqlConnection" 49 commandClass="System.Data.SqlClient.SqlCommand" 50 parameterClass="System.Data.SqlClient.SqlParameter" 51 parameterDbTypeClass="System.Data.SqlDbType" 52 parameterDbTypeProperty="SqlDbType" 53 dataAdapterClass="System.Data.SqlClient.SqlDataAdapter" 54 commandBuilderClass=" System.Data.SqlClient.SqlCommandBuilder" 55 usePositionalParameters = "false" 56 useParameterPrefixInSql = "true" 57 useParameterPrefixInParameter = "true" 58 parameterPrefix="@" 59 allowMARS="false" 60 /> 61 <provider 62 name="sqlServer2005" 63 enabled="false" 64 description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0" 65 assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 66 connectionClass="System.Data.SqlClient.SqlConnection" 67 commandClass="System.Data.SqlClient.SqlCommand" 68 parameterClass="System.Data.SqlClient.SqlParameter" 69 parameterDbTypeClass="System.Data.SqlDbType" 70 parameterDbTypeProperty="SqlDbType" 71 dataAdapterClass="System.Data.SqlClient.SqlDataAdapter" 72 commandBuilderClass=" System.Data.SqlClient.SqlCommandBuilder" 73 usePositionalParameters = "false" 74 useParameterPrefixInSql = "true" 75 useParameterPrefixInParameter = "true" 76 parameterPrefix="@" 77 allowMARS="true" 78 /> 79 <provider name="OleDb1.1" 80 description="OleDb, provider V1.0.5000.0 in framework .NET V1.1" 81 enabled="true" 82 assemblyName="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 83 connectionClass="System.Data.OleDb.OleDbConnection" 84 commandClass="System.Data.OleDb.OleDbCommand" 85 parameterClass="System.Data.OleDb.OleDbParameter" 86 parameterDbTypeClass="System.Data.OleDb.OleDbType" 87 parameterDbTypeProperty="OleDbType" 88 dataAdapterClass="System.Data.OleDb.OleDbDataAdapter" 89 commandBuilderClass="System.Data.OleDb.OleDbCommandBuilder" 90 usePositionalParameters="true" 91 useParameterPrefixInSql="false" 92 useParameterPrefixInParameter="false" 93 parameterPrefix="" 94 allowMARS="false" 95 /> 96 <provider name="OleDb2.0" 97 description="OleDb, provider V2.0.0.0 in framework .NET V2" 98 enabled="false" 99 assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 100 connectionClass="System.Data.OleDb.OleDbConnection" 101 commandClass="System.Data.OleDb.OleDbCommand" 102 parameterClass="System.Data.OleDb.OleDbParameter" 103 parameterDbTypeClass="System.Data.OleDb.OleDbType" 104 parameterDbTypeProperty="OleDbType" 105 dataAdapterClass="System.Data.OleDb.OleDbDataAdapter" 106 commandBuilderClass="System.Data.OleDb.OleDbCommandBuilder" 107 usePositionalParameters="true" 108 useParameterPrefixInSql="false" 109 useParameterPrefixInParameter="false" 110 parameterPrefix="" 111 allowMARS="false" 112 /> 113 <provider 114 name="Odbc1.1" 115 description="Odbc, provider V1.0.5000.0 in framework .NET V1.1" 116 enabled="true" 117 assemblyName="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 118 connectionClass="System.Data.Odbc.OdbcConnection" 119 commandClass="System.Data.Odbc.OdbcCommand" 120 parameterClass="System.Data.Odbc.OdbcParameter" 121 parameterDbTypeClass="System.Data.Odbc.OdbcType" 122 parameterDbTypeProperty="OdbcType" 123 dataAdapterClass="System.Data.Odbc.OdbcDataAdapter" 124 commandBuilderClass="System.Data.Odbc.OdbcCommandBuilder" 125 usePositionalParameters="true" 126 useParameterPrefixInSql="false" 127 useParameterPrefixInParameter="false" 128 parameterPrefix="@" 129 allowMARS="false" 130 /> 131 <provider 132 name="Odbc2.0" 133 description="Odbc, provider V2.0.0.0 in framework .NET V2" 134 enabled="false" 135 assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 136 connectionClass="System.Data.Odbc.OdbcConnection" 137 commandClass="System.Data.Odbc.OdbcCommand" 138 parameterClass="System.Data.Odbc.OdbcParameter" 139 parameterDbTypeClass="System.Data.Odbc.OdbcType" 140 parameterDbTypeProperty="OdbcType" 141 dataAdapterClass="System.Data.Odbc.OdbcDataAdapter" 142 commandBuilderClass="System.Data.Odbc.OdbcCommandBuilder" 143 usePositionalParameters="true" 144 useParameterPrefixInSql="false" 145 useParameterPrefixInParameter="false" 146 parameterPrefix="@" 147 allowMARS="false" 148 /> 149 <provider 150 name="oracle9.2" 151 description="Oracle, Oracle provider V9.2.0.401" 152 enabled="false" 153 assemblyName="Oracle.DataAccess, Version=9.2.0.401, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionClass="Oracle.DataAccess.Client.OracleConnection" 154 commandClass="Oracle.DataAccess.Client.OracleCommand" 155 parameterClass="Oracle.DataAccess.Client.OracleParameter" 156 parameterDbTypeClass="Oracle.DataAccess.Client.OracleDbType" 157 parameterDbTypeProperty="OracleDbType" 158 dataAdapterClass="Oracle.DataAccess.Client.OracleDataAdapter" 159 commandBuilderClass="Oracle.DataAccess.Client.OracleCommandBuilder" 160 usePositionalParameters="false" 161 useParameterPrefixInSql="true" 162 useParameterPrefixInParameter="false" 163 parameterPrefix=":" 164 useDeriveParameters="false" 165 allowMARS="false" 166 /> 167 <provider 168 name="oracle10.1" 169 description="Oracle, oracle provider V10.1.0.301" 170 enabled="false" 171 assemblyName="Oracle.DataAccess, Version=10.1.0.301, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionClass="Oracle.DataAccess.Client.OracleConnection" 172 commandClass="Oracle.DataAccess.Client.OracleCommand" 173 parameterClass="Oracle.DataAccess.Client.OracleParameter" 174 parameterDbTypeClass="Oracle.DataAccess.Client.OracleDbType" 175 parameterDbTypeProperty="OracleDbType" 176 dataAdapterClass="Oracle.DataAccess.Client.OracleDataAdapter" 177 commandBuilderClass="Oracle.DataAccess.Client.OracleCommandBuilder" 178 usePositionalParameters="true" 179 useParameterPrefixInSql="true" 180 useParameterPrefixInParameter="true" 181 parameterPrefix=":" 182 useDeriveParameters="false" 183 allowMARS="false" 184 /> 185 <provider 186 name="oracleClient1.0" 187 description="Oracle, Microsoft provider V1.0.5000.0" 188 enabled="false" 189 assemblyName="System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.OracleClient.OracleConnection" 190 commandClass="System.Data.OracleClient.OracleCommand" 191 parameterClass="System.Data.OracleClient.OracleParameter" 192 parameterDbTypeClass="System.Data.OracleClient.OracleType" 193 parameterDbTypeProperty="OracleType" 194 dataAdapterClass="System.Data.OracleClient.OracleDataAdapter" 195 commandBuilderClass="System.Data.OracleClient.OracleCommandBuilder" 196 usePositionalParameters="false" 197 useParameterPrefixInSql="true" 198 useParameterPrefixInParameter="false" 199 parameterPrefix=":" 200 allowMARS="false" 201 /> 202 <provider 203 name="ByteFx" 204 description="MySQL, ByteFx provider V0.7.6.15073" 205 enabled="false" 206 assemblyName="ByteFX.MySqlClient, Version=0.7.6.15073, Culture=neutral, PublicKeyToken=f2fef6fed1732fc1" connectionClass="ByteFX.Data.MySqlClient.MySqlConnection" 207 commandClass="ByteFX.Data.MySqlClient.MySqlCommand" 208 parameterClass="ByteFX.Data.MySqlClient.MySqlParameter" 209 parameterDbTypeClass="ByteFX.Data.MySqlClient.MySqlDbType" 210 parameterDbTypeProperty="MySqlDbType" 211 dataAdapterClass="ByteFX.Data.MySqlClient.MySqlDataAdapter" 212 commandBuilderClass="ByteFX.Data.MySqlClient.MySqlCommandBuilder" 213 usePositionalParameters="false" 214 useParameterPrefixInSql="true" 215 useParameterPrefixInParameter="true" 216 parameterPrefix="@" 217 allowMARS="false" 218 /> 219 <provider 220 name="MySql" 221 description="MySQL, MySQL provider 1.0.7.30072" 222 enabled="false" 223 assemblyName="MySql.Data, Version=1.0.7.30072, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionClass="MySql.Data.MySqlClient.MySqlConnection" 224 commandClass="MySql.Data.MySqlClient.MySqlCommand" 225 parameterClass="MySql.Data.MySqlClient.MySqlParameter" 226 parameterDbTypeClass="MySql.Data.MySqlClient.MySqlDbType" 227 parameterDbTypeProperty="MySqlDbType" 228 dataAdapterClass="MySql.Data.MySqlClient.MySqlDataAdapter" 229 commandBuilderClass="MySql.Data.MySqlClient.MySqlCommandBuilder" 230 usePositionalParameters="false" 231 useParameterPrefixInSql="true" 232 useParameterPrefixInParameter="true" 233 parameterPrefix="?" 234 allowMARS="false" 235 /> 236 <provider name="SQLite3 Finisar" 237 description="SQLite, SQLite.NET provider V0.21.1869.3794" 238 enabled="false" 239 assemblyName="SQLite.NET, Version=0.21.1869.3794, Culture=neutral, PublicKeyToken=c273bd375e695f9c" 240 connectionClass="Finisar.SQLite.SQLiteConnection" 241 commandClass="Finisar.SQLite.SQLiteCommand" 242 parameterClass="Finisar.SQLite.SQLiteParameter" 243 parameterDbTypeClass="System.Data.DbType, System.Data" 244 parameterDbTypeProperty="DbType" 245 dataAdapterClass="Finisar.SQLite.SQLiteDataAdapter" 246 commandBuilderClass="Finisar.SQLite.SQLiteCommandBuilder" 247 usePositionalParameters="false" 248 useParameterPrefixInSql="true" 249 useParameterPrefixInParameter="true" 250 parameterPrefix="@" 251 setDbParameterPrecision="false" 252 setDbParameterScale="false" 253 allowMARS="false" 254 /> 255 <provider name="SQLite3" 256 description="SQLite, SQLite.NET provider V1.0.43.0" 257 enabled="false" 258 assemblyName="System.Data.SQLite, Version=1.0.43.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" 259 connectionClass="System.Data.SQLite.SQLiteConnection" 260 commandClass="System.Data.SQLite.SQLiteCommand" 261 parameterClass="System.Data.SQLite.SQLiteParameter" 262 parameterDbTypeClass="System.Data.SQLite.SQLiteType" 263 parameterDbTypeProperty="DbType" 264 dataAdapterClass="System.Data.SQLite.SQLiteDataAdapter" 265 commandBuilderClass="System.Data.SQLite.SQLiteCommandBuilder" 266 usePositionalParameters="false" 267 useParameterPrefixInSql="true" 268 useParameterPrefixInParameter="true" 269 parameterPrefix="@" 270 setDbParameterPrecision="false" 271 setDbParameterScale="false" 272 allowMARS="false" 273 /> 274 <provider 275 name="Firebird1.7" 276 description="Firebird, Firebird SQL .NET provider V1.7.0.33200" 277 enabled="false" 278 assemblyName="FirebirdSql.Data.Firebird, Version=1.7.0.33200, Culture=neutral, PublicKeyToken=fa843d180294369d" connectionClass="FirebirdSql.Data.Firebird.FbConnection" 279 commandClass="FirebirdSql.Data.Firebird.FbCommand" 280 parameterClass="FirebirdSql.Data.Firebird.FbParameter" 281 parameterDbTypeClass="FirebirdSql.Data.Firebird.FbDbType" 282 parameterDbTypeProperty="FbDbType" 283 dataAdapterClass="FirebirdSql.Data.Firebird.FbDataAdapter" 284 commandBuilderClass="FirebirdSql.Data.Firebird.FbCommandBuilder" 285 usePositionalParameters="false" 286 useParameterPrefixInSql="true" 287 useParameterPrefixInParameter="true" 288 parameterPrefix="@" 289 allowMARS="false" 290 /> 291 <provider 292 name="PostgreSql0.99.1.0" 293 description="PostgreSql, Npgsql provider V0.99.1.0" 294 enabled="false" 295 assemblyName="Npgsql, Version=0.99.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" 296 connectionClass="Npgsql.NpgsqlConnection" 297 commandClass="Npgsql.NpgsqlCommand" 298 parameterClass="Npgsql.NpgsqlParameter" 299 parameterDbTypeClass="NpgsqlTypes.NpgsqlDbType" 300 parameterDbTypeProperty="NpgsqlDbType" 301 dataAdapterClass="Npgsql.NpgsqlDataAdapter" 302 commandBuilderClass="Npgsql.NpgsqlCommandBuilder" 303 usePositionalParameters="false" 304 useParameterPrefixInSql="true" 305 useParameterPrefixInParameter="true" 306 parameterPrefix=":" 307 allowMARS="true" 308 /> 309 <provider 310 name="iDb2.10" 311 description="IBM DB2 Provider, V 10.0" 312 enabled="false" 313 assemblyName="IBM.Data.DB2.iSeries, Version=10.0.0.0,Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26, Custom=null" connectionClass="IBM.Data.DB2.iSeries.iDB2Connection" 314 commandClass="IBM.Data.DB2.iSeries.iDB2Command" 315 parameterClass="IBM.Data.DB2.iSeries.iDB2Parameter" 316 parameterDbTypeClass="IBM.Data.DB2.iSeries.iDB2DbType" 317 parameterDbTypeProperty="iDB2DbType" 318 dataAdapterClass="IBM.Data.DB2.iSeries.iDB2DataAdapter" 319 commandBuilderClass="IBM.Data.DB2.iSeries.iDB2CommandBuilder" 320 usePositionalParameters="true" 321 useParameterPrefixInSql="false" 322 useParameterPrefixInParameter="false" 323 parameterPrefix="" 324 allowMARS="false" 325 /> 326 <provider 327 name="Informix" 328 description="Informix NET Provider, 2.81.0.0" 329 enabled="false" 330 assemblyName="IBM.Data.Informix, Version=2.81.0.0, Culture=neutral, PublicKeyToken=7c307b91aa13d208" 331 connectionClass="IBM.Data.Informix.IfxConnection" 332 commandClass="IBM.Data.Informix.IfxCommand" 333 parameterClass="IBM.Data.Informix.IfxParameter" 334 parameterDbTypeClass="IBM.Data.Informix.IfxType" 335 parameterDbTypeProperty="IfxType" 336 dataAdapterClass="IBM.Data.Informix.IfxDataAdapter" 337 commandBuilderClass="IBM.Data.Informix.IfxCommandBuilder" 338 usePositionalParameters = "true" 339 useParameterPrefixInSql = "false" 340 useParameterPrefixInParameter = "false" 341 useDeriveParameters="false" 342 allowMARS="false" 343 /> 344 </providers>
SqlMap.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > 4 <properties resource="Properties.config"/> 5 6 <settings> 7 <setting useStatementNamespaces="false"/> 8 <setting cacheModelsEnabled="true"/> 9 <setting validateSqlMap="false"/> 10 </settings> 11 12 <database> 13 <provider name="sqlServer1.1"/> 14 <dataSource name="Study" 15 connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=${root}Study.mdf;Integrated Security=True"/> 16 </database> 17 18 <sqlMaps> 19 <sqlMap resource="Maps/User.xml"/> 20 </sqlMaps> 21 </sqlMapConfig>
User.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <sqlMap namespace="Users" 3 xmlns="http://ibatis.apache.org/mapping" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > 5 <alias> 6 <typeAlias alias="User" type="BatisNetStudy.User, BatisNetStudy" /> 7 </alias> 8 9 <statements> 10 <insert id="InsertUser" parameterClass="User"> 11 INSERT INTO [Users] 12 (Id, Name) 13 VALUES 14 (#Id#, #Name#) 15 </insert> 16 </statements> 17 </sqlMap>
测试
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using IBatisNet.DataMapper; 8 9 namespace BatisNetStudy.Demo 10 { 11 class UserTest 12 { 13 public static void InserUser() 14 { 15 var mapper = Mapper.Instance(); 16 17 var user = new User 18 { 19 Id = Guid.NewGuid(), 20 Name = "段光伟" 21 }; 22 mapper.Insert("InsertUser", user); 23 } 24 } 25 }