MyBatis Generator入门配置
在日常开发中使用mybatis作为持久层框架要写entity,dao,mapper接口,mapper.xml做CRUD这些重复操作,mybatis官方提供了MyBatis Generator为我们做这些工作。
由于使用该插件有很多方式,目前介绍一种使用maven插件的方式。
引入pom配置文件
<!-- mybatis-generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
配置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>
<!--配置数据库连接的位置-->
<!-- <properties url="metersphere.properties"/>-->
<!-- 设置mysql驱动路径 -->
<!--<classPathEntry location="/Users/test/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar"/>-->
<!-- 此处指定生成针对MyBatis3的DAO -->
<context id="mysql" targetRuntime="MyBatis3">
<!-- 字段带`,解决列表跟关键字冲突问题 -->
<property name="autoDelimitKeywords" value="true" />
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 用来除去时间信息的,这在配合类似subversion的代码管理工具时使用很有效,因为可以减少没有必要的注释迁入 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 关闭自动生成的注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- jdbc连接信息 --> <!-- EduLoanManage EduTestDataBase -->
<!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.20.180:3306/fit2cloud"-->
<!--userId="root" password="Fit2cloud2015!" />-->
<!-- <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"-->
<!-- connectionURL="${spring.datasource.url}&nullCatalogMeansCurrent=true"-->
<!-- userId="${spring.datasource.username}" password="${spring.datasource.password}"/>-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://*.*.*:3306/metersphere?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8"
userId="root" password="****"/>
<!-- javaTypeResolver式类型转换的信息 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 模型对象 -->
<javaModelGenerator targetPackage="io.metersphere.base.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- XML映射文件 -->
<sqlMapGenerator targetPackage="io.metersphere.base.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="io.metersphere.base.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--要生成的数据库表 -->
<table tableName="table_name_1"/>
<table tableName="table_name_2"/>
<!-- 表名和关键字冲突-->
<!-- <table tableName="group" delimitIdentifiers="true"></table>-->
</context>
<!-- <settings>-->
<!-- <!– 打印sql日志 –>-->
<!-- <setting name="logImpl" value="STDOUT_LOGGING" />-->
<!-- </settings>-->
</generatorConfiguration>
编写启动类
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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("src/main/resources/mybatis-generator.xml"));
//Configuration config = cp.parseConfiguration(ClassLoader.getSystemResourceAsStream("generatorConfig.xml"));
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
for (String warning : warnings) {
System.out.println(warning);
}
}
}
转载自:https://juejin.cn/post/7257716027210792997