1. 程式人生 > >eclipse中利用Maven逆向工程生成PO類以及mapper(mybatis)

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

1、在pom.xml的project>build裡面新增如下程式碼,讓maven環境支援mybatis-generator元件

<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>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.19.1</version>
					<configuration>
						<skipTests>true</skipTests>
					</configuration>
				</plugin>

				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId>
					<version>3.0.1</version>
					<configuration>
						<encoding>UTF-8</encoding>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>

注:如果在dependencies中已經引入mysql-connector-java則不需加入以下dependency,反之加入
<dependency>  
                        <groupId>mysql</groupId>  
                        <artifactId>mysql-connector-java</artifactId>  
                        <version>5.1.35</version>  
                        <scope>runtime</scope>  
                    </dependency>

2、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="E:\apache-maven-3.3.9\repo\mysql\mysql-connector-java\5.1.18\mysql-connector-java-5.1.18.jar" />  
	<context id="Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 資料庫連結URL、使用者名稱、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://192.168.100.52:3306/dev_test" userId="資料庫使用者名稱"
			password="資料庫密碼">
			<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" 
				userId="msa" password="msa"> -->
		</jdbcConnection>
		<javaTypeResolver>
			<!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer, 為 true時把JDBC DECIMAL和NUMERIC型別解析為java.math.BigDecimal -->
			<property name="forceBigDecimals" value="true" />
		</javaTypeResolver>
		<!-- 生成實體類的包名和位置,這裡配置將生成的實體類放在com.loan.test.entity這個包下 -->
		<javaModelGenerator targetPackage="com.loan.test.entity"
			targetProject=".\src\main\java\">
			<!-- enableSubPackages:是否讓schema作為包的字尾 -->
			<property name="enableSubPackages" value="true" />
			<!-- 從資料庫返回的值被清理前後的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的SQL對映檔案包名和位置,這裡配置將生成的SQL對映檔案放在com.loan.test.dao.xml這個包下 -->
		<sqlMapGenerator targetPackage="com.loan.test.dao.xml"
			targetProject=".\src\main\java\">
			<!-- enableSubPackages:是否讓schema作為包的字尾 -->
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置,這裡配置將生成的dao類放在com.loan.test.dao.mapper這個包下 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.loan.test.dao.mapper" targetProject=".\src\main\java\">
			<!-- enableSubPackages:是否讓schema作為包的字尾 -->
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<table tableName="test" domainObjectName="Test"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false" />
	</context>
</generatorConfiguration>

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

專案右鍵->run as->maven build...,Goals:中輸入mybatis-generator:generate

.

4、重新整理專案,結果


1)Test.java

package com.loan.test.entity;

import java.math.BigDecimal;

public class Test {
    private Integer id;

    private String userName;

    private Integer cardId;

    private BigDecimal moneyAmount;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public Integer getCardId() {
        return cardId;
    }

    public void setCardId(Integer cardId) {
        this.cardId = cardId;
    }

    public BigDecimal getMoneyAmount() {
        return moneyAmount;
    }

    public void setMoneyAmount(BigDecimal moneyAmount) {
        this.moneyAmount = moneyAmount;
    }
}

2)TestMapper.java


package com.loan.test.dao.mapper;

import com.loan.test.entity.Test;

public interface TestMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Test record);

    int insertSelective(Test record);

    Test selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Test record);

    int updateByPrimaryKey(Test record);
}

