Ibatis.net implements multi-database configuration, ibatis.net Database
1.1 features
When using the ibatis.net ORM framework, you sometimes need to operate on multiple databases and sometimes encrypt the information of the connected database. In this article, you can write the configuration connection to Web. config,
In this way, the data can be encrypted in Web. config and decrypted and used in the read location.
The following is a specific configuration method. If you have a better method, please note that for the basic knowledge of ibatis.net, refer to other materials.
1.2 configuration process
(1) first configure the database node in sqlmap. config, set a value randomly, and then reset the connection value.
<Database> <provider name = "sqlServer2005"/> <! -- Set a false connection at will and assign a value later --> <dataSource name = "iBatisNet" connectionString = "data source = 11; database = 11; user id = 11; password = 11; connection reset = false; connection lifetime = 5; min pool size = 1; max pool size = 50 "/> </database>
(2) create two entity classes for SqlMap operations (here I will divide the master database and the intermediate database into two databases)
// Used to operate the master database public class BaseDao {
public static readonly BaseDao Instance = new BaseDao();
Private static ISqlMapper sqlMap; // <summary> //// </summary> public BaseDao () {try {// load the current Assembly string sAssembly = this. getType (). assembly. getName (). name; Assembly assembly = Assembly. load (sAssembly); string sqlMapPath = sAssembly + ". config. sqlmap. config "; Stream stream = assembly. getManifestResourceStream (sqlMapPath); DomSqlMapBuilder builder = new DomSqlMapBuilder (); sqlMap = builder. configure (stream); // read the database connection string and set it to a custom string (if encrypted, decryption is required first) string connString = System. configuration. configurationManager. appSettings ["MasterDB"]; sqlMap. dataSource. connectionString = connString;} catch (System. exception ex) {throw ex ;}} /// <summary >/// data operation object for the outer layer // </summary> public ISqlMapper SqlMap {get {return sqlMap ;}}}
Public class MiddleBaseDao {public static readonly MiddleBaseDao Instance = new MiddleBaseDao (); private static ISqlMapper sqlMap; public MiddleBaseDao () {try {// load the current Assembly string sAssembly = this. getType (). assembly. getName (). name; Assembly assembly = Assembly. load (sAssembly); // Load sqlmap. config string sqlMapPath = sAssembly + ". config. sqlmap. config "; Stream stream = assembly. getManifestResourceStream (sqlMapPath); DomSqlMapBuilder builder = new DomSqlMapBuilder (); sqlMap = builder. configure (stream); // read the database connection string and set it to a custom string (Operation intermediate database) string connString = System. configuration. configurationManager. appSettings ["MiddleDB"]; sqlMap. dataSource. connectionString = connString;} catch (System. exception ex) {throw ex ;}} /// <summary >/// data operation object for the outer layer // </summary> public ISqlMapper SqlMap {get {return sqlMap ;}}}
(3) Different SqlMap classes can be called based on different database operations.
public IList<DmsSysPost> FindByCondExt(DmsSysPost obj) { return BaseDao.Instance.SqlMap.QueryForList<DmsSysPost>("DmsSysPost_FindByCondExt", obj); }
public IList<DmsSysPost> FindByCondExt(DmsSysPost obj) { return MiddleBaseDao.Instance.SqlMap.QueryForList<DmsSysPost>("DmsSysPost_FindByCondExt", obj); }
(4) configure two connection strings in Web. config.
<Deleetask> <! -- Master database (which can be encrypted) --> <add key = "MasterDB" value = "data source =.; database = MasterDB; user id =; password ="/> <! -- Intermediate database --> <add key = "MiddleDB" value = "data source =.; database = MiddleDB; user id =; password ="/> </appSettings>
1.3 references
Http://blog.csdn.net/isprotect/article/details/8806974