一、 MyBatis Generator介绍
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.4</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/mybatis-generator.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
(2) src/main/resources/mybatis-generator.xml
<?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>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="cn.xiaows.app.util.MyMapper"/>
</plugin>-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"
userId="root"
password="123456"/>
<!-- 对于生成的entity所在包 -->
<javaModelGenerator targetPackage="cn.app.entity0" targetProject="src/main/java"/>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper0" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="cn. app.dao0" targetProject="src/main/java" type="XMLMAPPER" />
<table tableName="%"/><!-- 所有表 -->
<!--<table tableName="company" mapperName="CompanyDao"/>-->
<!--<table tableName="user" mapperName="UserDao"/>-->
<!--...-->
</context>
(3) 生成命令:(maven)或直接运行maven的generator插件
mybatis-generator:generate -f pom.xml
<dependencies>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- mybatis-generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.4</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
(2) MybatisGeneratorMain.java
public class MybatisGeneratorMain {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(new File("app-logs-web/src/main/resources/mybatis-generator.xml"));
// Configuration config = cp.parseConfiguration(ClassLoader.getSystemResourceAsStream("mybatis-generator.xml"));
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
for (String warning : warnings) {
System.out.println(warning);
}
}
}
(3) mybatis-generator.xml
<?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>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="cn.xiaows.app.util.MyMapper"/>
</plugin>-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"
userId="root"
password="123456">
</jdbcConnection>
<!-- 对于生成的entity所在包 -->
<javaModelGenerator targetPackage="cn.xiaows.app.entity" targetProject="app-logs-web/src/main/java"/>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="app-logs-web/src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="cn.xiaows.app.dao" targetProject="app-logs-web/src/main/java" type="XMLMAPPER" />
<table tableName="%"/><!-- 所有表 -->
<!--<table tableName="user" mapperName="UserDao"/>-->
<!--<table tableName="project" mapperName="ProjectDao"/>-->
<!--...-->
</context>
</generatorConfiguration>
(4) springboot启动类:AppLogApplication.java
@SpringBootApplication
@MapperScan("cn.app.dao")
public class AppLogApplication {
public static void main(String[] args) {
SpringApplication.run(AppLogsWebApplication.class, args);
}
}