鱼C论坛

 找回密码
 立即注册
查看: 1580|回复: 1

[技术交流] 构建dubbo分布式平台-maven构建ant-framework核心代码Base封装

[复制链接]
发表于 2018-8-28 15:27:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
因为涉及到springmvc、mybatis的集成,为了使项目编码更简洁易用,这边将基础的BASE进行封装,其中包括:BaseBean、BaseDao、BaseService、CRUD的基础封装、分页组件的封装、mybatis的mapper的基础封装,各种数据源支持的封装等。


1. BaseEntity基础封装,代码如下:
  1. /**
  2.  * Entity基础封装
  3.  */
  4. public abstract class BaseEntity<T> implements Serializable {
  5. private static final long serialVersionUID = 1234567890987654321L;
  6. /**
  7. * 实体编号(唯一标识)
  8. */
  9. protected String id;

  10. /**
  11. * 当前实体分页对象
  12. */
  13. protected Page<T> page;
  14. /**
  15. * 是否插入新纪录
  16. */
  17. protected boolean isNewRecord = false;
  18. public BaseEntity() {
  19. }
  20. public BaseEntity(String id) {
  21. this();
  22. this.id = id;
  23. }
  24. public String getId() {
  25. return id;
  26. }
  27. public void setId(String id) {
  28. this.id = id;
  29. }
  30. /**
  31. * 数据插入之前
  32. */
  33. public abstract void preInsert();

  34. /**
  35. * 更新数据之前
  36. */
  37. public abstract void preUpdate();

  38. &#160; &#160; &#160; &#160;/**
  39. * 是否是新记录(默认:false)
  40. &#160; &#160; &#160; &#160; */
  41. public boolean getIsNewRecord() {
  42. &#160; &#160; &#160; &#160; return isNewRecord || StringUtils.isBlank(getId());
  43. &#160; &#160; }
  44. /**
  45. * 是否是新记录(默认:false)
  46. */
  47. public void setIsNewRecord(boolean isNewRecord) {
  48. this.isNewRecord = isNewRecord;
  49. }
  50. /**
  51. * 全局变量对象
  52. */
  53. @JsonIgnore
  54. public Global getGlobal() {
  55. return Global.getInstance();
  56. }
  57. @Override
  58. public boolean equals(Object obj) {
  59. if (null == obj) {
  60. &#160; &#160;return false;
  61. }
  62. if (this == obj) {
  63. &#160; &#160;return true;
  64. }
  65. if (!getClass().equals(obj.getClass())) {
  66. &#160; &#160;return false;
  67. }
  68. BaseEntity<?> that = (BaseEntity<?>) obj;
  69. return null == this.getId() ? false : this.getId().equals(that.getId());
  70. }&#160;
  71. }
复制代码


2. BaseDao的基础封装(这个很简单,因为使用的是mybatis集成方案,只需要保留接口即可),代码如下:

  1. public interface BaseDao {
  2. }
复制代码

3. CrudDao的基础封装

  1. /**
  2. &#160;* DAO基础封装
  3. &#160;*/
  4. public interface CrudDao<T> extends BaseDao {


  5. /**
  6. * 获取单条数据
  7. * @param id
  8. * @return
  9. */
  10. public T get(String id);

  11. /**
  12. * 获取单条数据
  13. * @param entity
  14. * @return
  15. */
  16. public T get(T entity);

  17. /**
  18. * 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<T>());
  19. * @param entity
  20. * @return
  21. */
  22. public List<T> findList(T entity);

  23. /**
  24. * 查询所有数据列表
  25. * @param entity
  26. * @return
  27. */
  28. public List<T> findAllList(T entity);

  29. /**
  30. * 查询所有数据列表
  31. * @see public List<T> findAllList(T entity)
  32. * @return
  33. */
  34. @Deprecated
  35. public List<T> findAllList();

  36. /**
  37. * 插入数据
  38. * @param entity
  39. * @return
  40. */
  41. public int insert(T entity);

  42. /**
  43. * 更新数据
  44. * @param entity
  45. * @return
  46. */
  47. public int update(T entity);

  48. /**
  49. * 删除数据
  50. * @param id
  51. * @see public int delete(T entity)
  52. * @return
  53. */
  54. @Deprecated
  55. public int delete(String id);

  56. /**
  57. * 删除数据
  58. * @param entity
  59. * @return
  60. */
  61. public int delete(T entity);

  62. }
复制代码


4. BaseService的基础封装(里面封装了基础的CRUD操作,包括基础get,find,insert,update等)
  1. /**
  2. &#160;* BaseService基础封装
  3. &#160;*/
  4. @Transactional(readOnly = true)
  5. public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {

  6. /**
  7. * 持久层dao
  8. */
  9. @Autowired
  10. protected D dao;

  11. /**
  12. * 获取单条数据
  13. * @param id
  14. * @return
  15. */
  16. public T get(String id) {
  17. return dao.get(id);
  18. }

  19. /**
  20. * 获取单条数据
  21. * @param entity
  22. * @return
  23. */
  24. public T get(T entity) {
  25. return dao.get(entity);
  26. }

  27. /**
  28. * 查询列表数据
  29. * @param entity
  30. * @return
  31. */
  32. public List<T> findList(T entity) {
  33. return dao.findList(entity);
  34. }

  35. /**
  36. * 查询分页数据
  37. * @param page 分页对象
  38. * @param entity
  39. * @return
  40. */
  41. public Page<T> findPage(Page<T> page, T entity) {
  42. entity.setPage(page);
  43. page.setList(dao.findList(entity));
  44. return page;
  45. }


  46. /**
  47. * 保存数据(插入或更新)
  48. * @param entity
  49. */
  50. @Transactional(readOnly = false)
  51. public void save(T entity) {
  52. if (entity.getIsNewRecord()){
  53. entity.preInsert();
  54. dao.insert(entity);
  55. }else{
  56. entity.preUpdate();
  57. dao.update(entity);
  58. }
  59. }

  60. /**
  61. * 删除数据
  62. * @param entity
  63. */
  64. @Transactional(readOnly = false)
  65. public void delete(T entity) {
  66. dao.delete(entity);
  67. }
  68. }
复制代码


文章内容不写太多,希望大家能够掌握每一个知识点,基础的CRUD,BASE的封装差不多都在这里,后面会继续补充,具体的业务和实现后面会讲解到。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-28 15:28:01 | 显示全部楼层
希望可以帮助到你们哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 00:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表