zoukankan      html  css  js  c++  java
  • MyBatisGenerator

    说明:此组件是自动生成VO类,自动生成DAO接口,自动生成写SQL语句的那个配置文件

    1.先导入所需要的pom依赖包:

    <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.7</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
            </dependency>
    

    2.在src/main/resources/目录下粘贴 database.properties资源文件

    #前提是D:\0A_jars\mysql-connector-java-5.1.47.jar存在
    db.driverLocation=D:\0A_jars\mysql-connector-java-5.1.47.jar db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://inheart.club:3306/museum?useUnicode=true&characterEncoding=UTF-8 db.username=root db.password=mysqladmin

    3.在src/main/resources/目录下粘贴mybatis-generator-config.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="database.properties"/>  <!-- database.properties资源文件的路径,导入数据库连接属性 -->
        <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包,路径不要出现中文-->
        <classPathEntry location="${db.driverLocation}"/>   <!-- 设置数据库驱动程序 -->
        <context id="YootkContext" targetRuntime="MyBatis3">
            <!-- 生成toString()方法 -->
            <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
            <commentGenerator>
                <property name="suppressDate" value="true"/>   <!-- 是否去除注释时间戳,去除为true -->
                <property name="suppressAllComments" value="true"/>    <!-- 是否去除注释,去除为true -->
            </commentGenerator>
            <jdbcConnection driverClass="${db.driverClassName}"
                            connectionURL="${db.url}"
                            userId="${db.username}"
                            password="${db.password}"> <!--数据库链接URL,用户名、密码 -->
            </jdbcConnection>
            <javaTypeResolver>  <!-- 在数据库类型和Java类型之间进行转换 -->
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
            <!-- 生成vo类的包名和位置-->
            <javaModelGenerator targetPackage="com.yootk.dubbo.vo" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>   <!-- 是否允许生成子包 -->
                <property name="constructorBased" value="false"/> <!-- 是否添加构造方法 -->
                <property name="trimStrings" value="true"/> <!-- 是否对数据去除空格 -->
                <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
                <property name="immutable" value="false"/>
            </javaModelGenerator>
            <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
            <sqlMapGenerator targetPackage="com.yootk.dubbo.vo.mapper" targetProject="src/main/java">
                <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.yootk.dubbo.dao" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
            <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是要生成的vo类名-->
            <table tableName="grade" domainObjectName="Grade" enableCountByExample="false"
                   enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            </table>
    
            <table tableName="active_type" domainObjectName="Activetype" enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            </table>
    
            <table tableName="extension" domainObjectName="Extension" enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
                <columnOverride column="code" javaType="java.lang.String" jdbcType="TEXT"/><!--如果数据表中字段是text类型的需要将其字段名称粘贴到此行红色字体处-->
            </table>
            <table tableName="project_state" domainObjectName="Projectstate" enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            </table>
        </context>
    </generatorConfiguration>
    

    4.粘贴运行类

    public class MbgTest {
        @Test
        public void testMbg() throws Exception {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File("src/main/resources/mybatis-generator-config.xml");//上面那个配置文件的路径
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        }
    }
    

      

  • 相关阅读:
    4.3.4 查询语法基础(2)
    4.3.4 查询语法基础(1)
    一个存储过程调用另一个存储过程 (2009-12-29 13:57:05)
    4.3.3 基于行的操作
    4.3.2 基于集合的操作
    4.3 数据操纵语言(DML)
    4.2 从哪儿开始
    子查询、联合查询和连接查询
    4.1 SQL的本质
    本书说明
  • 原文地址:https://www.cnblogs.com/wxl123/p/11111619.html
Copyright © 2011-2022 走看看