1.新建项目导入jar包
-
新建一个新的项目,java项目,web项目,maven项目无所谓
-
导入需要的jar包
需要导入2个jar包:
一个是数据库的驱动包,mysql导mysql的驱动,oracle导入oracle的驱动
第二个就是反向生成包:mybatis-generator-core-1.3.2.jar
2:在src下新建一个xml配置文件generatorConfig.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="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码,根据自己安装时的账户和密码进行填写-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/dbname" userId="root" password="root">
</jdbcConnection>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short,)
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成实体类存放位置,这里是可以自动生成的,不需要创建-->
<javaModelGenerator targetPackage="com.sky.entity" targetProject="src">
<property name="enableSubPackages" value="true"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件(mapper.xml)存放位置,也不需要创建,可以生成-->
<sqlMapGenerator targetPackage="com.sky.mapping" targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Mapper接口存放位置, 不要创建,会生成-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.sky.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名,你想生成几个就给你生成几个,tableName:表名,domainObjectName:实体类名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="usertype" domainObjectName="Usertype" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="zhongjie" domainObjectName="Zhongjie" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<!-- <table tableName="t_contect" domainObjectName="Contect" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
<!-- <table tableName="t_county" domainObjectName="County" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
<!-- <table tableName="t_course" domainObjectName="Course" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
</context>
</generatorConfiguration>
3:新建一个类,并执行
在src下或者找个地方新建一个类,在类中书写如下代码并运行:
// 定义一个核心的方法然后调用
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件的地址,注意:写绝对路径
File configFile = new File("F:\\soft\\idea\\workspace\\XXXXXXX\\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 {
try {
// Auto - 创建的类名
Auto generatorSqlmap = new Auto();
//调用方法
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
4:执行main方法即可生成:
注意:一定要将java文件中的配置文件地址修改了。修改为自己本地的绝对路径。
如果表比较多的话可能执行的比较慢,等就行了
执行完成之后会生成mapper接口,mapper.xml文件,实体类
并且在mapper.xml文件中会生成简单的增删改语句。