1. Verwenden Sie das Maven -Projekt und fügen Sie Abhängigkeiten hinzu
<!-mybatis-plus begin-> <abhängigkeit> <gruppe> com.baomidou </Groupid> <artifactid> myBatis-plus </artifactid> <version> 2.2.0 </Version> </abhängig>
Es gibt auch eine Datenbankverbindung
<Depopenty> <gruppe> Mysql </Groupid> <artifactId> mysql-connector-java </artifactId> <Scope> Compile </scope> </abhängig>
Schließlich der Quellcode
import com.baomidou.mybatisplus.generator.autogenerator; import com.baomidou.mybatisplus.generator.config.datasourceConfig; import com.baomidou.mybatisplus.generator.config.globalconfig; import com.baomidou.mybatisplus.generator.config.packageConfig; import com.baomidou.mybatisplus.generator.config.strategyconfig; import com.baomidou.mybatisplus.generator.config.rules.dbType; import com.baomidou.mybatisplus.generator.config.rules.namingstrategy; import org.junit.jupiter.api.test; /** * MyBatis-plus generiert automatisch Code * * @Author Terry * @Version 1.0 * @date 2018-05-16 09:35 */public class SimpleMP {@test public void GenerateCode () {// Das Paketname String paketaName = "com.hciot.hhhhhhhhhhhhhhhhhhhhhhhhhhe"; // Benutzer -> UserService, auf True festgelegt: Benutzer -> iuserService boolean servicenamestartWithi = false; // Geben Sie den generierten Tabellennamen String [] tableNames = new String [] {"data_air_sensor_co", "order_product", "relation_device_gateway"} an; generateBytables (servicenamestartWithi, Packagename, TableName); } /** * Basierend auf der Tabelle automatisch generieren = getDataSourceConfig (); // Policy Configuration StrategyConfig StrategyConfig = getStrategyConfig (TableName); // Globale Variable -Konfiguration GlobalConfig GlobalConfig = getGlobalConfig (servicenamestartWithi); // Paketname Konfiguration PackageConfig PackageConfig = getPackageConfig (Packagename); // automatisch Atuogenerator generieren (DataSourceConfig, StrategyConfig, GlobalConfig, PackageConfig); } /** * Integration* * @param dataSourceConfig Configuration data source* @param strategyConfig Policy configuration* @param config Global variable configuration* @param packageConfig Package name configuration* @author Terry */ private void atuoGenerator(DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig, GlobalConfig config, PackageConfig packageConfig) {new autogenerator () .setGlobalConfig (config) .setDataSource (DataSourceConfig) .SetStrategy (StrategyConfig) .setPackageInfo (PackageConfig) .execute (); } / *** Paketname einstellen** @param packageName übergeordnetes Pfad -Paket -Paketname* @Return PackageConfig Paket Name Konfiguration* @Author Terry* / private packageConfig getPackaageConfig (String packagename) {return New PackageConfig () .setParent (packagename). .setController ("Controller") .setEntity ("Entity"); } / ** * Globale Konfiguration * * @param servicenamestartWithi false * @return GlobalConfig * @Author Terry * / privat GlobalConfig getGlobalConfig (boolean servicenamestartWithi) {GlobalConfig GlobalConfig = New GlobalConfig (); GlobalConfig .SetBaseColumnList (true) .setBaseSultMap (true) .setActivereCord (false) .setAuthor ("Terry") // Set den Ausgabempfad.SetoutputDir (GetOutputDir ("myBatis-plus") .SetFileOverride (true (true); if (! servicenamestartWithi) {// Setzen Sie den Dienstnamen globalConfig.setServiceName ("%sservice"); } Return GlobalConfig; } / *** Projektpfad zurückgeben** @param ProjectName -Projektname* @return Project Path* @Author Terry* / private String -GetOutputDir (String ProjectName) {String path = this.getClass (). GetClassloader (). GetResource (""). GetPath (); int index = path.indexof (projectName); return path.substring (1, index) + projectName + "/src/main/java/"; } / ** * Richtlinienkonfiguration * * @param tableName tableName Tabelle Name * @Return StrategyConfig * @Author Terry * / private StrategyConfig getStrategyConfig (String ... TableName) {return New StrategyConfig () // globale Kapitalisierungsnamen -Naming -Oracle -Note. Benennung (Standard -Falsch) .setDBColumnderderLine (TRUE) // Benennungsstrategie von der Datenbanktabelle zu Datei.SetNaming (NamingStrategy.underline_to_Camel) // Tabellenname, die generiert werden müssen, werden über Arrays.setinclude (tabbenamen) übergeben. }/** * Datenquelle konfigurieren * * @return dataSourceConfig * @author Terry */private dataSourceConfig getDataSourceConfig () {String dburl = "jdbc: mysql: // localHost: 3306/test"; Neue DataSourceConfig (). setDbType (dbType.mysql) .seturl (dburl) .setUserName ("root") .setPassword ("root") .setDriverName ("com.mysql.jdbc.driver"); } / *** Basierend auf der Tabelle automatisch generieren** @param packageName packageName packagename* @param tablenneams tischName tischName* / @SuppressWarnings ("Unbenutzt") private void generateByTables (String -Packagename, String ... TableName) {generateByTables (true, packgename, tabellenname); }}Zusammenfassen
Das obige ist der Implementierungscode basierend auf dem vom Editor vorgelegten MyBatis Plus -automatischen Codegenerator. Ich hoffe, es wird Ihnen hilfreich sein. Wenn Sie Fragen haben, hinterlassen Sie mir bitte eine Nachricht und der Editor wird Ihnen rechtzeitig antworten. Vielen Dank für Ihre Unterstützung auf der Wulin.com -Website!