(九)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven构建ant-framework核心代码Base封装

本文详细介绍了在Ant-Framework中如何对SpringMVC、MyBatis进行基础封装,涉及BaseBean、BaseDao、BaseService的实现,以及CRUD操作和分页组件、数据源支持的简化处理。

今天重点讲解的是ant-framework核心代码Base封装过程。

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

1. BaseEntity基础封装,代码如下:

/**

 * Entity基础封装

 */ 

public abstract class BaseEntity<T> implements Serializable { 

   

    private static final long serialVersionUID = 1234567890987654321L; 

   

    /**

     * 实体编号(唯一标识)

     */ 

    protected String id; 

       

    /**

     * 当前实体分页对象

     */ 

    protected Page<T> page; 

       

   

    /**

     * 是否插入新纪录

     */ 

    protected boolean isNewRecord = false

   

    public BaseEntity() { 

           

    

       

    public BaseEntity(String id) { 

        this(); 

        this.id = id; 

    

   

    public String getId() { 

        return id; 

    

   

    public void setId(String id) { 

        this.id = id; 

    

   

    /**

     * 数据插入之前

     */ 

    public abstract void preInsert(); 

       

    /**

     * 更新数据之前

     */ 

    public abstract void preUpdate(); 

       

       /**

     * 是否是新记录(默认:false)

        */ 

    public boolean getIsNewRecord() { 

        return isNewRecord || StringUtils.isBlank(getId()); 

    

   

    /**

     * 是否是新记录(默认:false)

     */ 

    public void setIsNewRecord(boolean isNewRecord) { 

        this.isNewRecord = isNewRecord; 

    

   

    /**

     * 全局变量对象

     */ 

    @JsonIgnore 

    public Global getGlobal() { 

        return Global.getInstance(); 

    

       

    @Override 

    public boolean equals(Object obj) { 

        if (null == obj) { 

            return false

        

        if (this == obj) { 

            return true

        

        if (!getClass().equals(obj.getClass())) { 

            return false

        

        BaseEntity<?> that = (BaseEntity<?>) obj; 

        return null == this.getId() ? false this.getId().equals(that.getId()); 

    }    

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

public interface BaseDao { 

        3. CrudDao的基础封装 

/**

 * DAO基础封装

 */ 

public interface CrudDao<T> extends BaseDao { 

   

    /**

     * 获取单条数据

     * @param id

     * @return

     */ 

    public T get(String id); 

       

    /**

     * 获取单条数据

     * @param entity

     * @return

     */ 

    public T get(T entity); 

       

    /**

     * 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<T>());

     * @param entity

     * @return

     */ 

    public List<T> findList(T entity); 

       

    /**

     * 查询所有数据列表

     * @param entity

     * @return

     */ 

    public List<T> findAllList(T entity); 

       

    /**

     * 查询所有数据列表

     * @see public List<T> findAllList(T entity)

     * @return

     */ 

    @Deprecated 

    public List<T> findAllList(); 

       

    /**

     * 插入数据

     * @param entity

     * @return

     */ 

    public int insert(T entity); 

       

    /**

     * 更新数据

     * @param entity

     * @return

     */ 

    public int update(T entity); 

       

    /**

     * 删除数据

     * @param id

     * @see public int delete(T entity)

     * @return

     */ 

    @Deprecated 

    public int delete(String id); 

       

    /**

     * 删除数据

     * @param entity

     * @return

     */ 

    public int delete(T entity); 

       

        4. BaseService的基础封装(里面封装了基础的CRUD操作,包括基础get,find,insert,update等) 

/**

 * BaseService基础封装

 */ 

@Transactional(readOnly = true

public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService { 

       

    /**

     * 持久层dao

     */ 

    @Autowired 

    protected D dao; 

       

    /**

     * 获取单条数据

     * @param id

     * @return

     */ 

    public T get(String id) { 

        return dao.get(id); 

    

       

    /**

     * 获取单条数据

     * @param entity

     * @return

     */ 

    public T get(T entity) { 

        return dao.get(entity); 

    

       

    /**

     * 查询列表数据

     * @param entity

     * @return

     */ 

    public List<T> findList(T entity) { 

        return dao.findList(entity); 

    

       

    /**

     * 查询分页数据

     * @param page 分页对象

     * @param entity

     * @return

     */ 

    public Page<T> findPage(Page<T> page, T entity) { 

        entity.setPage(page); 

        page.setList(dao.findList(entity)); 

        return page; 

    

   

    /**

     * 保存数据(插入或更新)

     * @param entity

     */ 

    @Transactional(readOnly = false

    public void save(T entity) { 

        if (entity.getIsNewRecord()){ 

            entity.preInsert(); 

            dao.insert(entity); 

        }else

            entity.preUpdate(); 

            dao.update(entity); 

        

    

       

    /**

     * 删除数据

     * @param entity

     */ 

    @Transactional(readOnly = false

    public void delete(T entity) { 

        dao.delete(entity); 

    

5.架构代码如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ok060

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值