controller:
package com.ws.byd.bmgl.bmzdpz.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.ws.byd.bmgl.bmzdpz.service.BmzdpzManager; import com.ws.byd.jcsj.gxgl.service.GxglManager; import com.ws.core.utils.DateUtils; import com.ws.core.web.utils.Pager; import com.ws.sys.core.model.User; import com.ws.sys.core.utils.IDGenerator; import com.ws.sys.core.utils.JsonUtil; import com.ws.sys.core.web.controller.BaseController; @Controller @RequestMapping("/byd/bmzdpz/") public class BmzdpzController extends BaseController { @Autowired private BmzdpzManager bmzdpzManager; @RequestMapping("bmzdpzQuery") public ModelAndView bmzdpzQuery(HttpServletRequest request){ ModelAndView mav = new ModelAndView("/byd/bmgl/bmzdpz/bmzdpz_query_ztree"); Map<String,String> map = super.getParamValues(request); mav.addObject("map", map); return mav; } @RequestMapping("bmzdpzZtree") public void bmzdpzZtree(HttpServletRequest request,HttpServletResponse response){ Map<String,String> map = super.getParamValues(request); User user = (User) request.getSession().getAttribute("userm"); List<Map> treeList = bmzdpzManager.getBmzdpzZtree(map); super.flushResponse(response, JsonUtil.toJson(treeList)); } @RequestMapping("bmzdpzListQuery") public ModelAndView bmzdpzListQuery(HttpServletRequest request){ ModelAndView mav = new ModelAndView("/byd/bmgl/bmzdpz/bmzdpz_query_list"); Map map = super.getParamValues(request); map = super.pageXX(map, request); Pager pager = bmzdpzManager.getBmzdpzListQuery(map); mav.addObject("pager", pager); mav.addObject("map", map); return mav; } @RequestMapping("bmzdpzZdxAdd") public ModelAndView bmzdpzZdxAdd(HttpServletRequest request){ ModelAndView mav = new ModelAndView("/byd/bmgl/bmzdpz/bmzdpz_zdx_add"); Map map = super.getParamValues(request); mav.addObject("map", map); return mav; } @RequestMapping("bmzdpzZdxIsExists") public void bmzdpzZdxIsExists(HttpServletRequest request,HttpServletResponse response){ int count = 0; Map<String,String> map = super.getParamValues(request); List<Map> resultList = bmzdpzManager.getBmzdpzZdxList(map); if(null != resultList && resultList.size() > 0) { count = 1; } super.flushResponse(response, String.valueOf(count)); } @RequestMapping("bmzdpzZdxAddSave") public void bmzdpzZdxAddSave(HttpServletRequest request,HttpServletResponse response){ Map map = super.getParamValues(request); Map<String,String[]> maps = getParameterValues(request); User user = (User) request.getSession().getAttribute("userm"); map.put("djrxm", user.getName()); map.put("djrgh", user.getIdnumber()); map.put("djsj", DateUtils.getCurrentTimeAs14String()); int count = bmzdpzManager.getBmzdpzZdxAddSave(map,maps); super.flushResponse(response, String.valueOf(count)); } @RequestMapping("bmzdpzZdxEdit") public ModelAndView bmzdpzZdxEdit(HttpServletRequest request){ ModelAndView mav = new ModelAndView("/byd/bmgl/bmzdpz/bmzdpz_zdx_edit"); Map map = super.getParamValues(request); Map resultMap = new HashMap(); List<Map> resultList = bmzdpzManager.getBmzdpzZdxList(map); if(null != resultList && resultList.size() > 0) { resultMap = resultList.get(0); } mav.addObject("resultMap", resultMap); mav.addObject("map", map); return mav; } @RequestMapping("bmzdpzZdxEditSave") public void bmzdpzZdxEditSave(HttpServletRequest request,HttpServletResponse response){ Map map = super.getParamValues(request); int count = bmzdpzManager.getBmzdpzZdxEditSave(map); super.flushResponse(response, String.valueOf(count)); } @RequestMapping("bmzdpzZdxDeleteSave") public void bmzdpzZdxDelete(HttpServletRequest request,HttpServletResponse response){ Map map = super.getParamValues(request); int count = 0; List<Map> list = bmzdpzManager.getBmzdpzgzQuery(map); if(list.size() == 0) { count = bmzdpzManager.getBmzdpzZdxDelSave(map); } super.flushResponse(response, String.valueOf(count)); } }
service:
interface BmzdpzManager----》
BmzdpzManagerImpl implements BmzdpzManager-------》
BmzdpzSql
接口
package com.ws.byd.bmgl.bmzdpz.service; import java.util.List; import java.util.Map; import com.ws.core.web.utils.Pager; public interface BmzdpzManager { List<Map> getBmzdpzZtree(Map map); Pager<Map<String,Object>> getBmzdpzListQuery(Map map); List<Map> getBmzdpzZdxList(Map map); int getBmzdpzZdxAddSave(Map map,Map<String,String[]> maps); int getBmzdpzZdxEditSave(Map map); int getBmzdpzZdxDelSave(Map map); Map<String,String> getBmzdpzZdx(); List<Map> getBmzdpzgzQuery(Map map); }
BmzdpzManagerImpl
package com.ws.byd.bmgl.bmzdpz.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Resource; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.ws.byd.bmgl.bmzdpz.service.BmzdpzManager; import com.ws.byd.bmgl.util.BmzdCacheUtil; import com.ws.byd.htgl.ftpgl.service.impl.FtpglSql; import com.ws.byd.htgl.jsgl.service.impl.JsglSql; import com.ws.byd.htgl.mkgl.service.impl.MkglSql; import com.ws.byd.jcsj.gxgl.service.GxglManager; import com.ws.byd.jcsj.lglr.service.impl.LglrSql; import com.ws.byd.jcsj.pcbagslr.service.impl.PcbagslrSql; import com.ws.core.web.utils.Pager; import com.ws.db.modle.SqlBuilder; import com.ws.db.service.SqlExecuteManager; import com.ws.sys.core.utils.IDGenerator; @Service("bmzdpzManager") public class BmzdpzManagerImpl implements BmzdpzManager { @Autowired private SqlExecuteManager sqlExecuteManager; @Resource(name = "sqlSessionFactory_2") private SqlSessionFactory sqlSessionFactory_2; @Override public List<Map> getBmzdpzZtree(Map map) { List<Map> treeList = new ArrayList<Map>(); Map rootMap = new HashMap(); rootMap.put("id", "0"); rootMap.put("pid", ""); rootMap.put("mc", "编码字典"); rootMap.put("dm", "root"); rootMap.put("level", "0"); treeList.add(rootMap); String sql = BmzdpzSql.getBmzdpzListSql(map); SqlBuilder sqlBuilder = new SqlBuilder(sql); List<Map> resultList = sqlExecuteManager.getResultList(sqlBuilder, sqlSessionFactory_2); treeList.addAll(resultList); return treeList; } @Override public Pager<Map<String, Object>> getBmzdpzListQuery(Map map) { int page = 1; int pageSize = 10; if(map.get("pageNumber") != null && !"".equals(map.get("pageNumber").toString())){ page = Integer.parseInt(map.get("pageNumber").toString()); } if(map.get("pageSize") != null && !"".equals(map.get("pageSize").toString())){ pageSize = Integer.parseInt(map.get("pageSize").toString()); } String sql = BmzdpzSql.getBmzdpzListSql(map); int count = sqlExecuteManager.getFalseResultCount(new SqlBuilder(sql), sqlSessionFactory_2); Pager<Map<String, Object>> pager = new Pager<Map<String, Object>>(count, pageSize, page); List<Map<String, Object>> resulList = sqlExecuteManager.getResultList(new SqlBuilder(sql+" limit "+pager.getStartIndex()+","+pageSize), sqlSessionFactory_2); pager.setResults(resulList); return pager; } @Override public List<Map> getBmzdpzZdxList(Map map) { return sqlExecuteManager.getResultList(new SqlBuilder(BmzdpzSql.getBmzdpzListSql(map)), sqlSessionFactory_2); } @Transactional("txManager_2") @Override public int getBmzdpzZdxAddSave(Map map, Map<String, String[]> maps) { int count = 0; List<Map> zdxList = new ArrayList<Map>(); String[] mc = maps.get("mc"); String[] dm = maps.get("dm"); for(int i=0;i<dm.length;i++) { Map zdxMap = new HashMap(); zdxMap.put("id", IDGenerator.getUid()); zdxMap.put("pid", map.get("pid")); zdxMap.put("mc", mc[i]); zdxMap.put("dm", dm[i]); zdxMap.put("level", "2"); zdxMap.put("djrgh", map.get("djrgh")); zdxMap.put("djrxm", map.get("djrxm")); zdxMap.put("djsj", map.get("djsj")); zdxList.add(zdxMap); } count += sqlExecuteManager.insertBath(new SqlBuilder(BmzdpzSql.insertBmzdpzZdxSql(map),zdxList), sqlSessionFactory_2); BmzdCacheUtil.getInstance().flushBmzdCache(); return count; } @Override public int getBmzdpzZdxEditSave(Map map) { int count = 0; BmzdCacheUtil.getInstance().flushBmzdCache(); count += sqlExecuteManager.update(new SqlBuilder(BmzdpzSql.updateBmzdpzZdxSql(map)), sqlSessionFactory_2); return count; } @Override public int getBmzdpzZdxDelSave(Map map) { int count = 0; BmzdCacheUtil.getInstance().flushBmzdCache(); count += sqlExecuteManager.delete(new SqlBuilder(BmzdpzSql.deleteBmzdpzZdxSql(map)), sqlSessionFactory_2); return count; } @Override public List<Map> getBmzdpzgzQuery(Map map) { return sqlExecuteManager.getResultList(new SqlBuilder(BmzdpzSql.getBmzdpzgzSql(map)), sqlSessionFactory_2); } @Override public Map<String, String> getBmzdpzZdx() { Map<String, String> zdxMap = new HashMap<String, String>(); Map<String,String> param = new HashMap<String, String>(); param.put("level", "2"); String sql = BmzdpzSql.getBmzdpzListSql(param); SqlBuilder sqlBuilder = new SqlBuilder(sql); List<Map> resultList = sqlExecuteManager.getResultList(sqlBuilder, sqlSessionFactory_2); for(Map result:resultList) { zdxMap.put(ObjectUtils.toString(result.get("id")), ObjectUtils.toString(result.get("mc"))); } return zdxMap; } }
BmzdpzSql
package com.ws.byd.bmgl.bmzdpz.service.impl; import java.util.Map; import com.ws.common.utils.GetCanShuUtil; public class BmzdpzSql extends GetCanShuUtil { public static String getBmzdpzListSql(Map map) { StringBuffer sql = new StringBuffer(); sql.append("select t.id,t.pid,t.mc,t.dm,t.level from t_bmgl_zdxx t where 1=1"); if(!isEmpty(map, "fid")) { sql.append(" and t.pid = "+getCanShu(map, "fid")); } if(!isEmpty(map, "id")) { sql.append(" and t.id = "+getCanShu(map, "id")); } if(!isEmpty(map, "ckdm")) { sql.append(" and t.dm = "+getCanShu(map, "dm")); } if(!isEmpty(map, "mc")) { sql.append(" and t.mc like "+getCanShuLike(map, "mc","1")); } if(!isEmpty(map, "dm")) { sql.append(" and t.dm like "+getCanShuLike(map, "dm","1")); } if(!isEmpty(map, "level")) { sql.append(" and t.level = "+getCanShu(map, "level")); sql.append(" order by t.id"); }else { sql.append(" order by t.id desc"); } return sql.toString(); } public static String insertBmzdpzZdxSql(Map map) { StringBuffer sql = new StringBuffer(); sql.append("insert into t_bmgl_zdxx (id,pid,mc,dm,level,djrgh,djrxm,djsj)"); sql.append(" values("); sql.append("#{id,jdbcType=VARCHAR},#{pid,jdbcType=VARCHAR},#{mc,jdbcType=VARCHAR},#{dm,jdbcType=VARCHAR},"); sql.append("#{level,jdbcType=VARCHAR},#{djrgh,jdbcType=VARCHAR},#{djrxm,jdbcType=VARCHAR},#{djsj,jdbcType=VARCHAR}"); sql.append(")"); return sql.toString(); } public static String updateBmzdpzZdxSql(Map map) { StringBuffer sql = new StringBuffer(); sql.append("update t_bmgl_zdxx set "); sql.append("mc="+getCanShu(map, "mc")); sql.append(" where id="+getCanShu(map, "id")); return sql.toString(); } public static String deleteBmzdpzZdxSql(Map map) { String sql = "delete from t_bmgl_zdxx where id="+getCanShu(map, "id"); return sql; } public static String getBmzdpzgzSql(Map map) { String sql = "select * from t_bmgl_gzxx where gzid like "+getCanShuLike(map, "id", "1"); return sql; } }
util
package com.ws.byd.bmgl.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.ws.byd.bmgl.bmzdpz.service.BmzdpzManager; import com.ws.byd.common.dictionary.service.BydDicItemManager; import com.ws.bzjggl.sjygl.service.SjyglManager; import com.ws.core.db.AutoCachedb; import com.ws.core.db.DataSourceFactory; import com.ws.sys.core.utils.ServiceLocator; public class BmzdCacheUtil { private static Logger logger = Logger.getLogger(BmzdCacheUtil.class); private static final BmzdCacheUtil instance = new BmzdCacheUtil(); Map<String,String> zdxMap = new HashMap<String,String>(); private BmzdCacheUtil() { BmzdpzManager bmzdpzManager = (BmzdpzManager) ServiceLocator.getInstance().getService(BmzdpzManager.class); logger.info("加载编码字典..."); this.zdxMap = bmzdpzManager.getBmzdpzZdx(); logger.info("编码字典加载完成...."); } public static BmzdCacheUtil getInstance() { return instance; } public String getBmzdCacheZdx(String id) { String name = ""; String[] ids = id.split(","); if(ids.length > 0) { for(String str:ids) { if(!"*".equals(str)) { name += zdxMap.get(str)+","; }else { name += "*,"; } } name = name.substring(0, name.length()-1); } return name; } public void flushBmzdCache() { BmzdpzManager bmzdpzManager = (BmzdpzManager) ServiceLocator.getInstance().getService(BmzdpzManager.class); //通过反射获取类里面的方法 this.zdxMap = bmzdpzManager.getBmzdpzZdx(); } }
interface SqlExecuteManager
package com.ws.db.service; import java.util.List; import org.apache.ibatis.session.SqlSessionFactory; import com.ws.core.web.utils.FalsePager; import com.ws.core.web.utils.Pager; import com.ws.db.modle.SqlBuilder; import com.ws.db.modle.TableOptionBuilder; public interface SqlExecuteManager { public <T> T getResultOne(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public <T> T callProcedure(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public <T> List<T> getResultList(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public int insert(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public int delete(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public int update(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public int insertBath(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); public <T> T QueryAndOperation(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory,QueryResultHandle<T> queryResultHandle); boolean createTable(TableOptionBuilder tableOptionBuilder, SqlSessionFactory sqlSessionFactory); /***** * 假分页获取查询结果集 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page 页码 * @return */ public <T> FalsePager<T> getResultFalsePager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page); /***** * 假分页获取查询结果集 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page页码 * @param row 每页记录数 * @return */ public <T> FalsePager<T> getResultFalsePager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page, int row); /****** * 统计符合条件的资源总数,多和假分页配套使用 * @param sqlBuilder * @param sqlSessionFactory * @return */ int getFalseResultCount(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory); /**** * 假分页 替换 sql语句中的排序字段,要求sql语句中必须要有order by且必须后面跟有asc或者desc,否则会替换出错 * 可以单独指定排序字段,用于需要在列表点击属性进行排序 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page 页码 * @param row 每页记录数 * @param sort 排序字段 新指定的排序字段,多个字段以逗号分隔,如果不指定排序规则,则默认为降序排序; * @return */ public <T> FalsePager<T> getResultFalsePager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page, int row, String sort); /**** * 假分页 替换 sql语句中的排序字段,要求sql语句中必须要有order by且必须后面跟有asc或者desc,否则会替换出错 * 可以单独指定排序字段,用于需要在列表点击属性进行排序 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page 页码 * @param sort 排序字段 新指定的排序字段,多个字段以逗号分隔,如果不指定排序规则,则默认为降序排序; * @return */ public <T> FalsePager<T> getResultFalsePager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page, String sort); /****** * 真分页 默认每页展示10条记录 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page * @return */ public <T> Pager<T> getResultPager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page); /***** * 真分页 需要指定每页展示的记录数 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page 页码 * @param row 每页展示的记录数 * @return */ public <T> Pager<T> getResultPager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page, int row); /***** * 真分页 需要指定排序字段 替换 sql语句中的排序字段,要求sql语句中必须要有order by且必须后面跟有asc或者desc,否则会替换出错 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page 页码 * @param row 每页展示的记录条数 * @param sort 排序字段 新指定的排序字段,多个字段以逗号分隔,如果不指定排序规则,则默认为降序排序; * @return */ public <T> Pager<T> getResultPager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page, int row, String sort); /***** * 真分页 需要指定排序字段 不需要指定每页展示的记录数 替换 sql语句中的排序字段,要求sql语句中必须要有order by且必须后面跟有asc或者desc,否则会替换出错 * @param <T> * @param sqlBuilder * @param sqlSessionFactory * @param page 页码 * @param sort 排序字段 新指定的排序字段,多个字段以逗号分隔,如果不指定排序规则,则默认为降序排序; * @return */ public <T> Pager<T> getResultPager(SqlBuilder sqlBuilder, SqlSessionFactory sqlSessionFactory, int page, String sort); }
============================================================================================================
Autowired注解
Resource注解
- @Resource注解和@Autowired注解都可以用作bean的注入.
- 在接口只有一个实现类的时候,两个注解可以互相替换,效果相同.
@Resource注解是Java自身的注解,@Autowired注解是Spring的注解.
@Resource注解有两个重要的属性,分别是name和type,如果name属性有值,则使用byName的自动注入策略,将值作为需要注入bean的名字,如果type有值,则使用byType自动注入策略,将值作为需要注入bean的类型.如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。即@Resource注解默认按照名称进行匹配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
@Autowired注解是spring的注解,此注解只根据type进行注入,不会去匹配name.但是如果只根据type无法辨别注入对象时,就需要配合使用@Qualifier注解或者@Primary注解使用.
总结
一个接口,俩个实现类(impl),Autowired就不知道注入哪一个实现类(得与@Qualifier(指定我要注入哪一个类)注解搭配使用才知道),而Resource有name属性,可以区分要注入哪一个实现类。