The actual development dao, entity how to automatically generate code? A tool for you

01

Concerned about "a small ape stresses" friends, all know that the previous article has been advocating refused CRUD, and that in the end what is CRUD? Today we will talk little sister thing Java ape the old man with the database interaction.

 

Product Nanxiaowang sonorous said: small ape students, we want to push a recent explosion models of products, you first achieve basic user login functionality.

 

Hell? Small ape heart Whine: explosion of products, but also basic login, is not that achieve the user's CRUD (CRUD) Why?

 

Xiao Wang word, exhausted armed forces. Wrangling a morning session was over, the little ape he called a takeaway, after fill the emptiness of the stomach, small apes in the habit of mind, began to build a mind map from the 0-1 explosion models of the product.

 

640?wx_fmt=png

 

Small ape idea is this: first analysis of the product Wang needs to see whether there will be demand for features missing the point; then the database table structure design; then is like a small ape-source operating code.

 

Way small ape code code, it is a very special name seriously, so we attracted onlookers.

 

Before just hearsay, today saw indeed. Only a small ape file first in sqlmaps suffer a lot of effort, a letter, a punctuation to write the mapping file; then another entity to define a class, a field of a database table are mapped; then use the same serious dao way to write code; and finally come back to confirm the database tables and fields correspondence whether the entity class.

 

I looked at the little ape seriously indeed commendable. But after watching a small ape code of code, but I see kidney pain, this is not what is repetitive work, it really is no good way to small ape can recommend it?

 

02

 

Ape with a small hand-made tools.

 

Small ape, you find you compare favored directory, create a folder, you name the mybatis-Generator , in fact, does not matter from what name ah, this is your tool kit directory, the following things are put in this directory.

 

Small apes, see no connection driver package database on your computer, let's not have a download, to give you a download link, give you a minute to download jar package it will be used later, I went to the root of smoke smoke.

https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar

Well, you download a small ape did not, I smoked a cigarette are friends.

 

Now that you download finished, it would then see if there is no package on the computer the mybatis-Generator-Core-1.3.2.jar , whether it is doing do not use, do not it is in accordance with the following address to download it anyway to use later to.

https://repo1.maven.org/maven2/org/mybatis/generator/mybatis-generator-core/1.3.2/mybatis-generator-core-1.3.2.jar

  

Small ape, to which our work is almost ready, but you also need to create a configuration file generatorConfig.xml, do not worry about the contents of the file directly to get past the following code to change it to change it on the line. Note must take inside the directory involved, had been turned into a directory on your computer, you first modify a while, I went to drink a cup of coffee. As follows (Note that we must modify directory)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <! - Specifies the data link address driver jar ->
  <classPathEntry
    location="/app/mybatis-generator/mysql-connector-java-5.1.38.jar" />

  <! - Specifies the data link address driver jar ->
  <context id="db2Tables" targetRuntime="MyBatis3">
    <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
    <commentGenerator>
      <property name="javaFileEncoding" value="UTF-8"/>
      <! - whether to generate comments on behalf of the timestamp ->
      <property name="suppressDate" value="true" />
      <! - whether uncommented ->
      <property name="suppressAllComments" value="false" />
    </commentGenerator>

    <- database connection information:! Class driver, connection address, user name, password ->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
      connectionURL="jdbc:mysql://localhost:3306/baokuan"
      userId="root" password="root" />

    <- targetProject:! Build directory entity classes stored ->
    <javaModelGenerator targetPackage="com.cool.baokuan.modules.model"
      targetProject="/app/mybatis-generator/src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <- targetProject:! Mapper mapping file generated location ->
    <sqlMapGenerator targetPackage="com.cool.baokuan.modules.dao"
      targetProject="/app/mybatis-generator/src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <- targetPackage:! Mapper interface generation position ->
    <javaClientGenerator targetPackage="com.cool.baokuan.modules.dao"
      targetProject="/app/mybatis-generator/src" type="XMLMAPPER">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <! - Specifies the database table ->
    <table tableName="T_USER" domainObjectName="User"
      enableCountByExample="false" enableUpdateByExample="false"
      enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false"/>
      
  </context>
