mybatis 之 DB的頻繁修改應對策略

來源:互聯網
上載者:User

標籤:單表   let   art   pre   update   外掛程式   new   系統   var   

  mybatis 當前最流行的ORM架構,使用中最煩的事情是什麼,無疑是需求的更改或者業務閉環思維不研究導致的資料庫欄位的增加或者刪除,增加或刪除一個欄位需要在xml多個地方做相應的新增或刪除欄位操作,稍不謹慎,等待的將是一大波Exceptions,俗話說解決辦法來源於問題的產生。接下來設計通用service 來解決DB頻繁改動,帶來的大量工作量。

  核心思路:

  0. mybatis 3.x 中通用mapper的整合使用

  1. 向上通用Service的設計(泛型思維 && 注入通用mapper)

  2. mapper 強制規範 (減少錯誤參數 && 加速開發)

  mapper 強制規範:

  0. 無級聯查詢(不是關聯查詢)的mapper 不寫resultMap,如下這些代碼堅決不要

 <resultMap id="BaseResultMap" type="com.javazx.batch.po.UserTo">        <id column="id" property="id" jdbcType="BIGINT"/>        <result column="user_name" property="userName" jdbcType="VARCHAR"/>        <result column="sex" property="sex" jdbcType="INTEGER"/>        <result column="age" property="age" jdbcType="INTEGER"/>        <result column="address" property="address" jdbcType="VARCHAR"/>        <result column="create_time" property="createTime" jdbcType="DATE"/>        <result column="update_time" property="updateTime" jdbcType="DATE"/>        <result column="status" property="status" jdbcType="INTEGER"/>        <result column="remark" property="remark" jdbcType="VARCHAR"/>    </resultMap>

  1. xml 中單表操作相關代碼堅決不要,比如最煩人的update ,insert 包含大量代碼的代碼塊。

  2. xml 中僅剩下了下邊這些欄位集

<sql id="Base_Column_List">        id, user_name, sex,        age, address,        create_time, update_time, status, remark    </sql>

  3. xml中sql操作都是關聯表操作

  接下來系統說說通用Service的設計思路,以及設計步驟:

  0.控制層層代碼變化不大,以前怎麼樣現在還怎麼樣:

@RequestMapping("/test")    @ResponseBody    public Object test(Country country){        List<Country> all = myService.selectCountryByExalple(country);        logger.info("------------selectAll-----------" + all + "---------------------------");        return all;    }

  1.業務層繼承通用Service便於調用能夠適用任何實體任何錶的單表操作功能的方法

  

public interface MyService extends BaseTestService<Country>

   2. 通用Service -- 泛型的應用

public interface BaseTestService<T>

   3. 通用Service實現,幾乎實現了所有單表操作

  

public class BaseTestServiceImpl<T>{    @Autowired    protected Mapper<T> mapper;    public Mapper<T> getMapper() {        return mapper;    }        public int delete(T entity) {        return mapper.delete(entity);    }    public int delectByExample(Object example){        return mapper.deleteByExample(example);    }    public int deleteByPrimaryKey(Object key){        return mapper.deleteByPrimaryKey(key);    }        public int insert(T record){        return mapper.insert(record);    }    public int insertSelective(T record){        return mapper.insertSelective(record);    }        public List<T> select(T record){        return mapper.select(record);    }    public List<T> selectAll(){        return mapper.selectAll();    }    public List<T> selectByExalple(Object example){        return mapper.selectByExample(example);    }    public List<T> selectByExampleAndRowBounds(Object example,RowBounds rowBounds){        return mapper.selectByExampleAndRowBounds(example, rowBounds);    }    public T selectByPrimaryKey(Object key){        return mapper.selectByPrimaryKey(key);    }    public List<T> selectByRowBounds(T record,RowBounds rowBounds){        return mapper.selectByRowBounds(record, rowBounds);    }    public int selectCount(T record){        return mapper.selectCount(record);    }    public int selectCountByExample(Object example){        return mapper.selectCountByExample(example);    }    public T selectOne(T record){        return mapper.selectOne(record);    }            public int updateByExample(T record,Object example){        return mapper.updateByExample(record, example);    }    public int updateByExampleSelective(T record,Object example){        return mapper.updateByExampleSelective(record, example);    }    public int updateByPrimaryKey(T record){        return mapper.updateByPrimaryKey(record);    }    public int updateByprimaryKeySelective(T record){        return mapper.updateByPrimaryKeySelective(record);    }    }

  4. 業務Service實現,總體上是通過java代碼構建操作資料庫條件,然後調用通用Service方法

  

@Override    public List<Country> selectCountryByExalple(Country country) {        Example example = new Example(Country.class);        Example.Criteria cirteria = example.createCriteria();        if(StringUtils.isNotBlank(country.getCountryname())){            cirteria.andLike("countryname", "%" + country.getCountryname() + "%");        }        return super.selectByExalple(example);    }

其中cirteria中包含了我們幾乎能夠試用到的所有方法

  第三方參加引用:

<!--分頁外掛程式-->        <dependency>            <groupId>com.github.pagehelper</groupId>            <artifactId>pagehelper</artifactId>            <version>${pagehelper.version}</version>        </dependency>        <!--通用Mapper-->        <dependency>            <groupId>tk.mybatis</groupId>            <artifactId>mapper</artifactId>            <version>${mapper.version}</version>        </dependency>

 

  宗上,通用Service設計完成,簡單且方便,實用且低複雜度,假設一個項目30個表,每個表欄位適中,每個初始mapper中大約150行代碼,使用上述xml規範,每個mapper大約7行代碼左右,總代碼由4500行減少到210行,這隻是一個優點,關鍵點還是代碼最後易維護,易迭代,不懼修改。

 

mybatis 之 DB的頻繁修改應對策略

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.