JEECG-自己用代码生成器生成的controller类是怎样进行数据持久化的

来源:互联网 发布:什么事windows原版系统 编辑:程序博客网 时间:2024/06/02 08:29
比如我用代码生成器生成了一个UnitInfoEntity.java的实体类,同时生成了UnitInfoController.java的控制类,在改类中用到unitInfoService进行数据的增删改查,

unitInfoService的声明方式是这个样的:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @Autowired  
  2.     private UnitInfoServiceI <span style="color:#ff0000;">unitInfoService</span>;  

查看UnitInfoServiceI接口,代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package jeecg.gwc.db.service.unitinfo;  
  2.   
  3. import org.jeecgframework.core.common.service.CommonService;  
  4.   
  5. public interface UnitInfoServiceI extends CommonService{  
  6.   
  7. }  

并没有任何方法的声明。

再看一下UnitInfoServiceI实现类UnitInfoServiceImpl的代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package jeecg.gwc.db.service.impl.unitinfo;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4. import org.springframework.transaction.annotation.Transactional;  
  5.   
  6. import jeecg.gwc.db.service.unitinfo.UnitInfoServiceI;  
  7. import org.jeecgframework.core.common.service.impl.CommonServiceImpl;  
  8.   
  9. <span style="color:#ff0000;">@Service("unitInfoService")  
  10. </span>@Transactional  
  11. public class UnitInfoServiceImpl extends CommonServiceImpl implements UnitInfoServiceI {  
  12.       
  13. }  


可以看到该类继承了CommonServiceImpl(org.jeecgframework.core.common.service.impl.)类,实现了UnitInfoServiceI(jeecg.gwc.db.service.unitinfo.)接口

可以看到该类上方有个注解:@Service("unitInfoService") 括号里的名称跟Controller中service的变量名称一致。所以应该是Spring将UnitInfoServiceImpl的对象注入到Controller类。

也就是用了UnitInfoServiceImpl的对象。

那么这个对象中有什么具体的方法吗?

就要看继承的CommonServiceImpl有哪些方法了。