</generatorConfiguration>

  

Small ape, completely changed the directory, right?

 

Since the change over, then the difference between the last step, you create a script execution, your system is Windows, so the script name is the run.bat , I'm a Mac, so my script is run.sh . The following content is created and then copy into it.

Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

  

Well, the little ape, Congratulations, you have completed hand tool kit to build your own to see results, not satisfied?

 

640?wx_fmt=png

 

Small ape, is a mule is a horse, we should pull out yo. By the time you try a small chopper, a singing friends. A key to run run.bat or run.sh try.

 

640?wx_fmt=jpeg

 

Wow, see the above effect, a small ape really shocked you, because with this tool in the future no longer have to repeat the mapping file write, write entity class, write a dao, you only configure the configuration database table name on the line, simply handsome friends.

 

Small ape, to refrain from excessive joy. When you use this tool in the future, we must pay attention to modify generatorConfig.xml following local file, or will the unitary moths.

1. Modify the specified data driver connected to the address jar;	
2. Modify the database connection url, user name, password;	
3. Modify targetProject directory to the directory of your computer;	
4. Modify database tables.

  

Well, the little ape, ape into a master-door practice that individual, after your own good fortune to see it (?).

 

03

 

Sleight, a small ape busy would like to recruit.

 

Just a few days on the product line, users increased sharply, really explosive product ah, user queries more slowly.

 

Product cattle breaking Xiao Wang Hong Hong, said: small ape, recently there are many users experience slower and slower reaction system, the code is not there Bug ah?

 

Hell, the code has Bug, how the code may have Bug! In fact, a small ape heart like a mirror, you know better than anyone, is a user table is too big, too slow so that the query, which we supposed to do that?

 

Only a small ape, God took out a pen, wrote numerous books on the matter, "sub-library sub-table", began the process of checking a series of sub-library sub-table burning brain, have to admire the mathematical skills of a small ape, while small apes do not pay attention, I remember a number of petty theft, and quickly share with you.

 

The rule of sub-library sub-table

 

If the master node M databases, data tables split into a total of N pieces of sub-tables, fields may split the table is converted into a numerical value X, the number of database instance X% M;  number data table is X% N.

 

For example: When the master node number M = 6, the total number of split N = 1200 tables, the table number of the single sub-compartment 200, the number of database instance X% 6; No. data table of X% 1200.

 

Then the number is 0 in the database, there is a number 0,6,12,18 ... data table; No. 1 in the database, there is a serial data table 1,7,13,19 ..., and so on .

 

Split the table mechanism numerical field

 

Take split the table by field, CRC32 take them, after the absolute value, i.e. the above-described X. "sub-sub-table repository formula" in

 

I did not expect a small ape, also wrote specifically for this segment pseudo-code.

java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();	
// split fields crc32	
crc32.update(splitField.getBytes(DEFAULT_ENCODING));	
long x = java.lang.Math.abs(crc32.getValue());	

	
// database subscript	
long n = x % dbNum;	
String dbPos = String.format("%02d", n);	
System.out.println ( "subscript database:" + dbPos);	

	
// table subscript	
long m = x % tableNum;	
String tablePos = String.format("%03d", m);	
System.out.println ( "table subscript:" + tablePos);

  

Moreover, when a small ape final manuscript inscribed, also wrote three times: Sharding-Jdbc, Sharding-Jdbc, Sharding-Jdbc. Especially written three times, so Tell me what you want to look at this stuff on their own brain make you, or there will never keep up with the rhythm of a small ape friends.

 

04

Indeed allow everyone to crowd together small ape, which I can only see a small ape ORZ worship. Quote a classic: your uncle or your uncle, but the little ape is no longer before repeating CRUD small ape friends. Who do three days when the wait and see, so do not underestimate every small ape around Yo.

 

 

Guess you like

Origin www.cnblogs.com/socoool/p/12629793.html