Http://git.oschina.net/alexgaoyh/MutiModule-parent
The code changed a lot, using the Mybatis-generator-maven-plugin plug-in, the domain part of the previous multi-module project was deleted, this part of the code to integrate into the Persise section, The reason is that when you use the Mybatis-generator-maven-plugin plug-in, you can automatically generate entity classes to reduce the effort associated with code movement.
The previous time using the MAVEN multi-module project to integrate the Spring MyBatis section, recently encountered a series of problems when dealing with the generic crud approach, with a close-up article documenting:
There are many solutions to the common CRUD methods on the network, the addition of the DAO Interface section, the dynamic splicing of SQL ..., this article does not do too much introduction to these methods.
Before using a section of hibernate (a strong type ORM), and recently turning into MyBatis, I found that mybatis can be understood as a semi-ORM, writing SQL in XML to meet business needs, and if you want to use a generic crud approach, many solution injections are too strong. So looking for a long time, feel that using mybatis-generator-maven-plugin can be a good alternative to this solution:
1: Do not change the characteristics of the MyBatis (interface, SQL write to the internal XML), the novice to get the project, can be quickly started;
2: Simple single-table crud can also be achieved;
3: Reduce the amount of code written, in a timely manner to write a common method (generics), but also need to write their own relevant code, inherit the relevant interface to achieve, using generator words, can be a large number of reduced code writing;
So, finally, Mybatis-generator-maven-plugin was chosen to implement a simple single-table crud approach.
Implementation, the main code is divided into the following two parts:
<plugin><groupId>org.mybatis.generator</groupId><artifactId> Mybatis-generator-maven-plugin</artifactid><version>1.3.2</version><configuration> <verbose>true</verbose> <overwrite>true</overwrite></configuration>< /plugin>
Create the Generatorconfig.xml file under/src/main/resource:
Note Modify the configuration of some database methods, so that after generating the relevant table structure in the database, through the following configuration, to map the relevant class file/xml file;
<?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><classpathentry location= "C:\Users\lenovo\.m2\repository\mysql\mysql-connector-java\5.1.21\mysql-connector-java-5.1.21.jar"/> < Context id= "Context1" targetruntime= "MyBatis3" > <jdbcconnection driverclass= "Com.mysql.jdbc.Driver" Connectionurl= "Jdbc:mysql://127.0.0.1:3306/mybatistest?useunicode=true&characterencoding=utf-8" UserI d= "root" password= "Alexgaoyh"/> <javamodelgenerator targetpackage= "com.alexgaoyh.MutiModule.pers Ist.demo "targetproject=" F:\eclipseWorkspace\MutiModule-parent\MutiModule-persist\src\main\java "/> < Sqlmapgenerator targetpackage= "Com.alexgaoyh.MutiModule.persist.demo" targetproject= "F:\eclipseWorkspace\ Mutimodule-parent\mutimodUle-persist\src\main\resources "/> <javaclientgenerator targetpackage=" Com.alexgaoyh.MutiModule.persi St.demo "targetproject=" F:\eclipseWorkspace\MutiModule-parent\MutiModule-persist\src\main\java "type=" Xmlmapper " /> <table schema= "" Tablename= "Demo"/> </context> </generatorconfiguration ><!--Each modified part is replaced Targetpackage tableName part, corresponding database different table structure, different package name, automatically map out related class file--
1: Database Generation related table structure (surface demo)
2:eclipse using MAVEN, Project right-click, run as Execution (mybatis-generator:generate) such as:
After the execution is complete, refresh the project:
The red box labels are automatically generated related files: These files can be very simple to meet the needs of single-table crud, and do not have to learn new code, simple and easy to use.
In the process of use, as far as possible to record the parts of the XML changes every time (such as the Demomapper.xml section), so that if the changes in the structure of the table changes, directly regenerate the relevant class file/configuration file. Avoid mybatis this, after modifying the table structure (Demo.java Demoexample.java demomapper.xml), many files need to change the case (ORM).
MyBatis Generic Crud Replacement method