Java自动生成oracle表实体和mybatis接口以及xml文件

为了提高开发效率,有一些工具和插件其实很好用,今天介绍怎么样自动生成我们数据库表的实体对象和mybatis接口以及xml文件。

先介绍一下使用方法,文章最后会附上我的实例项目,大家可直接下载!

1、首先主要的工具包就是mybatis-generator-core,我这里是mybatis-generator-core-1.3.2.jar包,大家需要其他版本的可以到网上下载
2、需要数据库jdbc连接的包,例如Oracle:ojdbc14-10.2.0.5.0.jar
3、在你的目录下新建一个src的文件夹
4、新建一个xml文件,自动生成的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:\work\generator\ojdbc14-10.2.0.5.0.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection
                driverClass="oracle.jdbc.driver.OracleDriver"
                connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
                userId="TEST" password="123">
        </jdbcConnection>

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


        <!--配置实体类存放路径,域模型层,生成的目标包 -->
        <javaModelGenerator targetPackage="com.entity"
                            targetProject="D:\work\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--XML映射文件,生成的位置(目标包),源代码文件夹 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="D:\work\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--XML对应的Mapper类 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.dao" targetProject="D:\work\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <!--下面是数据库表名和项目中需要生成类的名称,建议和数据库保持一致,如果有多个表,添加多个节点即可 -->
        <table tableName="A_B_C_D" domainObjectName="Abcd"
               enableCountByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false">
        </table>

    </context>
</generatorConfiguration>

在这里插入图片描述
在这里插入图片描述

配置好后在该路径下cmd:
执行代码介绍:java -jar mybatis-generator-core包 -configfile xml文件名 -overwrite
执行:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorOracle.xml -overwrite

这样就在你src目录下面自动生成好文件了!

1、生成的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.dao.AbcdMapper" >
  <resultMap id="BaseResultMap" type="com.entity.Abcd" >
    <id column="A1" property="a1" jdbcType="VARCHAR" />
    <result column="B2" property="b2" jdbcType="VARCHAR" />
    <result column="C3" property="c3" jdbcType="VARCHAR" />
    <result column="D4" property="d4" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    A1, B2, C3, D4
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from A_B_C_D
    where A1 = #{a1,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from A_B_C_D
    where A1 = #{a1,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.entity.Abcd" >
    insert into A_B_C_D (A1, B2, C3, D4
      )
    values (#{a1,jdbcType=VARCHAR}, #{b2,jdbcType=VARCHAR}, #{c3,jdbcType=VARCHAR}, #{d4,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.entity.Abcd" >
    insert into A_B_C_D
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="a1 != null" >
        A1,
      </if>
      <if test="b2 != null" >
        B2,
      </if>
      <if test="c3 != null" >
        C3,
      </if>
      <if test="d4 != null" >
        D4,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="a1 != null" >
        #{a1,jdbcType=VARCHAR},
      </if>
      <if test="b2 != null" >
        #{b2,jdbcType=VARCHAR},
      </if>
      <if test="c3 != null" >
        #{c3,jdbcType=VARCHAR},
      </if>
      <if test="d4 != null" >
        #{d4,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.entity.Abcd" >
    update A_B_C_D
    <set >
      <if test="b2 != null" >
        B2 = #{b2,jdbcType=VARCHAR},
      </if>
      <if test="c3 != null" >
        C3 = #{c3,jdbcType=VARCHAR},
      </if>
      <if test="d4 != null" >
        D4 = #{d4,jdbcType=VARCHAR},
      </if>
    </set>
    where A1 = #{a1,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.entity.Abcd" >
    update A_B_C_D
    set B2 = #{b2,jdbcType=VARCHAR},
      C3 = #{c3,jdbcType=VARCHAR},
      D4 = #{d4,jdbcType=VARCHAR}
    where A1 = #{a1,jdbcType=VARCHAR}
  </update>
</mapper>

2、接口类

package com.dao;

import com.entity.Abcd;

public interface AbcdMapper {
    int deleteByPrimaryKey(String a1);

    int insert(Abcd record);

    int insertSelective(Abcd record);

    Abcd selectByPrimaryKey(String a1);

    int updateByPrimaryKeySelective(Abcd record);

    int updateByPrimaryKey(Abcd record);
}

3、实体类

package com.entity;

public class Abcd {
    private String a1;

    private String b2;

    private String c3;

    private String d4;

    public String getA1() {
        return a1;
    }

    public void setA1(String a1) {
        this.a1 = a1 == null ? null : a1.trim();
    }

    public String getB2() {
        return b2;
    }

    public void setB2(String b2) {
        this.b2 = b2 == null ? null : b2.trim();
    }

    public String getC3() {
        return c3;
    }

    public void setC3(String c3) {
        this.c3 = c3 == null ? null : c3.trim();
    }

    public String getD4() {
        return d4;
    }

    public void setD4(String d4) {
        this.d4 = d4 == null ? null : d4.trim();
    }
}

简单粗暴,你学废了吗?

Mysql数据库请参考另一篇文章:
https://blog.csdn.net/qq_37928228/article/details/110704104

实例项目文件下载:
https://download.csdn.net/download/qq_37928228/13455402
或者
https://download.csdn.net/download/qq_37928228/13455392

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值