SpringBoot零基础入门指南4–配置generator自动生成数据库文件

1、在pom.xml 的<plugins>中添加

<!– Mybatis generator代码生成插件 配置 –>
<plugin>
    <groupId>org.mybatis.generator</groupId>
   
<artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>${plugin.mybatis.generator}</version>
    <configuration>
       
<configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
           
<artifactId>mybatis-generator-core</artifactId>
            <version>${plugin.mybatis.generator}</version>
            <type>jar</type>
        </dependency>
    </dependencies>

</plugin>

 

在<properties>中增加

<!– plugin setting –>
<mybatis.generator.generatorConfig.xml>${basedir}/src/test/resources/generatorConfig.xml
</mybatis.generator.generatorConfig.xml>
<mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/resources/generatorConfig.properties
</mybatis.generator.generatorConfig.properties>

 

重导入之后,maven插件裂变中会起这个插件

 MyBatis 1

 

添加mysql的连接jar包

<!– mysql-connector –>
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.30</version>
</dependency>

 

复导入后找到maven仓库里面的这个jar包 (jar包位置每个人不等,需好查看)

此处地址为:C:\\Users\\USERA\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.30\\mysql-connector-java-5.1.30.jar

 

每当目录Test文件加下新建resources文件夹并新建

generatorConfig.properties

generatorConfig.xml

零星独文本

 

generatorConfig.properties

# 数据库让jar 路径  刚刚下载的jar包的路径 需要改
drive.class.path=C:\\Users\\USERA\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.30\\mysql-connector-java-5.1.30.jar

# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

# 包路径配置
model.package=com.xingquan.entity
dao.package=com.xingquan.persistence
xml.mapper.package=com.xingquan.persistence

target.project=src/main/java
target.project.xml=src/main/resources

# 生成的表名  数据库需要映射的表名和浮动的文书称
gen.table-name=b
gen.domain-object-name=B

 

 

 

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"&gt;

<!– 配置Run As Maven build : Goals 参数 : mybatis-generator:generate
-Dmybatis.generator.overwrite=true –>
<!– 配置 tableName,使用 Run As Maven build 生成 dao model 层
–>
<generatorConfiguration>

    <!– 配置文件路径 –>
    <properties
url=”${mybatis.generator.generatorConfig.properties}”/>

    <!–数据库让包路径 –>
    <classPathEntry location=”${drive.class.path}”/>

    <context id=”DB2Tables” targetRuntime=”MyBatis3″>
        <!–关闭注释 –>
        <commentGenerator>
            <property name=”suppressAllComments” value=”true”/>
        </commentGenerator>

        <!–数据库连接信息 –>
        <jdbcConnection driverClass=”${jdbc.driver}”
connectionURL=”${jdbc.url}” userId=”${jdbc.username}”
                        password=”${jdbc.password}”>
        </jdbcConnection>

        <!–生成的model 包路径 –>
        <javaModelGenerator targetPackage=”${model.package}”
targetProject=”${target.project}”>
            <property name=”enableSubPackages” value=”ture”/>
            <property name=”trimStrings” value=”true”/>
        </javaModelGenerator>

        <!–生成xml mapper文件 路径 –>
        <sqlMapGenerator targetPackage=”${xml.mapper.package}”
targetProject=”${target.project.xml}”>
            <property name=”enableSubPackages” value=”ture”/>
        </sqlMapGenerator>

        <!– 生成的Dao接口 的包路径 –>
        <javaClientGenerator type=”XMLMAPPER”
targetPackage=”${dao.package}” targetProject=”${target.project}”>
            <property name=”enableSubPackages” value=”ture”/>
        </javaClientGenerator>

        <!–对许数据库表名 –>
        <!– 配置参考 –>
       
<!–http://generator.sturgeon.mopaas.com/configreference/table.html 
–>
        <table tableName=”${gen.table-name}”
domainObjectName=”${gen.domain-object-name}”>
            <property name=”useActualColumnNames” value=”true”
/>
        </table>
    </context>

</generatorConfiguration>

 

布好了后来运行插件:选中后点击开始按钮

 MyBatis 2

 

运转之后生成映射文件

 

 MyBatis 3

 

网站地图xml地图