TemplarJQ / migration-system-eureka-server

服务迁移系统的注册中心节点,主要由Eureka的服务端代码组成
0 stars 0 forks source link

[Project] - 如何引入mybatis #4

Open TemplarJQ opened 3 years ago

TemplarJQ commented 3 years ago

记录下引入mybatis和生成器的使用过程

TemplarJQ commented 3 years ago

首先引入connector

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.6</version>
    </dependency>

然后引入druid连接池和spring配置

    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>
TemplarJQ commented 3 years ago
TemplarJQ commented 3 years ago

在resources/mybatis-generator.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>
    <!--JDBC驱动jar包的位置-->
    <!--<classPathEntry location="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/>-->
    <context id="default" targetRuntime="MyBatis3">
        <!--创建Java类时是否取消生成注释-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--JDBC数据库连接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/migration"
                        userId="root"
                        password="buptljq1234">
        </jdbcConnection>
        <!--
        Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
        targetPackage     指定生成的model生成所在的包名
        targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.dataobject"
                            targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="true"/>
            <!-- 是否对model添加构造函数 -->
            <!--<property name="constructorBased" value="true"/>-->
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <!--<property name="immutable" value="false"/>-->
        </javaModelGenerator>
        <!--
        mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件
        -->
        <sqlMapGenerator targetPackage="mapping"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--
        客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
        type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
        type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
        type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.dao"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--tables表及类名-->
        <table tableName="user_info" domainObjectName="UserDO"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="user_password" domainObjectName="UserPasswordDO"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>
TemplarJQ commented 3 years ago

修改配置信息后在navicat中设置新的数据库名称,编码为utf-8,utf8-unicode-ci

TemplarJQ commented 3 years ago