ECSide核心模块介绍之:EasyDataAccess,数据存取模块.
ECSide 里加入了 easyList特性。可以通过在xml里配制sql语句来快速的展现列表。
最近我将该功能模块进行了重构,使其可以脱离ecside来使用,从而可以用来简化其他的数据库操作。
具体用法结合下面的例子来讲解:
示例 :
对 USER_INFO 表进行查询。 查询条件为 USERNAME GENDER。
第1步:定义spring文件:
<bean id="testQuery1" ref="test_datasource" /> <property name="sqls"> <map> <!-- 这个key的名字随便取 --> <entry key="queryInfoByNameGender" ><value>select * from user_info where 1=1 #{IF:USERNAME!=EMPTY} and username like #{USERNAME} #{/IF}#{IF:GENDER!=EMPTY} and gender=#{GENDER} #{/IF}</value></entry> </map> </property> </bean>
// 这里也可以使用 Ioc 注入。DataAccessModel dam=(DataAccessModel)factory.getBean("testQuery1");//parameterMap为传入参数,key为USERNAME GENDER。List list=(List)dam.executeQuery("queryInfoByNameGender", parameterMap);
public static Map getParameterMap(HttpServletRequest request) {Map parameterMap = new HashMap();Enumeration names = request.getParameterNames();while (names.hasMoreElements()) {String name = (String) names.nextElement();String[] s = request.getParameterValues(name);if (s != null) {if (s.length == 1) {parameterMap.put(name, request.getParameterValues(name)[0]);} else {parameterMap.put(name, request.getParameterValues(name));}}}return parameterMap;}
<entry key="queryInfoByNameGender" ><value>select * from user_info where 1=1 #{IF:i_user_name!=EMPTY} and username like #{i_user_name} #{/IF}#{IF:i_user_gender!=EMPTY} and gender=#{i_user_gender} #{/IF}</value></entry>
<bean id="myEasyDA" ref="test_datasource" /> <property name="sqls"> <map> <entry key="baseInfo1" ><value>{call CM_CUST_REAL_INFO_P(#{I_SERVICE_KIND},#{I_SERVICE_NO},#{I_CITY_CODE},#{I_SERVICE_ID},#{I_CUSTOMER_ID},#{OUT:OUT_F_CUST_NAME},#{OUT:OUT_F_GENDER},#{OUT:OUT_F_DUTY},#{OUT:OUT_F_PRODUCT_NAME},#{OUT:OUT_F_POINT_VALUE},#{OUT:OUT_F_STATUS},#{OUT:ON_FLAG},#{OUT:OS_PROMPT})}</value></entry> <entry key="updateUser" ><value>update user_info set passwd=#{PASSWD} , userrole=#{USERROLE} ,email=#{EMAIL}, gender=#{GENDER} WHERE userid=#{USERID}</value></entry> <entry key="deleteUser" ><value>delete from user_info WHERE userid=#{USERID}</value></entry> <entry key="getUserMemo" ><value>select MEMO,EMAILfrom user_info WHERE userid=#{USERID}</value></entry> </map> </property> </bean>
public class MyDataAccessModel extends DataAccessModel {public void registerInterceptors(){addInterceptor(new DataAccessInterceptor("updateUser"){public void before(Map parameterMap, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}public void after(Map parameterMap, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setAttribute(ECSideConstants.C_UPDATE_RESULT_MESSAGE,request.getParameter("USERNAME"));ServletUtil.writeDefaultTextToClient(parameterMap, request, response);}});addInterceptor(new DataAccessInterceptor("deleteUser"){public void before(Map parameterMap, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}public void after(Map parameterMap, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setAttribute(ECSideConstants.C_UPDATE_RESULT_MESSAGE,request.getParameter("USERNAME"));ServletUtil.writeDefaultTextToClient(parameterMap, request, response);}});}}