sqlserver使用mybatisgenerator自动生成实体类、Mapper接口以及对应的XML文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_19671783/article/details/89331581
  1. 准备工作

你需要先准备一个sqljdbc4-4.0.jar

  1. pom文件新增配置

新增依赖

<dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.5</version>
</dependency>

增加插件配置

<plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5</version>
          <configuration>
            <verbose>true</verbose>
            <overwrite>true</overwrite>
          </configuration>
        </plugin>
  1. 放入generatorConfig.xml及数据库配置文件

其中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">
<generatorConfiguration>
    <properties resource="jdbc.properties"></properties>
    <classPathEntry location="${dbconfig.sqlServer.driverLocation}" />

    <context  targetRuntime="MyBatis3" id="a">

        <!--<commentGenerator>
            <!– 去除自动生成的注释 –>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>-->
        <!-- 是否生成注释 去除自动生成的注释-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>

            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 数据库连接配置 -->
        <jdbcConnection driverClass="${dbconfig.sqlServer.driverClasss}"
                        connectionURL="${dbconfig.sqlServer.ssmDemo.read.jdbcUrl}"
                        userId="${dbconfig.sqlServer.username}"
                        password="${dbconfig.sqlServer.password}" />

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--配置生成的实体包
            targetPackage:生成的实体包位置,默认存放在src目录下
            targetProject:目标工程名
         -->
        <javaModelGenerator targetPackage="com.demo.entity" targetProject="src/main/java" />

        <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources" />
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <!-- 配置表
            schema:不用填写
            tableName: 表名
            enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
            去除自动生成的例子

        <table schema="" tableName="UzaiSupplierStore" domainObjectName="UzaiSupplierStore" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>-->
        <table tableName="test_user_mybatis" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true"></table>

    </context>
</generatorConfiguration>

jdbc.properties中内容如下:

dbconfig.sqlServer.driverClasss=com.microsoft.sqlserver.jdbc.SQLServerDriver
dbconfig.sqlServer.ssmDemo.read.jdbcUrl=jdbc:sqlserver://127.0.0.1;DatabaseName=test
dbconfig.sqlServer.username=sa
dbconfig.sqlServer.password=admin
#定义初始连接数
dbconfig.initialSize=0
#定义最大连接数
dbconfig.maxActive=20
#定义最大空闲
dbconfig.maxIdle=20
#定义最小空闲
dbconfig.minIdle=1
#定义最长等待时间
dbconfig.maxWait=60000
dbconfig.sqlServer.driverLocation=F://maven_repository//com//microsoft//sqlserver//sqljdbc4//4.0//sqljdbc4-4.0.jar
  1. 运行测试

在数据库中新建表test_user_mybatis

并建立主键

alter table test_user_mybatis alter column id bigint not null;
alter table test_user_mybatis add constraint PK_test_user_mybatis primary key(id);

点击idea上方run-->edit configurations

在maven上点击加号新建

在命令行输入:

mybatis-generator:generate -e

点击OK,并运行,即可得到实体类、Mapper接口以及对应的XML文件。

猜你喜欢

转载自blog.csdn.net/sinat_19671783/article/details/89331581