Java+mybatis-Table Routing

Source: Internet
Author: User
Tags null null stub

Concept:

The table is a large table according to a certain rule decomposition into a number of independent storage space of the entity table, we can call the child table, each table corresponds to three files, myd data files. Myi index file,. frm table structure file. These child tables can be distributed on the same disk or on different machines. When the app reads and writes, it gets the corresponding child table name according to the predefined rules, and then it operates.

1. Define routing interface

Public interface Routetableable {
	/**
	 * Total number of tables, default ten * @return * * public
	int tablesize ();
	/**
	 * Use the value% tablesize 
	 * @return
	/public int routevalue ();
	/** 
	 * routing table suffix
	 * @return
	 /public
	String Tableext ();
	/**
	 * Connection Table string
	 * @return
	 /Public
	string concat ();
}

2, the implementation of the interface (default 10 child table, with ' _ ' as a connector, using RouteValue and the total number of children to take the model as the table number)

Public abstract class Abstractroutetable implements Routetableable {

	@Override public
	int tablesize () {
		// TODO auto-generated Method Stub return
	@Transient
	@Override public
	String concat () {
		//TODO auto-generated a stub return
		"_";
	}
	
	@Transient
	@Override public
	String Tableext () {
		//TODO auto-generated a stub return
		concat () . Concat (String.valueof (RouteValue ()% tablesize ()));
	}

3, inherits the parent class, overrides the child table Total method, using the VIN field, the 8-bit hashcode as RouteValue

public class Carhealthbasic extends abstractroutetable implements Serializable {private static final long Serialversio

	nuID = 2291302184719452210L;
	/** * ID */@Id private Long ID;
    /** * Upload the Enterprise ID * * Private Long CompanyID;

    /** * License plate number/private String vehicleplatenumber;

    /** * Maintenance Enterprise Name * * Private String CompanyName;

    /** * Maintenance Enterprise Code */Private String Companycode;

    /** * Vehicle Real code/private String vin;

    /** * Repair Date/private date repairdate;

    /** * Repair Mileage * * Private String repairmileage;
    /** * ID * @return ID ID/public Long getId () {return id;
    }/** * ID * @param ID ID */public void setId (Long id) {this.id = ID; /** * License plate number * @return vehicleplatenumber/public String Getvehicleplatenumber () {Retu
    RN Vehicleplatenumber; /** * License plate number * @param vehicleplatenumber */public void Setvehicleplatenumber (String vehicleplatenumber) {THIS.VEHICL Eplatenumber = Vehicleplatenumber = = null?
    Null:vehicleplatenumber.trim (); /** * Maintenance Enterprise Name * @return CompanyName/Public String Getcompanyname () {return Companyna
    Me /** * Maintenance Enterprise Name * @param companyname/public void Setcompanyname (String CompanyName) {T His.companyname = CompanyName = = null?
    Null:companyname.trim (); /** * Maintenance Enterprise Code * @return Companycode */public String Getcompanycode () {return Companyco
    De /** * Maintenance Enterprise Code * @param companycode */public void Setcompanycode (String companycode) {T His.companycode = Companycode = = null?
    Null:companycode.trim ();
    /** * Vehicle Real code * @return VIN */public String Getvin () {return vin; 
/** * Vehicle Real code * @param vin     */public void Setvin (String vin) {This.vin = vin = null Null:vin.trim ();
    /** * Repair Date * @return Repairdate */Public Date getrepairdate () {return repairdate; /** * Repair Date * @param repairdate */public void setrepairdate (date repairdate) {THIS.R
    Epairdate = repairdate; /** * Repair Mileage * @return repairmileage */public String getrepairmileage () {return repairm
    Ileage;
      	 /** * Repair Mileage * @param repairmileage */public void Setrepairmileage (String repairmileage) { This.repairmileage = Repairmileage = = null?
   Null:repairmileage.trim ();
   Public Long Getcompanyid () {return companyid;
   } public void Setcompanyid (Long companyid) {This.companyid = CompanyID; @Override public int RouteValue () {//TODO auto-generated a stub return vin.substring (Vin.length ()-8, VI
   N.length ()). Hashcode ();
  }  
    @Override public int tablesize () {//TODO auto-generated method stub return 20; }    
	
}

4, MyBatis ( additions and deletions need to be spliced )

Public String Insertsql (Object t) {
		StringBuilder buffer = new StringBuilder ();
		Buffer.append ("INSERT into"). Append (Gettablefromclass (T.getclass ()). Append (Getroutetablefromobject (t)). Append ( " ");
		try {
			list<com.ctjy.support.mybaits.provider.entry>	List = getkeyvalueentry (t);
			Buffer.append ("(");
			for (Com.ctjy.support.mybaits.provider.Entry entry:list) {
				buffer.append (Entry.getcolumn ()). Append (",");
			String str = buffer.substring (0, Buffer.length ()-1);
			Buffer.setlength (0);
			Buffer.append (str). Append (")");
			Buffer.append ("VALUES (");
			
			for (Com.ctjy.support.mybaits.provider.Entry entry:list) {
				buffer.append ("?"). Append (",");
			}
			
			str = buffer.substring (0, Buffer.length ()-1);
			Buffer.setlength (0);
			Buffer.append (str). Append (")");
		catch (Introspectionexception e) {
			e.printstacktrace ();
		}
	
		return buffer.tostring ();
	}

5, Getroutetablefromobject method code is as follows: To determine whether the Pojo is a subclass of routetable, if it is, return the table name suffix

Public String getroutetablefromobject (Object obj) {
		if (obj = = null) {return
			"";
		}
		
		String ext = null;
		if (RouteTableAble.class.isAssignableFrom (Obj.getclass ())) {
			routetableable route = (routetableable) obj;
			ext = Route.tableext ();
		}
		
		return ext = null? "": ext;
	}

6, on the processing of mapper files, through the Tablesize splicing SQL statements in the table name (query parameters add a tablesize, the value through the table rules to calculate)

<!--check and repair records-->
	<select id= "Findrepairlog" resulttype= " Com.ctjy.wxmis.carhealth.dto.CarHealthBasicSuperviseDto ">
	select A.id,a.vehicleplatenumber,
	A.companyname,a.companycode,a.vin,a.repairdate,
	a.repairmileage,a.settledate,a.faultdescription, A.costlistcode
	from 
	<choose>
		<when test= "healthparam.tablesize!= null" >
			car_health_ Basic${healthparam.tablesize} A
		</when>
		<otherwise>
				car_health_basic a
		</ otherwise>
	</choose>
	<include refid= "cond"/> Limit #{index},#{pagesize
	}
	</ Select>




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.