3)TestMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.loan.test.dao.mapper.TestMapper">
  <resultMap id="BaseResultMap" type="com.loan.test.entity.Test">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="card_id" jdbcType="INTEGER" property="cardId" />
    <result column="money_amount" jdbcType="DECIMAL" property="moneyAmount" />
  </resultMap>
  <sql id="Base_Column_List">
    id, user_name, card_id, money_amount
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from test
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from test
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.loan.test.entity.Test">
    insert into test (id, user_name, card_id, 
      money_amount)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{cardId,jdbcType=INTEGER}, 
      #{moneyAmount,jdbcType=DECIMAL})
  </insert>
  <insert id="insertSelective" parameterType="com.loan.test.entity.Test">
    insert into test
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="userName != null">
        user_name,
      </if>
      <if test="cardId != null">
        card_id,
      </if>
      <if test="moneyAmount != null">
        money_amount,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="cardId != null">
        #{cardId,jdbcType=INTEGER},
      </if>
      <if test="moneyAmount != null">
        #{moneyAmount,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.loan.test.entity.Test">
    update test
    <set>
      <if test="userName != null">
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="cardId != null">
        card_id = #{cardId,jdbcType=INTEGER},
      </if>
      <if test="moneyAmount != null">
        money_amount = #{moneyAmount,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.loan.test.entity.Test">
    update test
    set user_name = #{userName,jdbcType=VARCHAR},
      card_id = #{cardId,jdbcType=INTEGER},
      money_amount = #{moneyAmount,jdbcType=DECIMAL}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="com.loan.test.entity.Test">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="card_id" jdbcType="INTEGER" property="cardId" />
    <result column="money_amount" jdbcType="DECIMAL" property="moneyAmount" />
  </resultMap>
  <sql id="Base_Column_List">
    id, user_name, card_id, money_amount
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from test
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from test
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.loan.test.entity.Test">
    insert into test (id, user_name, card_id, 
      money_amount)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{cardId,jdbcType=INTEGER}, 
      #{moneyAmount,jdbcType=DECIMAL})
  </insert>
  <insert id="insertSelective" parameterType="com.loan.test.entity.Test">
    insert into test
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="userName != null">
        user_name,
      </if>
      <if test="cardId != null">
        card_id,
      </if>
      <if test="moneyAmount != null">
        money_amount,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="cardId != null">
        #{cardId,jdbcType=INTEGER},
      </if>
      <if test="moneyAmount != null">
        #{moneyAmount,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.loan.test.entity.Test">
    update test
    <set>
      <if test="userName != null">
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="cardId != null">
        card_id = #{cardId,jdbcType=INTEGER},
      </if>
      <if test="moneyAmount != null">
        money_amount = #{moneyAmount,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.loan.test.entity.Test">
    update test
    set user_name = #{userName,jdbcType=VARCHAR},
      card_id = #{cardId,jdbcType=INTEGER},
      money_amount = #{moneyAmount,jdbcType=DECIMAL}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

配置完成!!

相關推薦

eclipse利用Maven逆向工程生成PO以及mappermybatis

1、在pom.xml的project>build裡面新增如下程式碼,讓maven環境支援mybatis-generator元件 <pluginManagement> <plugins> <plugin> &l

mybatis逆向工程生成pojomapper介面和對映檔案

1.下載mybatis-generator-core-1.3.2-bundle.zip,解壓為mybatis-generator-core-1.3.2-bundle。 2.建立Java工程,匯入mybatis.jar,mybatis-generator.jar,

Mybatis 在 IDEA 使用 generator 逆向工程生成 pojo,mapper

使用mybatis可以逆向生成pojo和mapper檔案有很多種方式,我以前用的是mybtais自帶的generator包來生成,連線如下:mybatis自己生成pojo 今天我用了IDEA上使用maven專案來生成pojo和mapper,具體步驟如下 1,先配置pom.xml檔案,先配置外

利用mybatis框架逆向工程生成實體dao和mapper

1、引入maven依賴 <build> <finalName>demo6</finalName> <plugins> <plugin> &l

mybatis 逆向工程生成實體

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.

搭建springBoot環境以及解決Eclipse建立maven專案時專案報錯以及資料夾resource報錯問題

maven專案時專案報錯以及資料夾resource報錯問題 因為這段時間公司再用springBoot,於是我就學習了springBoot,在這過程中遇到了很多問題,最後終於都解決了,現在為自己的學習做一下總結,同時簡單的搭建一個maven環境下的springBoot專案,供類似於我這種新

