eclipse中利用Maven逆向工程生成PO类以及mapper(mybatis)

在pom.xml的project>build里面添加如下代码,让maven环境支持mybatis-generator组件

 

  1.  
    <pluginManagement>
  2.  
    <plugins>
  3.  
    <plugin>
  4.  
    <groupId>org.mybatis.generator</groupId>
  5.  
    <artifactId>mybatis-generator-maven-plugin</artifactId>
  6.  
    <version>1.3.2</version>
  7.  
    <configuration>
  8.  
    <configurationFile>src/main/resources/generator.xml</configurationFile>
  9.  
    <verbose>true</verbose>
  10.  
    <overwrite>true</overwrite>
  11.  
    </configuration>
  12.  
    <executions>
  13.  
    <execution>
  14.  
    <id>Generate MyBatis Artifacts</id>
  15.  
    <goals>
  16.  
    <goal>generate</goal>
  17.  
    </goals>
  18.  
    </execution>
  19.  
    </executions>
  20.  
    <dependencies>
  21.  
    <dependency>
  22.  
    <groupId>org.mybatis.generator</groupId>
  23.  
    <artifactId>mybatis-generator-core</artifactId>
  24.  
    <version>1.3.2</version>
  25.  
    </dependency>
  26.  
    </dependencies>
  27.  
    </plugin>
  28.  
    <plugin>
  29.  
    <groupId>org.apache.maven.plugins</groupId>
  30.  
    <artifactId>maven-surefire-plugin</artifactId>
  31.  
    <version>2.19.1</version>
  32.  
    <configuration>
  33.  
    <skipTests>true</skipTests>
  34.  
    </configuration>
  35.  
    </plugin>
  36.  
     
  37.  
    <plugin>
  38.  
    <groupId>org.apache.maven.plugins</groupId>
  39.  
    <artifactId>maven-resources-plugin</artifactId>
  40.  
    <version>3.0.1</version>
  41.  
    <configuration>
  42.  
    <encoding>UTF-8</encoding>
  43.  
    </configuration>
  44.  
    </plugin>
  45.  
    </plugins>
  46.  
    </pluginManagement>


注:如果在dependencies中已经引入mysql-connector-java则不需加入以下dependency,反之加入

  1.  
    <dependency>
  2.  
    <groupId>mysql</groupId>
  3.  
    <artifactId>mysql-connector-java</artifactId>
  4.  
    <version>5.1.35</version>
  5.  
    <scope>runtime</scope>
  6.  
    </dependency>

 

2、generator.xml配置文件

 

需逆向的表:

逆向代码生成目录结构:

对应配置:

 

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3.  
    <generatorConfiguration>
  4.  
    <!-- 数据库驱动包位置 -->
  5.  
    <classPathEntry
  6.  
    location="E:\apache-maven-3.3.9\repo\mysql\mysql-connector-java\5.1.18\mysql-connector-java-5.1.18.jar" />
  7.  
    <context id="Tables" targetRuntime="MyBatis3">
  8.  
    <commentGenerator>
  9.  
    <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  10.  
    <property name="suppressAllComments" value="true" />
  11.  
    </commentGenerator>
  12.  
    <!-- 数据库链接URL、用户名、密码 -->
  13.  
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  14.  
    connectionURL="jdbc:mysql://192.168.100.52:3306/dev_test" userId="数据库用户名"
  15.  
    password="数据库密码">
  16.  
    <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
  17.  
    userId="msa" password="msa"> -->
  18.  
    </jdbcConnection>
  19.  
    <javaTypeResolver>
  20.  
    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
  21.  
    <property name="forceBigDecimals" value="true" />
  22.  
    </javaTypeResolver>
  23.  
    <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在com.loan.test.entity这个包下 -->
  24.  
    <javaModelGenerator targetPackage="com.loan.test.entity"
  25.  
    targetProject=".\src\main\java\">
  26.  
    <!-- enableSubPackages:是否让schema作为包的后缀 -->
  27.  
    <property name="enableSubPackages" value="true" />
  28.  
    <!-- 从数据库返回的值被清理前后的空格 -->
  29.  
    <property name="trimStrings" value="true" />
  30.  
    </javaModelGenerator>
  31.  
    <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在com.loan.test.dao.xml这个包下 -->
  32.  
    <sqlMapGenerator targetPackage="com.loan.test.dao.xml"
  33.  
    targetProject=".\src\main\java\">
  34.  
    <!-- enableSubPackages:是否让schema作为包的后缀 -->
  35.  
    <property name="enableSubPackages" value="true" />
  36.  
    </sqlMapGenerator>
  37.  
    <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在com.loan.test.dao.mapper这个包下 -->
  38.  
    <javaClientGenerator type="XMLMAPPER"
  39.  
    targetPackage="com.loan.test.dao.mapper" targetProject=".\src\main\java\">
  40.  
    <!-- enableSubPackages:是否让schema作为包的后缀 -->
  41.  
    <property name="enableSubPackages" value="true" />
  42.  
    </javaClientGenerator>
  43.  
    <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
  44.  
    <table tableName="test" domainObjectName="Test"
  45.  
    enableCountByExample="false" enableUpdateByExample="false"
  46.  
    enableDeleteByExample="false" enableSelectByExample="false"
  47.  
    selectByExampleQueryId="false" />
  48.  
    </context>
  49.  
    </generatorConfiguration>

 

 

