zoukankan      html  css  js  c++  java
  • idea中mybatis generator自动生成代码配置 数据库是sqlserver

    好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图

    generatorConfig.xml and jdbc.properties都要放在resource根目录下

    具体generatorConfig.xml内容如下:

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <!DOCTYPE generatorConfiguration
    3.  
      PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    4.  
      "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    5.  
      <generatorConfiguration>
    6.  
      <properties resource="jdbc.properties"></properties>
    7.  
      <classPathEntry location="${dbconfig.sqlServer.driverLocation}" />
    8.  
       
    9.  
      <context targetRuntime="MyBatis3">
    10.  
       
    11.  
      <!--<commentGenerator>
    12.  
      <!– 去除自动生成的注释 –>
    13.  
      <property name="suppressAllComments" value="true" />
    14.  
      </commentGenerator>-->
    15.  
      <!-- 是否生成注释 去除自动生成的注释-->
    16.  
      <commentGenerator>
    17.  
      <property name="suppressDate" value="true"/>
    18.  
       
    19.  
      <property name="suppressAllComments" value="true"/>
    20.  
      </commentGenerator>
    21.  
      <!-- 数据库连接配置 -->
    22.  
      <jdbcConnection driverClass="${dbconfig.sqlServer.driverClasss}"
    23.  
      connectionURL="${dbconfig.sqlServer.ssmDemo.read.jdbcUrl}"
    24.  
      userId="${dbconfig.sqlServer.username}"
    25.  
      password="${dbconfig.sqlServer.password}" />
    26.  
       
    27.  
      <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    28.  
      <javaTypeResolver>
    29.  
      <property name="forceBigDecimals" value="false"/>
    30.  
      </javaTypeResolver>
    31.  
       
    32.  
      <!--配置生成的实体包
    33.  
      targetPackage:生成的实体包位置,默认存放在src目录下
    34.  
      targetProject:目标工程名
    35.  
      -->
    36.  
      <javaModelGenerator targetPackage="com.pojo"
    37.  
      targetProject="src/main/java" />
    38.  
       
    39.  
      <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
    40.  
      <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources" />
    41.  
      <!--生成Dao类存放位置-->
    42.  
      <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="src/main/java">
    43.  
      <property name="enableSubPackages" value="true"/>
    44.  
      </javaClientGenerator>
    45.  
      <!--生成对应表及类名-->
    46.  
      <!-- 配置表
    47.  
      schema:不用填写
    48.  
      tableName: 表名
    49.  
      enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
    50.  
      去除自动生成的例子
    51.  
      -->
    52.  
      <table schema="" tableName="UzaiSupplierStore" domainObjectName="UzaiSupplierStore" enableCountByExample="false" enableSelectByExample="false"
    53.  
      enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
    54.  
      </table>
    55.  
      <table schema="" tableName="UzaiCountry" enableCountByExample="false" enableSelectByExample="false"
    56.  
      enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
    57.  
      </table>
    58.  
       
    59.  
      </context>
    60.  
      </generatorConfiguration>

     jdbc.properties内容如下

    dbconfig.sqlServer.driverClasss=com.microsoft.sqlserver.jdbc.SQLServerDriver
    dbconfig.sqlServer.ssmDemo.read.jdbcUrl=jdbc:sqlserver://127.0.0.1;DatabaseName=Travel
    dbconfig.sqlServer.username=uzai_trip
    dbconfig.sqlServer.password=Flzx3qC!$
    #定义初始连接数
    dbconfig.initialSize=0
    #定义最大连接数
    dbconfig.maxActive=20
    #定义最大空闲
    dbconfig.maxIdle=20
    #定义最小空闲
    dbconfig.minIdle=1
    #定义最长等待时间
    dbconfig.maxWait=60000
    dbconfig.sqlServer.driverLocation=C:/Users/zd69155/Documents/mssql-jdbc-6.3.0.jre7-preview.jar
    C:/Users/zd69155/Documents/mssql-jdbc-6.3.0.jre7-preview.jar这个地址一定是在本地下载好这个jar包的地址
    可以添加下面的dependency下载SQLjdbc jar包

    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
    <dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.3.0.jre7-preview</version>
    <scope>test</scope>
    </dependency>

    jdbc.properties的参数是对应generatorConfig.xml里面的,如下图

     

    然后就是需要配置pom.xml

    以上配置完成以后,点run弹出如下界面  

    mybatis-generator:generate -e 

    如果碰到这样的错误 “The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:972101e7-9ee5-4004-9abc-e0e932b921bd"

    可以尝试

    mybatis-generator:generate -Djdk.tls.client.protocols=TLSv1.2

    以上都配置以后点击运行就可以生成了

    目前发的生成的dao文件名字是Mapper,还有就是生成的数据库类没有中文注释,大多数解决方案是mysql数据库。关于sqlserver还在进一步了解中

  • 相关阅读:
    【crontab】误删crontab及其恢复
    New Concept English there (7)
    New Concept English there (6)
    New Concept English there (5)
    New Concept English there (4)
    New Concept English there (3)
    New Concept English there (2)Typing speed exercise
    New Concept English there (1)Typing speed exercise
    New Concept English Two 34 game over
    New Concept English Two 33 94
  • 原文地址:https://www.cnblogs.com/sjqq/p/10109279.html
Copyright © 2011-2022 走看看