MyBatis and Hibernate are persistent layer framework, mybatis appear than hibernate late, both frameworks I have used, for the advantages of both my feelings are not deep, personal feeling mybatis automatically generated model,mapping, The functionality of the mapper file reduces the number of encodings, but it is also prone to errors and is not easy to troubleshoot after an error.
I searched the internet for a bit about the comparison between MyBatis and Hibernate, I know this post is more detailed, we can refer to
https://www.zhihu.com/question/21104468
To automatically generate files, first download MyBatis Generator release,https://github.com/mybatis/generator/releases
The generated files can be placed directly under the project under the correct package directory, but personal advice or create a new folder, generated before the corresponding files copied into the different package path.
I am using the MySQL database, here also to download a database to connect to the jar package, official website: http://www.mysql.com/products/connector/
The key here is to configure the Generatorconfig.xml configuration file, I created the generatorconfig.xml in the Src/main/resources directory, the code is as follows
Generatorconfig.xml:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Htt P://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "><generatorconfiguration> <!--jar packages for database connections - <Classpathentry Location= "D:/code/myeclipse/mybatisgenerator/mysql-connector-java-5.1.40-bin.jar"/> <ContextID= "my"Targetruntime= "MyBatis3"> <Commentgenerator> < Propertyname= "Suppressdate"value= "false"/> < Propertyname= "Suppressallcomments"value= "true"/> </Commentgenerator> <!--Database Connection Configuration - <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://localhost/book"userId= "root"Password= "[email protected]"/> <JavamodelgeneratorTargetpackage= "Com.ese.book.pojo"Targetproject= "D:/code/myeclipse/mybatisgenerator/model"> < Propertyname= "Enablesubpackages"value= "true"/> < Propertyname= "Trimstrings"value= "true"/> </Javamodelgenerator> <SqlmapgeneratorTargetpackage= "Com.ese.book.mapping"Targetproject= "D:/code/myeclipse/mybatisgenerator/mapping"> < Propertyname= "Enablesubpackages"value= "true"/> </Sqlmapgenerator> <JavaclientgeneratorTargetpackage= "Com.ese.book.dao"Targetproject= "D:/code/myeclipse/mybatisgenerator/dao"type= "Xmlmapper"> < Propertyname= "Enablesubpackages"value= "true"/> </Javaclientgenerator> <TableTableName= "Books"Domainobjectname= "book"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false"> </Table> </Context></generatorconfiguration>
and copy this file to the same path as the above two jar packages. The contents of the folder are:
Once the condition is ready, you can generate the file. Generate statement:
Java-jar Mybatis-generator-core-1.3.5.jar-configfile Generatorconfig.xml-overwrite
Note: This statement is to be executed under the target folder path, which should be entered under the D:\code\myeclipse\mybatisGenerator folder by the command line first.
Bookmapper.java,bookmapping.xml and Book.java files will appear separately under the Dao,mapping,model folder after execution. Specific here is not given.
Automatically generate model,mapping and mapper files using Mybatisgenerator