记录一哈-mybatis使用generator生成代码

来源:互联网 发布:淄博双轨制直销软件 编辑:程序博客网 时间:2024/06/15 07:09

这个是一个maven项目,当然要引用配置文件
pom.xml添加

            <plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.3.2</version>                <configuration>                    <verbose>true</verbose>                    <overwrite>true</overwrite>                </configuration>            </plugin>

使用这个插件需要添加一个配置文件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>    <!--jdbc配置文件-->    <properties resource="jdbc.properties"/>    <!--jdbc的路径-->    <classPathEntry            location="C:/Users/fei/.m2/repository/mysql/mysql-connector-java/5.1.22/mysql-connector-java-5.1.22.jar"/>    <context id="default" targetRuntime="MyBatis3">        <commentGenerator>            <!-- 不生成注解 -->            <property name="suppressAllComments" value="true"/>            <!--除去注释-->            <property name="suppressDate" value="true"/>        </commentGenerator>        <!--不解释-->        <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}">        </jdbcConnection>        <javaTypeResolver>            <!-- 对于数据库中DECIMAL or NUMERIC类型字段是否强制使用java.math.BigDecimal表示 ,搜的-->            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!-- Model模型生成器          targetPackage     指定生成的model生成所在的包名          targetProject     指定在该项目下所在的路径      -->        <javaModelGenerator targetPackage="com.fei.model" targetProject="src/main/java">            <!-- 是否对model添加 构造函数 -->            <property name="constructorBased" value="true"/>            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->            <property name="enableSubPackages" value="false"/>            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->            <property name="immutable" value="false"/>            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">            <property name="enableSubPackages" value="false"/>        </sqlMapGenerator>        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口        -->        <javaClientGenerator targetPackage="com.fei.dao" targetProject="src/main/java" type="MIXEDMAPPER">            <property name="enableSubPackages" value=""/>            <!--                    定义Maper.java 源代码中的ByExample() 方法的可视性,可选的值有:                    public;                    private;                    protected;                    default                    注意:如果 targetRuntime="MyBatis3",此参数被忽略             -->            <property name="exampleMethodVisibility" value=""/>            <!--              方法名计数器              Important note: this property is ignored if the target runtime is MyBatis3.             -->            <property name="methodNameCalculator" value=""/>            <!--              为生成的接口添加父接口             -->            <property name="rootInterface" value=""/>        </javaClientGenerator>        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>    </context></generatorConfiguration>

网上搜了一些,改改就是自己的了
我的jdbc.properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql:///testusername=rootpassword=root#定义初始连接数initialSize=0#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000

基本工作就好了,因为是maven的,启动命令是这个样子的

mybatis-generator:generate -e

运行后就是这个样子

这里写图片描述

然后没了,然后需要更多的网上搜搜,我也是那样的啊
,不知道搜的那几篇教程,如有侵权,请联系我

0 0
原创粉丝点击