3、运用maven指令生成逆向工程

 

项目右键->run as->maven build...,Goals:中输入mybatis-generator:generate

.

4、刷新项目,结果

1)Test.java

 

  1.  
    package com.loan.test.entity;
  2.  
     
  3.  
    import java.math.BigDecimal;
  4.  
     
  5.  
    public class Test {
  6.  
    private Integer id;
  7.  
     
  8.  
    private String userName;
  9.  
     
  10.  
    private Integer cardId;
  11.  
     
  12.  
    private BigDecimal moneyAmount;
  13.  
     
  14.  
    public Integer getId() {
  15.  
    return id;
  16.  
    }
  17.  
     
  18.  
    public void setId(Integer id) {
  19.  
    this.id = id;
  20.  
    }
  21.  
     
  22.  
    public String getUserName() {
  23.  
    return userName;
  24.  
    }
  25.  
     
  26.  
    public void setUserName(String userName) {
  27.  
    this.userName = userName == null ? null : userName.trim();
  28.  
    }
  29.  
     
  30.  
    public Integer getCardId() {
  31.  
    return cardId;
  32.  
    }
  33.  
     
  34.  
    public void setCardId(Integer cardId) {
  35.  
    this.cardId = cardId;
  36.  
    }
  37.  
     
  38.  
    public BigDecimal getMoneyAmount() {
  39.  
    return moneyAmount;
  40.  
    }
  41.  
     
  42.  
    public void setMoneyAmount(BigDecimal moneyAmount) {
  43.  
    this.moneyAmount = moneyAmount;
  44.  
    }
  45.  
    }

 

2)TestMapper.java

 

 

 

  1.  
    package com.loan.test.dao.mapper;
  2.  
     
  3.  
    import com.loan.test.entity.Test;
  4.  
     
  5.  
    public interface TestMapper {
  6.  
    int deleteByPrimaryKey(Integer id);
  7.  
     
  8.  
    int insert(Test record);
  9.  
     
  10.  
    int insertSelective(Test record);
  11.  
     
  12.  
    Test selectByPrimaryKey(Integer id);
  13.  
     
  14.  
    int updateByPrimaryKeySelective(Test record);
  15.  
     
  16.  
    int updateByPrimaryKey(Test record);
  17.  
    }

 

