逆向工程字面意思就是反向生成工程,和hibernate一样mybatis也有自己的逆向工程工具,hibernate的逆向生成我没有做过.
使用逆向工程时,需要注意的是表之间的关系无法映射出来.
亦即mybatis的逆向工程生成的都是单表操作.
1:mybatis逆向工程开发文档:
http://www.mybatis.org/genera...使用逆向工程生成代码有好几种方式,此处就介绍1种最简单的java程序生成.
0.一个在线快速生成的简单springboot项目
1.测试数据库
create database if not exists test00 default character set utf8 collate utf8_general_ci; use test00; create table citizen( id int(11) not null auto_increment comment '公民ID', citizenName varchar(20) not null comment '公民名字', PRIMARY KEY (`id`), KEY `cid` (`id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
<dependency> <groupId>io.github.orange1438</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>
<?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> <!--数据库驱动 --> <!-- 如果IDE(eclipse或者idea)项目里导入了jar包,那么就不需要配置了jar包的绝对路径了 <classPathEntry location="e:/project/mybatis/lib/mysql-connector-java-5.0.8-bin.jar"/> --> <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--数据库链接地址账号密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test00" userId="root" password="root"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--生成实体类存放位置 --> <javaModelGenerator targetPackage="cn.example.demo.po" targetProject="src/main/java"> </javaModelGenerator> <!--生成映射xml文件存放位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> </sqlMapGenerator> <!--生成Dao类存放位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.example.demo.mapper" targetProject="src/main/java"> </javaClientGenerator> <!--生成对应表及类名 --> <!--生成对应表及类名,需要记住的一点是逆向工程无法生成关联关系,只能生成单表操作 --> <table tableName="citizen" domainObjectName="Citizen" /> </context> </generatorConfiguration>
package cn.example.demo.util; import java.io.InputStream; import java.util.ArrayList; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; /** * 逆向生成 * * @author gzh * */ public class ReverseGeneratorFunction { public static void main(String[] args) throws Exception { ArrayList<String> warnings = new ArrayList<String>(); boolean overwrite = true; //文件名: generatorConfiguration.xml InputStream is = ReverseGeneratorFunction.class.getClassLoader().getResource("generatorConfiguration.xml") .openStream(); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); is.close(); System.out.println("生成代码成功,刷新项目,查看文件"); } }
运行main方法,分别生成实体类/映射xml文件/DAO方法,结果如下:
/* https://github.com/orange1438 */ package cn.example.demo.po; public class Citizen { /** * 公民ID */ private Integer id; /** * 公民名字 */ private String citizenname; /** * 获取 公民ID citizen.id * @return 公民ID */ public Integer getId() { return id; } /** * 设置 公民ID citizen.id * @param id 公民ID */ public void setId(Integer id) { this.id = id; } /** * 获取 公民名字 citizen.citizenName * @return 公民名字 */ public String getCitizenname() { return citizenname; } /** * 设置 公民名字 citizen.citizenName * @param citizenname 公民名字 */ public void setCitizenname(String citizenname) { this.citizenname = citizenname; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.example.demo.mapper.CitizenDAO"> <resultMap id="BaseResultMap" type="cn.example.demo.po.Citizen"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="citizenName" jdbcType="VARCHAR" property="citizenname" /> </resultMap> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from citizen where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="cn.example.demo.po.Citizen"> insert into citizen (id, citizenName) values (#{id,jdbcType=INTEGER}, #{citizenname,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKey" parameterType="cn.example.demo.po.Citizen"> update citizen set citizenName = #{citizenname,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select id, citizenName from citizen where id = #{id,jdbcType=INTEGER} </select> <select id="selectAll" resultMap="BaseResultMap"> select id, citizenName from citizen </select> </mapper>
/* https://github.com/orange1438 */ package cn.example.demo.mapper; import cn.example.demo.po.Citizen; import java.util.List; public interface CitizenDAO { /** * 根据ID删除 * @param id 主键ID * @return 返回删除成功的数量 */ int deleteByPrimaryKey(Integer id); /** * 添加对象所有字段 * @param record 插入字段对象(必须含ID) * @return 返回添加成功的数量 */ int insert(Citizen record); /** * 根据ID查询 * @param id 主键ID * @return 返回查询的结果 */ Citizen selectByPrimaryKey(Integer id); /** * * @return 返回查询的结果 */ List<Citizen> selectAll(); /** * 根据ID修改所有字段(必须含ID) * @param record 修改字段对象(必须含ID) * @return 返回更新成功的数量 */ int updateByPrimaryKey(Citizen record); }
pom内的 mysql-connector-java
连接依赖版本必须等于,或高于当前MySQL数据库版本,低了会报错,等于是最好的,高了也会报异常,但逆向生成照样成功.