Başlığı hortlatmak gibi olacak ama bahsi geçen micro-orm' nin java implementasyonunu da tamamladım. Ek olarak .Net ve Java için ufak birer rehber hazıladım, dileyen arkadaşlar deneyebilir;
Kaynak Kodlar;
.Net;
https://github.com/ionixNet
Java;
https://github.com/ionixJava
Kaynak kodların derlenmiş halleri;
.Net;
https://drive.google.com/file/d/0BwknnP7...sp=sharing
Java;
https://drive.google.com/file/d/0BwknnP7...sp=sharing
Veritabanı olarak ünlü Northwind kullanıldı. .Net örneğinde Sql Server, Java için Oracle Kullanılıyor
Kullanırken;
.Net;
Alıntı:
Model Sınıfımız;
Kod:[Table("Products")] public class Products { [DbSchema(IsNullable = false, IsKey = true, DatabaseGeneratedOption = ionix.Data.StoreGeneratedPattern.Identity)] public int ProductID { get; set; } [DbSchema(IsNullable = false, MaxLength = 40)] public string ProductName { get; set; } [DbSchema(IsNullable = true)] public int? SupplierID { get; set; } [DbSchema(IsNullable = true)] public int? CategoryID { get; set; } [DbSchema(IsNullable = true, MaxLength = 20)] public string QuantityPerUnit { get; set; } [DbSchema(IsNullable = true)] public decimal? UnitPrice { get; set; } [DbSchema(IsNullable = true, DefaultValue = "0")] public short? UnitsInStock { get; set; } [DbSchema(IsNullable = true, DefaultValue = "0")] public short? UnitsOnOrder { get; set; } [DbSchema(IsNullable = true, DefaultValue = "0")] public short? ReorderLevel { get; set; } [DbSchema(IsNullable = false, DefaultValue = "0")] public bool Discontinued { get; set; } }
Factory Nesnemiz;
Kod:public static class ionixFactory { private static readonly string ConnetionString = "Data Source=192.168.204.128;Initial Catalog=NORTHWND;User Id=sa;Password=1;"; public static DbConnection CreateEmptyConnection() { return new SqlConnection(); } public static DbConnection CreateDbConnection() { DbConnection conn = CreateEmptyConnection(); conn.ConnectionString = ConnetionString; conn.Open(); return conn; } public static IDbAccess CreatDataAccess(DbConnection connection) { DbAccess dataAccess = new DbAccess(connection); dataAccess.ExecuteSqlComplete += (e) => { try { //Sql Sorgularını loglamak veya görüntülemek için aşağıdaki kodları aktif hale getirin. //string path = e.Succeeded ? "x:\\sql.txt" : "x:\\sqlHata.txt"; //using (Stream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) //{ // using (StreamWriter sw = new StreamWriter(fs)) // { // if (e.Query != null) // { // sw.WriteLine(e.Query.ToParameterlessQuery()); // sw.WriteLine(); // } // } //} } catch { } }; return dataAccess; } public static IDbAccess CreatDataAccess() { return CreatDataAccess(CreateDbConnection()); } public static ITransactionalDbAccess CreateTransactionalDataAccess() { TransactionalDbAccess dataAccess = new TransactionalDbAccess(CreateDbConnection()); #if DEBUG dataAccess.ExecuteSqlComplete += (e) => { try { //string path = e.Succeeded ? "x:\\sql.txt" : "x:\\sqlHata.txt"; //using (Stream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) //{ // using (StreamWriter sw = new StreamWriter(fs)) // { // if (e.Query != null) // { // sw.WriteLine(e.Query.ToParameterlessQuery()); // sw.WriteLine(); // } // } //} } catch { } }; #endif return dataAccess; } public static ICommandFactory CreateFactory(IDbAccess dataAccess) { return new CommandFactory(dataAccess); } public static ICommandAdapter CreateCommand(IDbAccess dataAccess) { return new CommandAdapter(CreateFactory(dataAccess), CreateEntityMetaDataProvider); } public static IRepository<TEntity> CreateRepository<TEntity>(IDbAccess dataAccess) where TEntity : class ,new() { return new Repository<TEntity>(CreateCommand(dataAccess)); } public static IEntityMetaDataProvider CreateEntityMetaDataProvider() { return new DbSchemaMetaDataProvider(); } }
Uygulamadaki örneği;
Kod:using (IDbAccess dataAccess = ionixFactory.CreatDataAccess()) { IRepository<Products> rep = ionixFactory.CreateRepository<Products>(dataAccess); Products entity = rep.SelectById(1); entity.ProductName = "Chai Değişti"; rep.Upsert(entity); IList<Products> entityList = rep.Query("select top 3 * from products ".ToQuery()); rep.BatchInsert(entityList); }
Java;
Alıntı:Model Sınıfımız;
Kod:import ionix.Annotation.DbSchema; import ionix.Annotation.Table; import ionix.Data.StoreGeneratedPattern; import java.math.BigDecimal; @Table(name="PRODUCTS") public class Products { @DbSchema(columnName = "PRODUCTID", isKey = true,databaseGeneratedOption = StoreGeneratedPattern.AutoGenerateSequence) private int productId; public int getProductId(){ return this.productId;} public Products setgetProductId(int value){ this.productId = value; return this; } @DbSchema(columnName = "PRODUCTNAME") private String productName; public String getProductName(){ return this.productName;} public Products setProductName(String value){ this.productName = value; return this; } @DbSchema(columnName = "SUPPLIERID") private int supplierId; public int getSupplierId(){ return this.supplierId;} public Products setSupplierId(int value){ this.supplierId = value; return this; } @DbSchema(columnName = "CATEGORYID") private int categoryId; public int getCategoryId(){ return this.categoryId; } public Products setCategoryId(int value){ this.categoryId = value; return this; } @DbSchema(columnName = "QUANTITYPERUNIT") private String quantityPerUnit; public String getQuantityPerUnit(){ return this.quantityPerUnit;} public Products setQuantityPerUnit(String value){ this.quantityPerUnit = value; return this; } @DbSchema(columnName = "UNITPRICE") private BigDecimal unitPrice; public BigDecimal getUnitPrice(){ return this.unitPrice; } public Products setUnitPrice(BigDecimal value){ this.unitPrice = value; return this; @DbSchema(columnName = "UNITSINSTOCK") private int unitsInStock; public int getUnitsInStock(){ return this.unitsInStock;} public Products setUnitsInStock(int value){ this.unitsInStock = value; return this; } @DbSchema(columnName = "UNITSONORDER") private int unitsInOrder; public int getUnitsInOrder(){ return this.unitsInOrder;} public Products setUnitsInOrder(int value){ this.unitsInOrder = value; return this; } @DbSchema(columnName = "REORDERLEVEL") private int reorderLevel; public int getReorderLevel(){ return this.reorderLevel;} public Products setReorderLevel(int value){ this.reorderLevel = value; return this; } @DbSchema(columnName = "DISCONTINUED") private int discontinued; public int getDiscontinued(){ return this.discontinued;} public Products setDiscontinued(int value){ this.discontinued = value; return this; } }
Factory Nesnemiz;
Kod:import ionix.Data.*; import ionix.Data.DbAccess; import ionix.Data.Oracle.CommandFactory; import ionix.Data.SqlQueryHelper; import ionix.Data.TransactionalDbAccess; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public final class ionixFactory { public static Connection createConnection() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@DB:1521:ORCL", "NORTHWND", "1"); return conn; } catch (ClassNotFoundException ex) { throw new RuntimeException(ex); } catch (SQLException ex) { throw new RuntimeException(ex); } } public static DbAccess creatDataAccess(Connection connection) { DbAccess dataAccess = new DbAccess(connection); dataAccess.onExecuteSqlComplete((e) -> System.out.println(SqlQueryHelper.toParameterlessQuery(e.getQuery())) ); return dataAccess; } public static DbAccess creatDataAccess() { return creatDataAccess(createConnection()); } public static TransactionalDbAccess createTransactionalDbAccess() { TransactionalDbAccess dataAccess = new TransactionalDbAccess(createConnection()); dataAccess.onExecuteSqlComplete((e) -> System.out.println(SqlQueryHelper.toParameterlessQuery(e.getQuery())) ); return dataAccess; } public static CommandFactory CreateFactory(DbAccess dataAccess) { return new CommandFactory(dataAccess); } public static CommandAdapter createCommand(DbAccess dataAccess) { return new CommandAdapter(CreateFactory(dataAccess), createEntityMetaDataProvider()); } public static <TEntity> Repository<TEntity> createRepository(Class<TEntity> cls, DbAccess dataAccess){ return new Repository<TEntity>(cls, createCommand(dataAccess)); } public static EntityMetaDataProvider createEntityMetaDataProvider() { return new DbSchemaMetaDataProvider(); } }
Uygulamadaki örneği;
Kod:try(TransactionalDbAccess dataAccess = ionixFactory.createTransactionalDbAccess()) { Repository<Products> rep = ionixFactory.createRepository(Products.class, dataAccess); Products entity = rep.selectById(1); entity.setProductName("Chai değişti"); rep.update(entity); List<Products> entityList = rep.query("select * from products where rownum <= 3"); rep.batchInsert(entityList); dataAccess.commit(); }