3)TestMapper.xml

 

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3.  
    <mapper namespace="com.loan.test.dao.mapper.TestMapper">
  4.  
    <resultMap id="BaseResultMap" type="com.loan.test.entity.Test">
  5.  
    <id column="id" jdbcType="INTEGER" property="id" />
  6.  
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
  7.  
    <result column="card_id" jdbcType="INTEGER" property="cardId" />
  8.  
    <result column="money_amount" jdbcType="DECIMAL" property="moneyAmount" />
  9.  
    </resultMap>
  10.  
    <sql id="Base_Column_List">
  11.  
    id, user_name, card_id, money_amount
  12.  
    </sql>
  13.  
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  14.  
    select
  15.  
    <include refid="Base_Column_List" />
  16.  
    from test
  17.  
    where id = #{id,jdbcType=INTEGER}
  18.  
    </select>
  19.  
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  20.  
    delete from test
  21.  
    where id = #{id,jdbcType=INTEGER}
  22.  
    </delete>
  23.  
    <insert id="insert" parameterType="com.loan.test.entity.Test">
  24.  
    insert into test (id, user_name, card_id,
  25.  
    money_amount)
  26.  
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{cardId,jdbcType=INTEGER},
  27.  
    #{moneyAmount,jdbcType=DECIMAL})
  28.  
    </insert>
  29.  
    <insert id="insertSelective" parameterType="com.loan.test.entity.Test">
  30.  
    insert into test
  31.  
    <trim prefix="(" suffix=")" suffixOverrides=",">
  32.  
    <if test="id != null">
  33.  
    id,
  34.  
    </if>
  35.  
    <if test="userName != null">
  36.  
    user_name,
  37.  
    </if>
  38.  
    <if test="cardId != null">
  39.  
    card_id,
  40.  
    </if>
  41.  
    <if test="moneyAmount != null">
  42.  
    money_amount,
  43.  
    </if>
  44.  
    </trim>
  45.  
    <trim prefix="values (" suffix=")" suffixOverrides=",">
  46.  
    <if test="id != null">
  47.  
    #{id,jdbcType=INTEGER},
  48.  
    </if>
  49.  
    <if test="userName != null">
  50.  
    #{userName,jdbcType=VARCHAR},
  51.  
    </if>
  52.  
    <if test="cardId != null">
  53.  
    #{cardId,jdbcType=INTEGER},
  54.  
    </if>
  55.  
    <if test="moneyAmount != null">
  56.  
    #{moneyAmount,jdbcType=DECIMAL},
  57.  
    </if>
  58.  
    </trim>
  59.  
    </insert>
  60.  
    <update id="updateByPrimaryKeySelective" parameterType="com.loan.test.entity.Test">
  61.  
    update test
  62.  
    <set>
  63.  
    <if test="userName != null">
  64.  
    user_name = #{userName,jdbcType=VARCHAR},
  65.  
    </if>
  66.  
    <if test="cardId != null">
  67.  
    card_id = #{cardId,jdbcType=INTEGER},
  68.  
    </if>
  69.  
    <if test="moneyAmount != null">
  70.  
    money_amount = #{moneyAmount,jdbcType=DECIMAL},
  71.  
    </if>
  72.  
    </set>
  73.  
    where id = #{id,jdbcType=INTEGER}
  74.  
    </update>
  75.  
    <update id="updateByPrimaryKey" parameterType="com.loan.test.entity.Test">
  76.  
    update test
  77.  
    set user_name = #{userName,jdbcType=VARCHAR},
  78.  
    card_id = #{cardId,jdbcType=INTEGER},
  79.  
    money_amount = #{moneyAmount,jdbcType=DECIMAL}
  80.  
    where id = #{id,jdbcType=INTEGER}
  81.  
    </update>
  82.  
    <resultMap id="BaseResultMap" type="com.loan.test.entity.Test">
  83.  
    <id column="id" jdbcType="INTEGER" property="id" />
  84.  
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
  85.  
    <result column="card_id" jdbcType="INTEGER" property="cardId" />
  86.  
    <result column="money_amount" jdbcType="DECIMAL" property="moneyAmount" />
  87.  
    </resultMap>
  88.  
    <sql id="Base_Column_List">
  89.  
    id, user_name, card_id, money_amount
  90.  
    </sql>
  91.  
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  92.  
    select
  93.  
    <include refid="Base_Column_List" />
  94.  
    from test
  95.  
    where id = #{id,jdbcType=INTEGER}
  96.  
    </select>
  97.  
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  98.  
    delete from test
  99.  
    where id = #{id,jdbcType=INTEGER}
  100.  
    </delete>
  101.  
    <insert id="insert" parameterType="com.loan.test.entity.Test">
  102.  
    insert into test (id, user_name, card_id,
  103.  
    money_amount)
  104.  
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{cardId,jdbcType=INTEGER},
  105.  
    #{moneyAmount,jdbcType=DECIMAL})
  106.  
    </insert>
  107.  
    <insert id="insertSelective" parameterType="com.loan.test.entity.Test">
  108.  
    insert into test
  109.  
    <trim prefix="(" suffix=")" suffixOverrides=",">
  110.  
    <if test="id != null">
  111.  
    id,
  112.  
    </if>
  113.  
    <if test="userName != null">
  114.  
    user_name,
  115.  
    </if>
  116.  
    <if test="cardId != null">
  117.  
    card_id,
  118.  
    </if>
  119.  
    <if test="moneyAmount != null">
  120.  
    money_amount,
  121.  
    </if>
  122.  
    </trim>
  123.  
    <trim prefix="values (" suffix=")" suffixOverrides=",">
  124.  
    <if test="id != null">
  125.  
    #{id,jdbcType=INTEGER},
  126.  
    </if>
  127.  
    <if test="userName != null">
  128.  
    #{userName,jdbcType=VARCHAR},
  129.  
    </if>
  130.  
    <if test="cardId != null">
  131.  
    #{cardId,jdbcType=INTEGER},
  132.  
    </if>
  133.  
    <if test="moneyAmount != null">
  134.  
    #{moneyAmount,jdbcType=DECIMAL},
  135.  
    </if>
  136.  
    </trim>
  137.  
    </insert>
  138.  
    <update id="updateByPrimaryKeySelective" parameterType="com.loan.test.entity.Test">
  139.  
    update test
  140.  
    <set>
  141.  
    <if test="userName != null">
  142.  
    user_name = #{userName,jdbcType=VARCHAR},
  143.  
    </if>
  144.  
    <if test="cardId != null">
  145.  
    card_id = #{cardId,jdbcType=INTEGER},
  146.  
    </if>
  147.  
    <if test="moneyAmount != null">
  148.  
    money_amount = #{moneyAmount,jdbcType=DECIMAL},
  149.  
    </if>
  150.  
    </set>
  151.  
    where id = #{id,jdbcType=INTEGER}
  152.  
    </update>
  153.  
    <update id="updateByPrimaryKey" parameterType="com.loan.test.entity.Test">
  154.  
    update test
  155.  
    set user_name = #{userName,jdbcType=VARCHAR},
  156.  
    card_id = #{cardId,jdbcType=INTEGER},
  157.  
    money_amount = #{moneyAmount,jdbcType=DECIMAL}
  158.  
    where id = #{id,jdbcType=INTEGER}
  159.  
    </update>
  160.  
    </mapper>


配置完成!!

posted @ 2021-01-06 14:34  歪歪//  阅读(178)  评论(0编辑  收藏  举报