eclipse中利用Maven逆向工程生成PO类以及mapper(mybatis)
在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类以及mapper(mybatis)的更多相关文章
- 在Eclipse中利用maven整合搭建ssm框架
首先说明用到的框架: spring + springMVC + mybatis 构建工具:maven 开发工具:eclipse 开发环境:win10 java版本:jdk1.8 ...
- 利用Eclipse中的Maven构建Web项目(三)
利用Eclipse中的Maven构建Web项目 1.将Maven Project转换成动态Web项目,鼠标右键项目,输入"Project Facets" 2.依据Dynamic W ...
- 利用Eclipse中的Maven构建Web项目报错(二)
利用Eclipse中的Maven构建Web项目 1.错误描述 [INFO] Scanning for projects... [INFO] [INFO] Using the builder org.a ...
- 利用Eclipse中的Maven构建Web项目报错(一)
利用Eclipse中的Maven构建Web项目 1.在进行上述操作时,pom.xml一直报错 <project xmlns="http://maven.apache.org/POM/4 ...
- 利用Eclipse中的Maven构建Web项目(二)
利用Eclipse中的Maven构建Web项目 1.新建源文件夹,Java Resources鼠标右键,"New-->Source Folder" 2.新建src/main/ ...
- 利用Eclipse中的Maven构建Web项目(一)
利用Eclipse中的Maven构建Web项目 1.新建一个Maven Project,"New-->Other..." 2.选择"Maven Project&qu ...
- Eclipse中发布Maven管理的Web项目时找不到类的问题根源和解决办法(转)
转自:http://blog.csdn.net/lvguanming/article/details/37812579?locationNum=12 写在前面的话 现在是越来越太原讨厌Eclipse这 ...
- Maven学习(一) -- 安装Maven及Eclipse中配置Maven
标签(空格分隔): 学习笔记 本文环境:Windows7, JDK1.7.0_76 安装及配置Maven环境变量 需要电脑中已经有Java环境 在控制台中输入:echo %JAVA_HOME%看是否能 ...
- Eclipse 中构建 Maven 项目的完整过程 - SpringBoot 项目
进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) 一.Maven项目的新建 1.鼠标右键---->New--- ...
- Mybatis逆向工程生成po、mapper接口、mapper.xml
Mybatis逆向工程生成po.mapper接口.mapper.xml 一.新建一个maven工程 请查看我的另一篇博客:<使用idea创建一个maven工程> 二.引入所需依赖 需要my ...
随机推荐
- 关于.NET CORE 的 swagger 分组简单使用
在避免swagger接口过多时,可以使用分组进行功能区分 简单三部曲 1.添加分组和级别名称 services.AddSwaggerGen(c => { #region swagger分组 c. ...
- PY3多继承
__author__ = "Alex Li"class A: def __init__(self): print("A")class B(A): pass #d ...
- redis的linux下安装
Linux 源码安装 下载地址:http://redis.io/download,下载最新稳定版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: # wget http://downloa ...
- mybatis判断是否为空或null
<if test="catagoryCode != null and catagoryCode != ''"> AND train.CATAGORY_CODE = #{ ...
- 使用vue渲染大量数据时应该怎么优化?
Object.freeze 适合一些 big data的业务场景.尤其是做管理后台的时候,经常会有一些超大数据量的 table,或者一个含有 n 多数据的图表,这种数据量很大的东西使用起来最明显的感受 ...
- Linux CentOS下搭建golang 1.17 开发环境
1. 下载软件包并安装 cd ~ wget https://storage.googleapis.com/golang/go1.17.2.linux-amd64.tar.gz tar zxvf go1 ...
- GNOME 窗口添加最大化、最小化按钮
1.安装工具 使用终端命令安装优化工具 yum install gnome-tweak-tool 2.配置 gnome-tweak-tool 安装完毕后,在应用程序的"工具"中找到 ...
- Win10下VM虚拟机桥接模式无法上网的相关问题?
首先:在LR中点击虚拟机->设置,勾选桥接模式,勾选之后无法上网 1. 选中虚拟网卡VMnet8(NAT模式)和WLAN(本机)网卡,右键选择"桥接",然后系统创建一个 ...
- Linux&Android相关常用命令汇总记录
Linux&Android相关常用命令汇总记录 0@Linux&Android系统命令行下如何查看命令的帮助信息: command --help 1@在Linux系统中,设备分为三类, ...
- 推测执行 Speculative execution
如果我们只是靠随便网络上查找一个Speculative这个词的含义,是很难去理解的.但是我们通过查看英文原文去理解,可能就比较清楚地理解了: speculative (adjective) 1. ba ...