日期:2014-05-16  浏览次数:20407 次

no sql jdbc orm[5-18]
最近几天在写的一个jdbc orm, 放上来交流学习~~

目标:
no sql, no map , no sql, no map...............................(省略一百遍 ).

约束&特点:
显示代理
操纵proxy domain getter/setter等于构建sql, 重构对象即重构sql
domain对象属性和column名字相同
domain有接口
domain是javabean
某些dao接口参数需要beanproxy(主要是update的接口)
基本没有级联orm


待实现:
多表查询
稍微复杂的sql生成
考虑local&remote Object cache,query cache...

dao example:
	**
 * @version 2007-5-4
 * @author xalinx at gmail dot com
 * 
 */
public class UserDaoImpl extends DaoSupport<User, Long> implements UserDao {
	private final ParameterizedRowMapper<UserImpl> rowMapper = new AutoRowMapper<UserImpl>() {
	};

	public void deleteById(Long id) {
		// create proxy
		BeanMonitor<User> monitor = new BeanMonitor<User>();
		User proxy = monitor.proxy(new UserImpl());

		// proxy where
		proxy.setId(id);
		monitor.eq();

		getSimpleDaoTemplate().proxyDelete(monitor);
	}

	public User findById(Long id) {
		// create proxy
		BeanMonitor<User> monitor = new BeanMonitor<User>();
		User proxy = monitor.proxy(new UserImpl());

		// proxy select
		proxy.getId();
		proxy.getUsername();
		proxy.getPassword();
		proxy.getNickname();
		proxy.getCity();
		proxy.getProv();
		proxy.getUserStatus();
		proxy.getCreateTime();
		proxy.getModifyTime();

		// proxy where
		proxy.setId(id);
		monitor.eq();

		return getSimpleDaoTemplate().proxyQueryObject(monitor, rowMapper);
	}

	public int findCount() {
		return getSimpleDaoTemplate().findCount(UserImpl.class);
	}

	public void store(User u) {
		// integrity validate
		if (u == null || u.getId() <= 0 || u.getUserStatus() < 0 || u.getUsername() == null || u.getPassword() == null
				|| u.getNickname() == null || u.getCreateTime() == null || u.getModifyTime() == null) {
			throw new DataIntegrityViolationException(u.toString());
		}

		BeanMonitor<User> monitor = new BeanMonitor<User>();
		User proxy = monitor.proxy(new UserImpl());

		// proxy insert
		proxy.setId(u.getId());
		proxy.setUsername(u.getUsername());
		proxy.setPassword(u.getPassword());
		proxy.setNickname(u.getNickname());
		proxy.setCity(u.getCity());
		proxy.setProv(u.getProv());
		proxy.setUserStatus(u.getUserStatus());
		proxy.setCreateTime(u.getCreateTime());
		proxy.setModifyTime(u.getModifyTime());

		// store
		getSimpleDaoTemplate().proxyStore(monitor);
	}

	public void updateById(BeanMonitor<User> monitor) {
		User u = monitor.getBean();
		// integrity validate
		if (u == null || u.getId() <= 0) {
			throw new DataIntegrityViolationException(u.toString());
		}

		// proxy where
		User proxy = monitor.getProxy();
		monitor.where();
		proxy.setId(u.getId());
		monitor.eq();

		// update
		getSimpleDaoTemplate().proxyUpdate(monitor, u.getId());
	}

	public BasePage<User> findPage(UserPageQuery pageQuery) {
		// create proxy
		BeanMonitor<User> monitor = new BeanMonitor<User>();
		User proxy = monitor.proxy(new UserImpl());

		// proxy select
		proxy.getId();
		proxy.getUsername();
		proxy.getCreateTime();

		// proxy where
		if (null != pageQuery.getUser().getCity()) {
			proxy.setCity(pageQuery.getUser().getCity());
			monitor.eq();
		}
		if (null != pageQuery.getEndCreateTime()) {
			proxy.setCreateTime(pageQuery.getEndCreateTime());
			monitor.and().leeq();
		}
		if (null != pageQuery.getStartCreateTime()) {
			proxy.setCreateTime(pageQuery.getStartCreateTime());
			monitor.and().gteq();
		}
		if (null != pageQuery.getUser().getUsername()) {
			proxy.setUsername(pageQuery.getUser().getUsername());
			monitor.and().like();
		}

		// query & orm & page
		return getSimpleDaoTemplate().proxyQueryPage(monitor, rowMapper, pageQuery);
	}

}

1 楼 ali