Maven项目中,使用mybatis,根据数据库自动生成pojo实体类daomapper

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven项目中,使用mybatis,根据数据库自动生成pojo实体类daomapper相关的知识,希望对你有一定的参考价值。

一、首先,用eclipse创建一个maven项目。

二、其次,在pom.xml文件下,加入如下插件配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.liyi.test</groupId>
  <artifactId>hello-mybatis</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>hello-mybatis Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>hello-mybatis</finalName>
      <pluginManagement>  
         <plugins>   
           <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.2</version>
            <configuration>
                <configurationFile>src/main/resources/generator.xml</configurationFile>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
             </configuration>
             <executions>
                <execution>
                    <id>Generate MyBatis Artifacts</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
             </executions>
             <dependencies>
                <dependency>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-core</artifactId>
                    <version>1.3.2</version>
                </dependency>
             </dependencies>
            </plugin>
        </plugins>  
       </pluginManagement>   
  </build>
</project>

三、从pom.xml中找到configurationFile标签,按照标签上的路径(src/main/resources/generator.xml)放入以下生成代码的核心配置文件,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:\Program Files\jeesite-master\doc\maven\repository\mysql\mysql-connector-java\5.1.37\mysql-connector-java-5.1.37.jar" /> 
     <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
     <context id="DB2Tables" targetRuntime="MyBatis3">
         <commentGenerator>
             <property name="suppressAllComments" value="true" />
         </commentGenerator>
         <!-- 数据库链接URL、用户名、密码 -->
          <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.254.78:3306/cjb_ecommerce" userId="root" password="123"> 
         <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
         </jdbcConnection>
         <javaTypeResolver>
             <property name="forceBigDecimals" value="false" />
         </javaTypeResolver>
         <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在cn.chejinbao.ecommerce.entity这个包下 -->
         <javaModelGenerator targetPackage="cn.chejinbao.ecommerce.entity" targetProject="D:\Program Files\eclipseworkspace\cn-chejinbao-ecommerce\src\main\java\">
                                                                                 
             <property name="enableSubPackages" value="true" />
             <property name="trimStrings" value="true" />
         </javaModelGenerator>
         <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在cn.chejinbao.ecommerce.mapping这个包下 -->
         <sqlMapGenerator targetPackage="cn.chejinbao.ecommerce.mapping" targetProject="D:\Program Files\eclipseworkspace\cn-chejinbao-ecommerce\src\main\java\">
             <property name="enableSubPackages" value="true" />
         </sqlMapGenerator>
         <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在cn.chejinbao.ecommerce.dao这个包下 -->
         <javaClientGenerator type="XMLMAPPER" targetPackage="cn.chejinbao.ecommerce.dao" targetProject="D:\Program Files\eclipseworkspace\cn-chejinbao-ecommerce\src\main\java\">
             <property name="enableSubPackages" value="true" />
         </javaClientGenerator>
         <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
         <table tableName="cjb_admin_role" domainObjectName="AdminRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_administrator" domainObjectName="Administrator" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_advertising" domainObjectName="Advertising" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_adverts" domainObjectName="Adverts" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_announcements" domainObjectName="Announcements" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_appointment" domainObjectName="Appointment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_brand" domainObjectName="Brand" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars" domainObjectName="Cars" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_base" domainObjectName="CarsBase" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_body" domainObjectName="CarsBody" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_chair" domainObjectName="CarsChair" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_chassis" domainObjectName="CarsChassis" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_control" domainObjectName="CarsControl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_engine" domainObjectName="CarsEngine" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_external" domainObjectName="CarsExternal" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_glass" domainObjectName="CarsGlass" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_high_technology" domainObjectName="CarsHighTechnology" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_img" domainObjectName="CarsImg" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_inside" domainObjectName="CarsInside" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_lease" domainObjectName="CarsLease" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_light" domainObjectName="CarsLight" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_media" domainObjectName="CarsMedia" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_refrigeration" domainObjectName="CarsRefrigeration" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_security" domainObjectName="CarsSecurity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_transmission_case" domainObjectName="CarsTransmissionCase" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_cars_wheel_brake" domainObjectName="CarsWheelBrake" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_city" domainObjectName="City" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_collection_cars" domainObjectName="CollectionCars" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_collection_stores" domainObjectName="CollectionStores" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_dictionaries_type" domainObjectName="DictionariesType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_dictionaries_value" domainObjectName="DictionariesValue" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_groups" domainObjectName="Groups" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_groups_permissions" domainObjectName="GroupsPermissions" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_information_type" domainObjectName="InformationType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_informations" domainObjectName="Informations" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_links" domainObjectName="Links" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_manager" domainObjectName="Manager" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_operation_log" domainObjectName="OperationLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_operations" domainObjectName="Operations" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_permissions" domainObjectName="Permissions" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_permissions_operations" domainObjectName="PermissionsOperations" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_provice" domainObjectName="Provice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_recommend_store" domainObjectName="RecommendStore" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_roles" domainObjectName="Roles" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_roles_groups" domainObjectName="RolesGroups" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_series" domainObjectName="Series" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_store_apply_enter" domainObjectName="StoreApplyEnter" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_stores" domainObjectName="Stores" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_stores_cars" domainObjectName="StoresCars" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_system_message" domainObjectName="SystemMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
         <table tableName="cjb_videos" domainObjectName="Videos" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />   
     </context>
</generatorConfiguration>

四、cmd打开dos窗口,到项目的根路径,比如我的

      cd D:\Program Files\eclipseworkspace\cn-chejinbao-ecommerce

       执行mvn mybatis-generator:generate(前提是你的电脑配置了maven的环境变量,可以用mvn命令)

技术分享

       刷新项目就有以下效果了。

技术分享

以上是关于Maven项目中,使用mybatis,根据数据库自动生成pojo实体类daomapper的主要内容,如果未能解决你的问题,请参考以下文章

IDEA+Maven实现MyBatis逆向工程

三尚筹网项目-后台-基于 Maven 的 MyBatis 逆向工程

三尚筹网项目-后台-基于 Maven 的 MyBatis 逆向工程

Maven搭建SpringMVC+Mybatis项目详解

Maven搭建SpringMVC+Mybatis项目详解

Eclipse Maven Mybatis的使用