Giter VIP home page Giter VIP logo

db-orm's Introduction

DB-ORM

If you dont like the Hibernate or some else big bloated ORM frameworks, you should try this to write a new ORM framework of your own. :D

Introduction

原理介绍文章参见这里

注意事项:

  • 默认使用tomcat的链接池(其他连接池请自行build,或fork扩展)
  • 默认事务自动提交(一个用例多次查询,请手动创建事务,避免数据库创建多个事务)
  • 只对常用数据库操作进行伪ORM封装(特殊查询请使用jdbc)
  • 无任何缓存

Installation

下载源码,使用maven构建

Example

使用之前你需要创建一个module,这里所有的字段都要使用封装类型,而不是直接类型。就是说int要用Integer代替,float要用Float代替。

public class User{
	@Id
	private Integer id;
	private String name;
	private Integer sex;	// 1 for male and 2 for female
	private Integer age;

	// getter and setter
}

新增

public static void main(String[] args) {
	Dao dao=Dao.getInstances();
	User u=new User();
	u.setName("a");
	u.setSex(1);
	u.setAge(24);
	try {
		int retId=dao.save(u);
		// do something..
	} catch (Exception e) {
		e.printStackTrace();
	}
}

更新

public static void main(String[] args) {
	Dao dao=Dao.getInstances();
	User u=new User();
	u.setId(123);
	u.setName("Mike");
	try {
		int effect=dao.update(u);
		// do something..
	} catch (Exception e) {
		e.printStackTrace();
	}
}

删除

public static void main(String[] args) {
	Dao dao=Dao.getInstances();
	User u=new User();
	u.setId(123);
	try {
		int effect=dao.delete(u);
		// do something..
	} catch (Exception e) {
		e.printStackTrace();
	}
}

查询

public static void main(String[] args) {
	Dao dao=Dao.getInstances();
	SqlQuery query=new SqlQuery();	// the Query object
	query.appendSql("select * from user where sex = ? ");
	query.addParam(1);
	try {
		List<User> users=dao.getList(query);
		// do something..
	} catch (Exception e) {
		e.printStackTrace();
	}
}

Automation

可以使用 ModuleGenerator.generate("tabel_name"); 自动生成实体映射文件。

db-orm's People

Contributors

leeyaf avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

sishui198

db-orm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.