首先创建一个MAVEN项目
在pom.xml中添加如下依赖
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>//mysql对应的版本,需要注意的
</dependency>
</dependencies>
从MyBatis官网看配置文件(http://mybatis.org/generator/quickstart.html#MyBatis3)
并创建配置文件generatorConfig.xml(官网有模板的)
jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"注意这个是对应mysql8.0版本的
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--MyBatis3Simple:生成简单版的CRUD
MyBatis:生成复杂版
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!--jdbcConnection:指定如何连接到目标数据库-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/atcrowdfunding_test?useUnicode=true& characterEncoding=UTF-8& userSSL=false& serverTimezone=GMT%2B8"
userId="root"
password="123456">
</jdbcConnection>
<!--javaTypeResolver:java类型的解析器-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--javaModelGenerator:指定javaBean的生成策略
targetPackage:目标包名
targetProject:目标工程
-->
<javaModelGenerator targetPackage="com.atguigu.atcrowdfunding.bean"
targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--sqlMapGenerator:sql映射生成策略-->
<sqlMapGenerator targetPackage="example.mapper"
targetProject="src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--javaClientGenerator:指定mapper接口所在的位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.atcrowdfunding.manager.dao"
targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--table:逆向分析哪些表,根据表创建javaBean-->
<table tableName="t_account_type_cert" domainObjectName="AccountTypeCert"></table>
<table tableName="t_advertisement" domainObjectName="Advertisement"></table>
<table tableName="t_cert" domainObjectName="Cert"></table>
<table tableName="t_dictionary" domainObjectName="Distionary"></table>
<table tableName="t_member" domainObjectName="Member"></table>
<table tableName="t_member_address" domainObjectName="MemberAddress"></table>
<table tableName="t_member_cert" domainObjectName="MemberCert"></table>
<table tableName="t_member_project_follow" domainObjectName="MemberProjectFollow"></table>
<table tableName="t_message" domainObjectName="Message"></table>
<table tableName="t_order" domainObjectName="Order"></table>
<table tableName="t_param" domainObjectName="Param"></table>
<table tableName="t_permission" domainObjectName="Permission"></table>
<table tableName="t_project" domainObjectName="Project"></table>
<table tableName="t_project_tag" domainObjectName="ProjectTag"></table>
<table tableName="t_project_type" domainObjectName="ProjectType"></table>
<table tableName="t_return" domainObjectName="Return"></table>
<table tableName="t_role" domainObjectName="Role"></table>
<table tableName="t_role_permission" domainObjectName="RolePermission"></table>
<table tableName="t_tag" domainObjectName="Tag"></table>
<table tableName="t_type" domainObjectName="Type"></table>
<table tableName="t_user" domainObjectName="User"></table>
<table tableName="t_user_role" domainObjectName="UserRole"></table>
</context>
</generatorConfiguration>
然后编写一个类(方法可从官网上获取)
public static void runMBG() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("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);
}
public static void main(String[] args) throws Exception {
runMBG();
}
然后运行即可