mybatis逆向工程

62次阅读

共计 1940 个字符,预计需要花费 5 分钟才能阅读完成。

一.pom 文件, 加入如下代码

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/mybatis_generate.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>

                <dependencies>
                    <!-- 此处添加一个 mysql-connector-java 依赖可以防止找不到 jdbc Driver-->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.28</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

二.resources 文件夹下创建 mybatis_generate.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="context" targetRuntime="MyBatis3">

        <!--oracla-->
        <!--connectionURL="jdbc:oracle:thin:@172.16.182.18:1521:orcl"-->
        <!--driverClass="oracle.jdbc.driver.OracleDriver"-->
        <jdbcConnection
            driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=CTT"
            userId="root" password="root" />
        <javaModelGenerator
            targetPackage="com.rainbow.test.model"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="com.rainbow.test.mapper"
            targetProject="src/main/resources">
            <property name="enableSubPackages" value="false" />
            <property name="isMergeable" value="false" />
        </sqlMapGenerator>
        <javaClientGenerator
            targetPackage="com.rainbow.test.mapper"
            targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <table tableName="person"></table>
    </context>
</generatorConfiguration>    

三. 运行 maven 插件命令:mybatis-generator:generate

成功 …

正文完
 0

MyBatis逆向工程

63次阅读

共计 2835 个字符,预计需要花费 8 分钟才能阅读完成。

通过逆向工程生成 mapper 文件,大大提升了开发效率
一、引入依赖
<dependency>
<groupId>org.mybatis.generator</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>

<context id=”DB2Tables” targetRuntime=”MyBatis3″>
<commentGenerator>
<property name=”suppressAllComments” value=”true” />
</commentGenerator>
<jdbcConnection driverClass=”com.mysql.jdbc.Driver”
connectionURL=”jdbc:mysql://localhost:3306/shortvideo?characterEncoding=utf-8&amp;serverTimezone=GMT%2B8″
userId=”root”
password=”123456″>
</jdbcConnection>

<javaTypeResolver >
<property name=”forceBigDecimals” value=”false” />
</javaTypeResolver>

<!– 指定 javabean 生成的位置 –>
<javaModelGenerator targetPackage=”com.imooc.pojo” targetProject=”.\src\main\java”>
<property name=”enableSubPackages” value=”true” />
<property name=”trimStrings” value=”true” />
</javaModelGenerator>

<!– 指定 mapper 文件生成的位置 –>
<sqlMapGenerator targetPackage=”mapper” targetProject=”.\src\main\resources”>
<property name=”enableSubPackages” value=”true” />
</sqlMapGenerator>

<!– 指定 mapper 接口生成的位置 –>
<javaClientGenerator type=”XMLMAPPER” targetPackage=”com.imooc.mapper” targetProject=”.\src\main\java”>
<property name=”enableSubPackages” value=”true” />
</javaClientGenerator>

<table tableName=”vuser” domainObjectName=”Users”></table>
<table tableName=”bgm”></table>
<table tableName=”comments”></table>
<table tableName=”search_records”></table>
<table tableName=”users_fans”></table>
<table tableName=”users_like_videos”></table>
<table tableName=”users_report”></table>
<table tableName=”videos”></table>
</context>
</generatorConfiguration>
三、编写启动类
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MybatisGenerator {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件的路径,相对或者绝对路径都可以
File configFile = new File(“E:\\idea\\short-video\\src\\main\\java\\com\\imooc\\utils\\mybatisGenerator\\mbg”);
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);
}
}

正文完
 0