Mybatis持久层构架

(简单的构架CRUD的复用模板)

/**
 * 数据库处理类回调函数类,类似于spring 的hibernate template 中的回调函数
 * 虽然提倡完整的变量名称写法,但是这里使用了简单的变量、类名(如C),是因为考虑数据层访问非常频繁,
 * 所以用简单的名称节约调用时的代码量
 */
public interface C {
	/**
	 * 回调函数,实际上就在这里写所有的数据库处理逻辑,注意,所有这里面用到的外部变量
	 * 必须申明为final,这一点现在看好像还没有造成太多的困扰
	 * @param ss SqlSession
	 */
	public void d(SqlSession ss);
}
public interface CR {
	/**
	 * @param ss
	 * @return 回调返回结果
	 */
	public Object d(SqlSession ss);
}
下面是复用方法

/**
	 * 数据库业务逻辑实现的主要函数,参考用法:
	 * 
	 * @param c
	 *            构造的匿名回调函数类
	 */
	public void exe(C c) throws MFTException {
		SqlSession session = null;
		try {
			session = SessionFactory.getSession();
			c.d(session);
		} finally {
			session.close();
		}
	}

	/**
	 * 区别于exe在于能够返回结果
	 * 
	 * @param c
	 * @return
	 */
	public Object exer(CR c) {
		SqlSession session = null;
		try {
			session = SessionFactory.getSession();
			return c.d(session);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
		return session;
	}

	/**
	 * @deprecated 请使用exe结合回调函数进行数据库操作,而不是自行使用session进行操作
	 * @throws Exception
	 */
	public void getSession() throws Exception {
		throw new Exception("不允许直接获取mybatis session,请全部通过exe方法进行数据库操作");
	}
	/**
	 * 标准的insert插入方法
	 * @param entity
	 *     要insert到数据库的实体
	 * @return 成功 or 失败   下面用了  反射机制 需要了解一下
	 */
	public boolean add(Object entity) {
		SqlSession session = null;
		try {
			session = SessionFactory.getSession();
			try {
				Object mapper = session.getMapper(Class.forName(entity
						.getClass().getName() + "$Mapper"));//接口下的方法
				Method method = mapper.getClass().getMethod("insert",
						entity.getClass());//insert是你要调用的方法,后面的参数是类
				method.invoke(mapper, entity);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				throw new MFTException(e.toString());
			}
		} finally {
			if(session!=null)
			  session.close();
		}
	}
封装了sqlsession的getmapper方法,可以直接使用而不需要考虑session关闭等问题
@SuppressWarnings("unchecked")
	public <T> T getMapper(Class<T> type) {
		MapperProxy handler = new MapperProxy(type);
		return (T) Proxy.newProxyInstance(type.getClassLoader(),
				new Class[] { type }, handler);
	}

	public void select(String statement, Object parameter,
			ResultHandler handler) {
		SqlSession session = null;
		try {
			session = SessionFactory.getSession();
			try {
				session.select(statement, handler);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new MFTException(e.toString());
			}
		} finally {
			session.close();
		}
	}
class MapperProxy implements InvocationHandler {
	// private SqlSession sqlsession = null;
	private Class<?> type = null;

	public MapperProxy(Class<?> type) {
		// this.sqlsession = sqlsession;
		this.type = type;
	}

	public Object invoke(Object proxy, final Method method,
			final Object[] parames) throws Throwable {
		return new M().exer(new CR() {
			public Object d(SqlSession ss) {
				try {
					return method.invoke(ss.getMapper(type), parames);
				} catch (Exception e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		});

	}

}

sessionFaction 类不在粘贴,打开关闭,是否重建连接,风格自己定

上面一共定义了,两种方式,

1, 通过代理 mm.getMapper(WyUser2.Mapper.class).queryCartAndListByOpenId("456456"); 调用实体里面的接口方法

2,直接调用 模板方法 mm.add(user2); 复用模板

下面是个junit测试

mm.exe(new C() {		
		@Override
public void d(SqlSession ss) {
		// TODO Auto-generated method stub	
}
})


欢迎技术交流;







猜你喜欢

转载自blog.csdn.net/Uniquelike/article/details/25409051