做项目时,学到一个牛逼的逆向工程。
为什么要用它?
因为他能帮你自动生成dao层的mapper文件和mapper接口。实现对数据库的增删改,还有单表查询。完全不用自己写
dao接口和xml。
如何使用?
1.获取逆向工程的源码
(1)去官网下载sorce.zip.解压导入eclipse新工程 网址https://github.com/mybatis/generator/releases
(2)或者直接下载这里的工程导入eclipse 网址 https://pan.baidu.com/s/1yC_cZ3IMNT78ZYKpFJCNCg
密码:rvt7
导入时直接选择existing projects into workspace就行。只是普通工程
2.改generatorConfig.xml配置
修改导入工程中的generatorConfig.xml配置文件(修改JdbcConnection、修改生成的package和src位置)
<?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="testTables" targetRuntime="MyBatis3">
<!-- JavaBean 实现 序列化 接口 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
</plugin>
<!-- genenat entity时,生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/e3mall" userId="root"
password="Lijiexin">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.jackson.e3.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.jackson.e3.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.jackson.e3.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_desc"></table>
<table schema="" tableName="tb_item_param"></table>
<table schema="" tableName="tb_item_param_item"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_order_shipping"></table>
<table schema="" tableName="tb_user"></table>
</context>
</generatorConfiguration>
3.运行程序生成xml和接口
运行导入工程中的GeneratorSqlmap.java (main).会在导入工程中生成两个指定的package。
如果运行多次会生成多次,不会覆盖,只会重复。因此最好点击一次。如果要再次运行先把原先生成的删除
4.复制2个package到自己工程的dao层
把生成的xxMapper.java、xxMapper.xml两个package加到自己的工程中。逆向工程