回顾一下MyBatis逆向工程——自动生成代码

前言

最近做的项目(SSM+Shiro)的数据库表已经创建完成,一共有15张表,如果我们一个个去写pojo/bean的代码以及各种sql语句的话未免太过麻烦而且很容易出错,这个时候我们就需要MyBatis逆向工程去为我们生成这些基本的东西。
先来简单的了解一下什么是逆向工程

一 什么是逆向工程

官网解释浓缩版:
MyBatis逆向工程需要用到的就是MyBatis官方提供的MyBatis Generator(MBG)。MBG是MyBatis和iBATIS的代码生成器,它将为所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代码。MBG对简单CRUD(增删改查)的大部分数据库操作产生重大影响。但是您仍然需要为连接查询或存储过程手动编写SQL和对象代码。
简要概括版:
创建好数据库表之后,MBG可以根据数据库表自动为您生成pojo类、example类(用于添加条件,相当where语句后面的部分 )、mapper文件。
关于Mapper接口以及Example的实例的讲解可以查看这一篇文章:《Mapper接口以及Example的实例》
blog.csdn.net/biandous/ar…

介绍完逆向工程是个什么东西后,接下来我们就要开始学习如何使用它了?

二 使用MyBatis逆向工程

考虑到大家的基础可能不同,所以我尽可能详细一点。
2.1 新建Maven项目并添加相关依赖
1.新建Maven项目(普通的Java项目):
回顾一下MyBatis逆向工程——自动生成代码

2.添加相关依赖(pom.xml):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
<modelVersion>4.0.0</modelVersion>

<groupId>mybatis-generator</groupId>
<artifactId>sql-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sql-mapper</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!--log4j -->
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!--MyBatis Generator -->
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>
    <!--mysql-connector-java -->
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
    <!--mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.1</version>
    </dependency>
</dependencies>

</project>

2.2 项目创建完成之后的目录结构
回顾一下MyBatis逆向工程——自动生成代码
2.3 MyBatis逆向工程代码编写
核心代码示例(推荐读取xml配置文件的形式,还有一种基于Java的配置这里就不做演示):
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//读取xml配置文件,推荐使用这种方式
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);
本项目中使用代码MyBatisGeneratorApp.java:
public class MyBatisGeneratorApp {

public void generator() 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 {
    try {
        MyBatisGeneratorApp generatorSqlmap = new MyBatisGeneratorApp();
        generatorSqlmap.generator();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}
2.4 MyBatis逆向工程配置文件以及log4j配置文件编写
MyBatis逆向工程配置文件如下(注释已经解释的很详细了,我这里就不多做解释了):
<?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"&gt;

<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 ,加上“useSSL=false”是因为我SSL连接数据库出现了错误 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/kpi_project?useSSL=false"
userId="root" password="xxx">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

    <!-- targetProject:生成pojo类的位置 -->
    <javaModelGenerator targetPackage="pojo"
        targetProject=".\src\main\java">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
        <!-- 从数据库返回的值被清理前后的空格 -->
        <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!-- targetProject:mapper映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\java">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>

    <!-- targetPackage:mapper接口生成的位置 -->
    <javaClientGenerator type="XMLMAPPER"
        targetPackage="mapper" targetProject=".\src\main\java">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </javaClientGenerator>

    <!-- 指定数据库表 -->
    <table schema="" tableName="com_role"></table>
    <table schema="" tableName="com_rule"></table>
    <table schema="" tableName="kpi_assess"></table>
    <table schema="" tableName="kpi_assign"></table>
    <table schema="" tableName="kpi_complain"></table>
    <table schema="" tableName="kpi_index"></table>
    <table schema="" tableName="kpi_institute"></table>
    <table schema="" tableName="kpi_peformance"></table>
    <table schema="" tableName="kpi_result"></table>
    <table schema="" tableName="kpi_score"></table>
    <table schema="" tableName="sys_permission"></table>
    <table schema="" tableName="sys_role"></table>
    <table schema="" tableName="sys_role_perm"></table>
    <table schema="" tableName="sys_user"></table>
    <table schema="" tableName="sys_user_role"></table> 
</context>

</generatorConfiguration>

2.5 运行结果

pojo及example类:回顾一下MyBatis逆向工程——自动生成代码
mapper接口及mapper.xml:回顾一下MyBatis逆向工程——自动生成代码
我们已经介绍了MyBatis逆向工程以及它的使用,下面我们简单的总结与补充一下所学的知识。

三 总结

什么是逆向工程?
MBG可以根据数据库表自动为您生成pojo类、example类(用于添加条件,相当where语句后面的部分 )、mapper文件`。

如何使用逆向工程?
pom文件(添加相关jar包)->逆向工程核心代码编写->创建generatorConfig.xml配置文件(可以加上log4j日志打印配置文件)

转载于:https://blog.51cto.com/13792737/2140603

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值