看下CommonServiceImpl的具体源码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.jeecgframework.core.common.service.impl;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Collection;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import javax.annotation.Resource;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import jeecg.system.pojo.base.TSDepart;  
  12.   
  13. import org.hibernate.Session;  
  14. import org.jeecgframework.core.common.dao.ICommonDao;  
  15. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;  
  16. import org.jeecgframework.core.common.hibernate.qbc.HqlQuery;  
  17. import org.jeecgframework.core.common.hibernate.qbc.PageList;  
  18. import org.jeecgframework.core.common.model.common.DBTable;  
  19. import org.jeecgframework.core.common.model.common.UploadFile;  
  20. import org.jeecgframework.core.common.model.json.ComboTree;  
  21. import org.jeecgframework.core.common.model.json.DataGridReturn;  
  22. import org.jeecgframework.core.common.model.json.ImportFile;  
  23. import org.jeecgframework.core.common.model.json.TreeGrid;  
  24. import org.jeecgframework.core.common.service.CommonService;  
  25. import org.jeecgframework.tag.vo.datatable.DataTableReturn;  
  26. import org.jeecgframework.tag.vo.easyui.Autocomplete;  
  27. import org.jeecgframework.tag.vo.easyui.ComboTreeModel;  
  28. import org.jeecgframework.tag.vo.easyui.TreeGridModel;  
  29. import org.springframework.stereotype.Service;  
  30. import org.springframework.transaction.annotation.Transactional;  
  31.   
  32.   
  33. @Service("commonService")  
  34. @Transactional  
  35. public class <span style="color:#ff0000;">CommonServiceImpl </span>implements CommonService {  
  36.     public ICommonDao commonDao = null;  
  37.     /** 
  38.      * 获取所有数据库表 
  39.      * @return 
  40.      */  
  41.     public List<DBTable> getAllDbTableName()  
  42.     {  
  43.         return commonDao.getAllDbTableName();  
  44.     }  
  45.     public Integer getAllDbTableSize() {  
  46.         return commonDao.getAllDbTableSize();  
  47.     }  
  48.     @Resource  
  49.     public void setCommonDao(ICommonDao commonDao) {  
  50.         this.commonDao = commonDao;  
  51.     }  
  52.   
  53.     @Override  
  54.     public <T> void save(T entity) {  
  55.         commonDao.save(entity);  
  56.     }  
  57.   
  58.     @Override  
  59.     public <T> void saveOrUpdate(T entity) {  
  60.         commonDao.saveOrUpdate(entity);  
  61.   
  62.     }  
  63.   
  64.     @Override  
  65.     public <T> void delete(T entity) {  
  66.         commonDao.delete(entity);  
  67.   
  68.     }  
  69.   
  70.     /** 
  71.      * 删除实体集合 
  72.      *  
  73.      * @param <T> 
  74.      * @param entities 
  75.      */  
  76.     public <T> void deleteAllEntitie(Collection<T> entities) {  
  77.         commonDao.deleteAllEntitie(entities);  
  78.     }  
  79.   
  80.     /** 
  81.      * 根据实体名获取对象 
  82.      */  
  83.     public <T> T get(Class<T> class1, Serializable id) {  
  84.         return commonDao.get(class1, id);  
  85.     }  
  86.   
  87.     /** 
  88.      * 根据实体名返回全部对象 
  89.      *  
  90.      * @param <T> 
  91.      * @param hql 
  92.      * @param size 
  93.      * @return 
  94.      */  
  95.     public <T> List<T> getList(Class clas) {  
  96.         return commonDao.loadAll(clas);  
  97.     }  
  98.   
  99.     /** 
  100.      * 根据实体名获取对象 
  101.      */  
  102.     public <T> T getEntity(Class entityName, Serializable id) {  
  103.         return commonDao.getEntity(entityName, id);  
  104.     }  
  105.   
  106.     /** 
  107.      * 根据实体名称和字段名称和字段值获取唯一记录 
  108.      *  
  109.      * @param <T> 
  110.      * @param entityClass 
  111.      * @param propertyName 
  112.      * @param value 
  113.      * @return 
  114.      */  
  115.     public <T> T findUniqueByProperty(Class<T> entityClass, String propertyName, Object value) {  
  116.         return commonDao.findUniqueByProperty(entityClass, propertyName, value);  
  117.     }  
  118.   
  119.     /** 
  120.      * 按属性查找对象列表. 
  121.      */  
  122.     public <T> List<T> findByProperty(Class<T> entityClass, String propertyName, Object value) {  
  123.   
  124.         return commonDao.findByProperty(entityClass, propertyName, value);  
  125.     }  
  126.   
  127.     /** 
  128.      * 加载全部实体 
  129.      *  
  130.      * @param <T> 
  131.      * @param entityClass 
  132.      * @return 
  133.      */  
  134.     public <T> List<T> loadAll(final Class<T> entityClass) {  
  135.         return commonDao.loadAll(entityClass);  
  136.     }  
  137.       
  138.     public <T> T singleResult(String hql)  
  139.     {  
  140.         return commonDao.singleResult(hql);  
  141.     }  
  142.   
  143.     /** 
  144.      * 删除实体主键ID删除对象 
  145.      *  
  146.      * @param <T> 
  147.      * @param entities 
  148.      */  
  149.     public <T> void deleteEntityById(Class entityName, Serializable id) {  
  150.         commonDao.deleteEntityById(entityName, id);  
  151.     }  
  152.   
  153.     /** 
  154.      * 更新指定的实体 
  155.      *  
  156.      * @param <T> 
  157.      * @param pojo 
  158.      */  
  159.     public <T> void updateEntitie(T pojo) {  
  160.         commonDao.updateEntitie(pojo);  
  161.   
  162.     }  
  163.   
  164.     /** 
  165.      * 通过hql 查询语句查找对象 
  166.      *  
  167.      * @param <T> 
  168.      * @param query 
  169.      * @return 
  170.      */  
  171.     public <T> List<T> findByQueryString(String hql) {  
  172.         return commonDao.findByQueryString(hql);  
  173.     }  
  174.   
  175.     /** 
  176.      * 根据sql更新 
  177.      *  
  178.      * @param query 
  179.      * @return 
  180.      */  
  181.     public int updateBySqlString(String sql) {  
  182.         return commonDao.updateBySqlString(sql);  
  183.     }  
  184.   
  185.     /** 
  186.      * 根据sql查找List 
  187.      *  
  188.      * @param <T> 
  189.      * @param query 
  190.      * @return 
  191.      */  
  192.     public <T> List<T> findListbySql(String query) {  
  193.         return commonDao.findListbySql(query);  
  194.     }  
  195.   
  196.     /** 
  197.      * 通过属性称获取实体带排序 
  198.      *  
  199.      * @param <T> 
  200.      * @param clas 
  201.      * @return 
  202.      */  
  203.     public <T> List<T> findByPropertyisOrder(Class<T> entityClass, String propertyName, Object value, boolean isAsc) {  
  204.         return commonDao.findByPropertyisOrder(entityClass, propertyName, value, isAsc);  
  205.     }  
  206.   
  207.     /** 
  208.      *  
  209.      * cq方式分页 
  210.      *  
  211.      * @param cq 
  212.      * @param isOffset 
  213.      * @return 
  214.      */  
  215.     public PageList getPageList(final CriteriaQuery cq, final boolean isOffset) {  
  216.         return commonDao.getPageList(cq, isOffset);  
  217.     }  
  218.   
  219.     /** 
  220.      * 返回DataTableReturn模型 
  221.      *  
  222.      * @param cq 
  223.      * @param isOffset 
  224.      * @return 
  225.      */  
  226.     public DataTableReturn getDataTableReturn(final CriteriaQuery cq, final boolean isOffset) {  
  227.         return commonDao.getDataTableReturn(cq, isOffset);  
  228.     }  
  229.   
  230.     /** 
  231.      * 返回easyui datagrid模型 
  232.      *  
  233.      * @param cq 
  234.      * @param isOffset 
  235.      * @return 
  236.      */  
  237.     public DataGridReturn getDataGridReturn(final CriteriaQuery cq, final boolean isOffset) {  
  238.         return commonDao.getDataGridReturn(cq, isOffset);  
  239.     }  
  240.   
  241.     /** 
  242.      *  
  243.      * hqlQuery方式分页 
  244.      *  
  245.      * @param cq 
  246.      * @param isOffset 
  247.      * @return 
  248.      */  
  249.     public PageList getPageList(final HqlQuery hqlQuery, final boolean needParameter) {  
  250.         return commonDao.getPageList(hqlQuery, needParameter);  
  251.     }  
  252.   
  253.     /** 
  254.      *  
  255.      * sqlQuery方式分页 
  256.      *  
  257.      * @param cq 
  258.      * @param isOffset 
  259.      * @return 
  260.      */  
  261.     public PageList getPageListBySql(final HqlQuery hqlQuery, final boolean isToEntity) {  
  262.         return commonDao.getPageListBySql(hqlQuery, isToEntity);  
  263.     }  
  264.   
  265.     public Session getSession()  
  266.   
  267.     {  
  268.         return commonDao.getSession();  
  269.     }  
  270.   
  271.     public List findByExample(final String entityName, final Object exampleEntity) {  
  272.         return commonDao.findByExample(entityName, exampleEntity);  
  273.     }  
  274.   
  275.     /** 
  276.      * 通过cq获取全部实体 
  277.      *  
  278.      * @param <T> 
  279.      * @param cq 
  280.      * @return 
  281.      */  
  282.     public <T> List<T> getListByCriteriaQuery(final CriteriaQuery cq,Boolean ispage) {  
  283.         return commonDao.getListByCriteriaQuery(cq,ispage);  
  284.     }  
  285.   
  286.     /** 
  287.      * 文件上传 
  288.      *  
  289.      * @param request 
  290.      */  
  291.     public <T> T  uploadFile(UploadFile uploadFile) {  
  292.         return commonDao.uploadFile(uploadFile);  
  293.     }  
  294.   
  295.     public HttpServletResponse viewOrDownloadFile(UploadFile uploadFile)  
  296.   
  297.     {  
  298.         return commonDao.viewOrDownloadFile(uploadFile);  
  299.     }  
  300.   
  301.   
  302.     /** 
  303.      * 生成XML文件 
  304.      *  
  305.      * @param fileName 
  306.      *            XML全路径 
  307.      * @return 
  308.      */  
  309.     public HttpServletResponse createXml(ImportFile importFile) {  
  310.         return commonDao.createXml(importFile);  
  311.     }  
  312.   
  313.     /** 
  314.      * 解析XML文件 
  315.      *  
  316.      * @param fileName 
  317.      *            XML全路径 
  318.      */  
  319.     public void parserXml(String fileName) {  
  320.         commonDao.parserXml(fileName);  
  321.     }  
  322.   
  323.     public List<ComboTree> comTree(List<TSDepart> all, ComboTree comboTree) {  
  324.         return commonDao.comTree(all, comboTree);  
  325.     }  
  326.   
  327.     /** 
  328.      * 根据模型生成JSON 
  329.      *  
  330.      * @param all 
  331.      *            全部对象 
  332.      * @param in 
  333.      *            已拥有的对象 
  334.      * @param comboBox 
  335.      *            模型 
  336.      * @return 
  337.      */  
  338.     public List<ComboTree> ComboTree(List all,ComboTreeModel comboTreeModel,List in) {  
  339.         return commonDao.ComboTree(all,comboTreeModel,in);  
  340.     }  
  341.     /** 
  342.      * 构建树形数据表 
  343.      */  
  344.     public List<TreeGrid> treegrid(List all, TreeGridModel treeGridModel) {  
  345.         return commonDao.treegrid(all, treeGridModel);  
  346.     }  
  347.   
  348.     /** 
  349.      * 获取自动完成列表 
  350.      *  
  351.      * @param <T> 
  352.      * @return 
  353.      */  
  354.     public <T> List<T> getAutoList(Autocomplete autocomplete) {  
  355.         StringBuffer sb = new StringBuffer("");  
  356.         for(String searchField:autocomplete.getSearchField().split(",")){  
  357.             sb.append("  or "+searchField+" like '%"+autocomplete.getTrem() + "%' ");  
  358.         }  
  359.         String hql = "from " + autocomplete.getEntityName() + " where 1!=1 " + sb.toString();  
  360.         return commonDao.getSession().createQuery(hql).setFirstResult(autocomplete.getCurPage()-1).setMaxResults(autocomplete.getMaxRows()).list();  
  361.     }  
  362.       
  363.       
  364.     @Override  
  365.     public Integer executeSql(String sql, List<Object> param) {  
  366.         return commonDao.executeSql(sql, param);  
  367.     }  
  368.     @Override  
  369.     public Integer executeSql(String sql, Object... param) {  
  370.         return commonDao.executeSql(sql, param);  
  371.     }  
  372.       
  373.     @Override  
  374.     public Integer executeSql(String sql, Map<String, Object> param) {  
  375.         return commonDao.executeSql(sql, param);  
  376.     }  
  377.       
  378.     @Override  
  379.     public List<Map<String, Object>> findForJdbc(String sql, int page, int rows) {  
  380.         return commonDao.findForJdbc(sql, page,rows);  
  381.     }  
  382.     @Override  
  383.     public List<Map<String, Object>> findForJdbc(String sql, Object... objs) {  
  384.         return commonDao.findForJdbc(sql, objs);  
  385.     }  
  386.     @Override  
  387.     public List<Map<String, Object>> findForJdbcParam(String sql, int page,  
  388.             int rows, Object... objs) {  
  389.         return commonDao.findForJdbcParam(sql, page, rows, objs);  
  390.     }  
  391.     @Override  
  392.     public <T> List<T> findObjForJdbc(String sql, int page, int rows,  
  393.             Class<T> clazz) {  
  394.         return commonDao.findObjForJdbc(sql, page, rows, clazz);  
  395.     }  
  396.     @Override  
  397.     public Map<String, Object> findOneForJdbc(String sql, Object... objs) {  
  398.         return commonDao.findOneForJdbc(sql, objs);  
  399.     }  
  400.     @Override  
  401.     public Long getCountForJdbc(String sql) {  
  402.         return commonDao.getCountForJdbc(sql);  
  403.     }  
  404.     @Override  
  405.     public Long getCountForJdbcParam(String sql, Object[] objs) {  
  406.         return commonDao.getCountForJdbc(sql);  
  407.     }  
  408.     @Override  
  409.     public <T> void batchSave(List<T> entitys) {  
  410.         this.commonDao.batchSave(entitys);  
  411.     }  
  412.       
  413.       
  414.     /** 
  415.      * 通过hql 查询语句查找对象 
  416.      *  
  417.      * @param <T> 
  418.      * @param query 
  419.      * @return 
  420.      */  
  421.     public <T> List<T> findHql(String hql, Object... param) {  
  422.         return this.commonDao.findHql(hql, param);  
  423.     }  
  424.       
  425.   
  426. }  


该类是实现了CommonService接口,该接口中就声明了各种数据操作方法。但是在该实现类中用到了具体的Dao操作数据库。看一下这个Dao的具体实现是什么样的。

org.jeecgframework.core.common.dao.ICommonDao

这是一个 接口

commonDao是一个接口变量。

他的实现类:org.jeecgframework.core.common.dao.impl.CommonDao

代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @Repository  
  2. public class CommonDao extends GenericBaseCommonDao implements ICommonDao, IGenericBaseCommonDao {  


可以看到,CommonDao还继承 了GenericBaseCommonDao,操作数据的方法就在GenericBaseCommonDao中,

GenericBaseCommonDao类是一个抽象类

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @SuppressWarnings("hiding")  
  2. public abstract class GenericBaseCommonDao<T, PK extends Serializable> implements IGenericBaseCommonDao {  


 反正是关于数据库操作的都在GenericBaseCommonDao类里

0 0
原创粉丝点击