mybatis逆向工程的配置文件,maven引入mybatis逆向工程插件。资源文件,Mybatis的全局配置文件

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">
<generatorConfiguration>
<context id="sqlGenerate" targetRuntime="MyBatis3">
    <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    <commentGenerator>
        <property name="suppressAllComments" value="true" />
    </commentGenerator>

<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/huangjin?serverTimezone=UTC"
                userId="root" password="mysql">
</jdbcConnection>

<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
    <property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- 生成Pojo包名和位置 -->
<javaModelGenerator targetPackage="com.hj.pojo"
                    targetProject="src/main/java/">
    <!-- enableSubPackages:是否让schema作为包的后缀 -->
    <property name="enableSubPackages" value="true" />
    <!-- 清理前后的空格 -->
    <property name="trimStrings" value="true" />
</javaModelGenerator>

<!-- 生成Mapper映射XML文件位置 -->
<sqlMapGenerator targetPackage="com.hj.Mapper"
                 targetProject="src/main/resources/">
    <property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<!-- 生成Mapper接口文件位置 -->
<javaClientGenerator type="XMLMAPPER"
                     targetPackage="com.hj.Mapper" targetProject="src/main/java/">
    <property name="enableSubPackages" value="true" />
</javaClientGenerator>

<!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
<!-- tableName:要生成的表名
domainObjectName:生成后的实例名
enableCountByExample:Count语句中加入where条件查询,默认为true开启
enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
-->
<table tableName="hjcg" domainObjectName="Hjcg" />
       <!--enableCountByExample="false" enableUpdateByExample="false"-->
       <!--enableDeleteByExample="false" enableSelectByExample="false"-->
       <!--selectByExampleQueryId="false"-->
<!--<table tableName="category" />-->
<!--<table tableName="order"/>-->
<!--<table tableName="product"/>-->
<!--<table tableName="order_detail"/>-->
</context>
</generatorConfiguration>

连接数据库的资源文件 db.properties

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/huangjin?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
db.username=root
db.password=mysql

maven引入mybatis逆向工程插件 在pom.xml文件中配置

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.15</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <!--是否将生成过程输出到控制台-->
                    <verbose>true</verbose>
                    <!--是否覆盖同名文件-->
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

有了如上的配置之后,就可以使用mybatis的逆向工程了。

mybaits实施逆向工程之后,就要使用了,这时候需要mybatis的全局配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 加载java的配置文件或者声明属性信息 -->
    <properties resource="db.properties"></properties>

    <!-- <settings></settings> -->

    <!-- 自定义别名 -->
    <!--此处为mybatis全局变量,全局变量中,能用到别名的地方只有映射文件中的传递参数类型-->
    <!--<typeAliases>-->
        <!--&lt;!&ndash; 单个别名定义 &ndash;&gt;-->
        <!--&lt;!&ndash; <typeAlias type="com.itheima.mybatis.po.User" alias="user"/> &ndash;&gt;-->

        <!--&lt;!&ndash; 批量别名定义(推荐) &ndash;&gt;-->
        <!--&lt;!&ndash; package:指定包名称来为该包下的po类声明别名,默认的别名就是类名(首字母大小写都可) &ndash;&gt;-->
        <!--<package name="com.itheima.mybatis.po" />-->
    <!--</typeAliases>-->

    <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
    <environments default="development">
        <environment id="development">
            <!-- 配置JDBC事务控制,由mybatis进行管理 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源,采用mybatis连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}" />
                <property name="url" value="${db.url}" />
                <property name="username" value="${db.username}" />
                <property name="password" value="${db.password}" />
            </dataSource>
        </environment>
    </environments>

    <!-- 加载映射文件 -->
    <mappers>
        <!--<mapper resource="User.xml" />-->
        <!-- <mapper resource="mapper/UserMapper.xml" /> -->

        <!-- 批量加载映射文件 -->
        <package name="com.hj.Mapper" />
    </mappers>
</configuration>

全局配置文件配置之后,就可以进行连接数据库进行增删改查了。

猜你喜欢

转载自blog.csdn.net/hjkuanggong/article/details/89386473