Mybatis逆向工程
Posted tiantongtong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis逆向工程相关的知识,希望对你有一定的参考价值。
1、在pom.xml中添加
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>
2、在项目下创建mbg.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="DB2Tables" targetRuntime="MyBatis3">
<!-- 逆向工程生成时不要注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mon201903"
userId="root"
password="1234">
</jdbcConnection>
<!-- 不管先 -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定JavaBean生成的位置 -->
<javaModelGenerator
targetPackage="com.sun.bean"
targetProject=".srcmainjava">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定sql映射文件生成位置 -->
<sqlMapGenerator
targetPackage="mapper"
targetProject=".srcmain esources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定dao接口生成位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.sun.dao"
targetProject=".srcmainjava">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- table指定每个表生成策略,指定实体类Bean的类名 -->
<table tableName="tab_emp" domainObjectName="Employee"></table>
<table tableName="tab_dept" domainObjectName="Dept"></table>
</context>
</generatorConfiguration>
3、编写测试类
public class Test { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mbg.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); } }
以上是关于Mybatis逆向工程的主要内容,如果未能解决你的问题,请参考以下文章