MyBatis+逆向工程生成實體Pojo和Mapper對映檔案 簡單+實用。

       Mybatis 作為一個半自動的ORM框架,相比hibernate而言,靈活了許多,優化sql語句的時候自己可以直接在xml檔案中自己優化或者編寫,但是有很多簡單的增刪改查的程式碼需要自己書寫,這時候自己可以可以用mybatis官方提供的逆向工程幫自己生成大部分

MyEclipse2014使用Hibernate逆向工程生成實體

1. 為對應的工程專案新增Hibernate支援 到這裡我們就成功為自己的專案添加了Hibernate支援。我們可以清楚地看到專案的圖示有些許的改變,並且對應的配置檔案和SessionFactory類都自動生成好了。

逆向工程核心原理》學習總結

介紹 PE檔案是windows作業系統的可執行檔案格式(包括.exe、.scr、.dll、.sys、.obj等檔案),PE檔案指32位的可執行檔案,也稱為PE32。64位可執行檔案稱為PE+或PE32+,是PE32檔案的一種擴充套件形式。 基本結構 P

C語言存儲類別又分為四:自動auto、靜態static、寄存器的register和外部的extern

字符變量 修飾 例如 register ext 進行 適合 sta -- 除法運算中註意: 如果相除的兩個數都是整數的話,則結果也為整數,小數部分省略,如8/3 = 2;而兩數中有一個為小數,結果則為小數,如:9.0/2 = 4.500000。 取余運算中註意: 該運算只適

在IDEA使用MyBatis Generator逆向工程生成代碼

classpath 生成器 targe base time jar包 選項 操作 ava 本文介紹一下用Maven工具如何生成Mybatis的代碼及映射的文件。 一、配置Maven pom.xml 文件 在pom.xml增加以下插件: <build>

在IDEA使用MyBatis Generator逆向工程生成程式碼

一、配置Maven pom.xml 檔案 在pom.xml增加以下外掛: <build> <finalName>aaa</finalName> <plugins>

eclipse建立Maven版的Web工程出現的問題

1.建立的Mave版的Web工程的目錄結構,就只有webapp,而沒有WEB-INF、MATA-INF檔案。 解決方法:Web工程右擊--->Propeties-->Project Facets--->Dymatic Web Module--->

Mybatis 逆向工程生成mapper.xml 對映檔案方法的使用

方法1:selectByExample(TbItemDescExample  example)                     返回值:List<TbItemDesc>             作用:通過特定限制條件查詢資訊,example用於生成

eclipse使用maven生成jar包並執行到centos7

java -Dfile.encoding=utf-8 -jar D:\***.jar 詳細: 執行 nohup java -jar memory-0.0.1-SNAPSHOT.jar >/usr/local/jar/memory.out 2>

Eclipse建立Maven多模組工程

在平時的Javaweb專案開發中為了便於後期的維護,我們一般會進行分層開發,最常見的就是分為domain(域模型層)、dao(資料庫訪問層)、service(業務邏輯層)、web(表現層),這樣分層之後,各個層之間的職責會比較明確,後期維護起來也相對比較容易,今天我們就是使

Eclipse建立Maven多模組工程的例子

如果,你需要建立多個專案,專案之間即獨立又有關係,那麼建立一個Maven多模組專案是個非常好的選擇,也非常cool!怎麼在Eclipse裡面建立多模組工程,以及需要注意哪些地方,我在這裡做個簡單的介紹。 一、準備 若想在Eclipse裡面做這些的話,那麼在做這一切前,請確認

在IDEA使用MyBatis Generator逆向工程生成程式碼 實踐

第一步:在pom檔案下加入 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId

利用EclipseMaven構建Web專案報錯

利用Eclipse中的Maven構建Web專案 1、錯誤描述 [INFO] Scanning for projects... [INFO] [INFO] Using the builder org.apache.maven.lifecycle.internal.bui

eclipse 逆向工程生成hibernate實體(註解或配置檔案)

1.安裝hibernate tools 在 help --> Eclipse Marketplace 中搜索 hibernate, 找到 jboss tools , 點選 install, 選擇 hibernate tools , 其他的不裝, 按照步驟安裝即可 2