Mybatis generator (MBG)
Role: automatic generation of Bean object based on database tables, Java interfaces, and configuration file SqlMapper.xml
Setting up an MBG project
a) Download MBG core packages
b) Create a Java project
c) obtain configuration table from the official documentation, sample code
MyBatis Generator to the official website https://www.mybatis.org/generator/configreference/xmlconfig.html
Configuration table to paste the copied files you created in config.xml
d) introducing dependencies
2. MBG configuration and database tables generated according to the desired file (Bean, Interface, Mapper.xml):
Profiles:
<?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="MyGenerator" targetRuntime="MyBatis3">
<!-- 此标签用于去掉注释 -->
<commentGenerator>
<property name="suppressllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName =WJYLTeachingWebsite"
userId="sa"
password="ROOT">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- JavaBean配置 -->
<javaModelGenerator targetPackage="com.lk.domian" targetProject="src">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper.xml配置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- Java接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lk.mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 数据表 -->
<table tableName="t_stu" />
</context>
</generatorConfiguration>
Main method last executed:
public class Generator {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
operation result: