Strategies for frequent modification of MyBatis db

Source: Internet
Author: User

MyBatis Current Most popular ORM framework, what is the most annoying thing in use, is undoubtedly the need to change or business closed-loop thinking does not study the resulting database field additions or deletions, adding or removing a field needs to do the corresponding new or deleted field operations in the XML in several places, slightly less cautious, Waiting will be a big wave of exceptions, saying that the solution stems from the problem. The next step is to design a generic service to address the large amount of work that DB frequently changes.

Core ideas:

0. Integrated use of universal Mapper in MyBatis 3.x

1. Up-to-the-General Service design (generic thinking && Injection Universal mapper)

2. Mapper Mandatory specification (reduced error parameters && accelerated development)

Mapper Mandatory Specification:

0. Mapper without cascading queries (not associated queries) does not write Resultmap, and the following code is determined not to

<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 single-table operation related code strongly not, such as the most annoying update, insert a block of code containing a lot of code.

2. Only the bottom of these field sets are left in XML

<sql id="base_column_list">        ID, user_name, sex, age        , address,        Create_time, Update_time, status, remark     </sql>

3. SQL operations in XML are associated table operations

Then the system is talking about the General Service design ideas, as well as the design steps:

0. The control layer code changes little, what happened before now:

@RequestMapping ("/test")    @ResponseBody    public  Object Test (Country country) {        List<Country> all = myservice.selectcountrybyexalple (country);        Logger.info ("------------selectall-----------""--------- ------------------");         return All ;    }

1. The business layer inherits the common service to make it easy to invoke a method that can apply the single-table operation function of any table in any entity

  

Public interface MyService extends basetestservice<country>

2. Generic Service-Generic application

public interface basetestservice<t>

3. Universal service implementation, virtually all single table operations implemented

  

 Public classBasetestserviceimpl<t>{@AutowiredprotectedMapper<t>mapper;  PublicMapper<t>Getmapper () {returnmapper; }         Public intDelete (T entity) {returnMapper.delete (entity); }     Public intDelectbyexample (Object example) {returnMapper.deletebyexample (example); }     Public intDeletebyprimarykey (Object key) {returnMapper.deletebyprimarykey (key); }         Public intInsert (T record) {returnMapper.insert (record); }     Public intinsertselective (T record) {returnmapper.insertselective (record); }         PublicList<t>Select(T record) {returnMapper.Select(record); }     PublicList<t>SelectAll () {returnMapper.selectall (); }     PublicList<t>Selectbyexalple (Object example) {returnMapper.selectbyexample (example); }     PublicList<t>selectbyexampleandrowbounds (Object example,rowbounds rowbounds) {returnMapper.selectbyexampleandrowbounds (example, rowbounds); }     PublicT Selectbyprimarykey (Object key) {returnMapper.selectbyprimarykey (key); }     PublicList<t>selectbyrowbounds (T record,rowbounds rowbounds) {returnmapper.selectbyrowbounds (record, rowbounds); }     Public intSelectCount (T record) {returnMapper.selectcount (record); }     Public intSelectcountbyexample (Object example) {returnMapper.selectcountbyexample (example); }     PublicT SelectOne (t record) {returnMapper.selectone (record); }             Public intUpdatebyexample (T record,object example) {returnMapper.updatebyexample (record, example); }     Public intupdatebyexampleselective (T record,object example) {returnmapper.updatebyexampleselective (record, example); }     Public intUpdatebyprimarykey (T record) {returnMapper.updatebyprimarykey (record); }     Public intupdatebyprimarykeyselective (T record) {returnmapper.updatebyprimarykeyselective (record); }    }

4. Business service implementations, in general, build operational database conditions through Java code, and then call the Generic service method

  

@Override PublicList<country>selectcountrybyexalple (Country country) {Example Example=NewExample (country.class); Example.criteria Cirteria=Example.createcriteria (); if(Stringutils.isnotblank (Country.getcountryname ())) {Cirteria.andlike ("CountryName","%"+ country.getcountryname () +"%"); }        returnSuper.selectbyexalple (example); }

Where Cirteria contains all the methods we can almost try

Third-party participation references:

<!--page plug-ins-        <dependency>            <groupId>com.github.pagehelper</groupId>            < artifactid>pagehelper</artifactid>            <version>${pagehelper.version}</version>        </ Dependency>        <!--general mapper-->        <dependency>            <groupId>tk.mybatis</groupId>            <artifactId>mapper</artifactId>            <version>${mapper.version}</version>        </dependency>

Parcel, the General Service design is complete, simple and convenient, practical and low complexity, suppose a project 30 tables, each table field is moderate, each initial mapper about 150 lines of code, using the above XML specification, each mapper about 7 lines of code around, The total code is reduced from 4500 lines to 210 lines, which is only one advantage, the key point is that the code is finally easy to maintain, easy to iterate, not afraid of modification.

MyBatis DB's frequent modification response policy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.