天天看点

用mybatis的generator插件在项目中自动生成dao及entity

1、在Eclipse安装mybatis generator自动生成工具

(1)下载mybatis生成架包工具MyBatis_Generator_1.3.1.zip,解压jar包把features、plugins文件夹下的架包分别拷贝到eclipse安装目录下的features、plugins文件夹(复制和替换eclipse原有的这两个文件夹)。然后重启eclipse。

用mybatis的generator插件在项目中自动生成dao及entity

(2)在pom.xml中添加mybatis-generator-maven-plugin的jar包、以及连接数据库的包

<!-- mybatis generator自动生成jar包 -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <type>maven-plugin</type>
</dependency>

<!-- java连接数据库的包 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-Java</artifactId>
    <version>5.1.6</version>
</dependency>
           

(3)配置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>
    <!-- 数据库驱动包位置 (改成自己存放的具体路径) -->
    <classPathEntry location="D:/softwaretools/someNeeded/mysql-connector-java-5.1.34.jar" /> 

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/student" userId="root" password="root"> </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成模型的包名和位置 ,targetPackage:包名,targetProject:你的项目名-->
        <javaModelGenerator targetPackage="com.danni.model.entity" targetProject="mybatis-maven">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="com.danni.model.dao" targetProject="mybatis-maven">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.danni.model.dao" targetProject="mybatis-maven">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- tableName:数据库中的表名,domainObjectName:表名对应的类名 -->
        <table tableName="admins" domainObjectName="Admins" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="employee" domainObjectName="Employee" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="department" domainObjectName="Department" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>
           

(4)选中generator.xml,右键选择Generate MyBatis/IBATIS Artifacts,点击即可生成代码文件。

继续阅读