实体类?Dao接口?Mapper映射文件?都别写了!!!用这种方法就可以

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 实体类?Dao接口?Mapper映射文件?都别写了!!!用这种方法就可以


每次我们在写代码的时候,都会对应数据表来写实体类,以及接口中常用的增删改查方法,包括Mapper映射文件,一个两个表还可以,如果数据表多的情况下,可见工作量也会很大,对此有没有一种很好的解决办法呢?答案当然是有的,下面我们就来看看如何让程序自动生成这些代码呢?

01

下载所需的工具

需要下载“mybatis反向生成工具”(可在本公众号【雄雄的小课堂】后台回复:mybatis反向生成工具,即可免费下载)。

02

修改配置文件generatorConfig.xml

打开下载的工具,进入lib文件夹,右击用记事本(其他编辑器也可以)打开generatorConfig.xml文件,如下所示:

数据库表设计如下:

所在数据库为:schooldb

1.加载所对应数据库的jar,我在这里用的是mysql数据库,所以就得需要mysql的驱动jar文件(jar文件已经放至lib目录中,oracle的也有),将名字写在下面的地方。

<classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">

mysql-connector-java-5.1.25-bin.jar就是数据库的jar包。

2.连接配置(此步骤的作用就是连接数据库),将对应的driverClass,connectionURL,userId和password写上,如下是我的,你就把用户名和密码改成你的就行。

<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/schooldb" userId="root" password="root"> 
        </jdbcConnection>

3.配置生成实体类、Dao接口以及映射文件的路径,我在这放在了org.dao里面了,最后在放在src下面,如下代码:

<javaModelGenerator targetPackage="org.entity" targetProject="src">
      <!-- 是否在当前路径下新加一层schema,eg:fase路径com.shkj.pojo, true:com.shkj.pojo.[schemaName] -->
            <property name="enableSubPackages" value="true"/>
      <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
           <!-- <property name="trimStrings" value="false"/> -->
        </javaModelGenerator>  
        <!-- 生成xml文件的路径,这个路径可以自动生成,但是必须有src这个路径-->
        <sqlMapGenerator targetPackage="org.dao" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成Dao类的路径,这个路径可以自动生成,但是必须有src这个路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="org.dao" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>

4.根据表生成对应的类名:

<table tableName="BookManage" domainObjectName="BookManage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

注意:这是一个表,如果有多个表的话,需要写多个<table>标签。

03

开始生成

打开文件中的cmd.bat批处理,双击进入之后自己会定位当前所在的目录,然后输入:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite  回车即可。

如下所示:

最后打开文件夹中的src目录,就会发现想要的都在里面。

实体类:

package org.entity;
import java.util.Date;
public class BookManage {
    private Integer bid;
    private String bname;
    private String bauthor;
    private Date btime;
    private Integer btype;
    public Integer getBid() {
        return bid;
    }
    public void setBid(Integer bid) {
        this.bid = bid;
    }
    public String getBname() {
        return bname;
    }
    public void setBname(String bname) {
        this.bname = bname;
    }
    public String getBauthor() {
        return bauthor;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    public Date getBtime() {
        return btime;
    }
    public void setBtime(Date btime) {
        this.btime = btime;
    }
    public Integer getBtype() {
        return btype;
    }
    public void setBtype(Integer btype) {
        this.btype = btype;
    }
}

Dao接口:

package org.dao;
import org.entity.BookManage;
public interface BookManageMapper {
    int deleteByPrimaryKey(Integer bid);
    int insert(BookManage record);
    int insertSelective(BookManage record);
    BookManage selectByPrimaryKey(Integer bid);
    int updateByPrimaryKeySelective(BookManage record);
    int updateByPrimaryKey(BookManage record);
}

Dao接口所对应的Mapper文件:

<?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="org.dao.BookManageMapper" >
  <resultMap id="BaseResultMap" type="org.entity.BookManage" >
    <id column="bid" property="bid" jdbcType="INTEGER" />
    <result column="bname" property="bname" jdbcType="VARCHAR" />
    <result column="bauthor" property="bauthor" jdbcType="VARCHAR" />
    <result column="btime" property="btime" jdbcType="TIMESTAMP" />
    <result column="btype" property="btype" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    bid, bname, bauthor, btime, btype
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from bookmanage
    where bid = #{bid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from bookmanage
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="org.entity.BookManage" >
    insert into bookmanage (bid, bname, bauthor, 
      btime, btype)
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{bauthor,jdbcType=VARCHAR}, 
      #{btime,jdbcType=TIMESTAMP}, #{btype,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="org.entity.BookManage" >
    insert into bookmanage
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="bid != null" >
        bid,
      </if>
      <if test="bname != null" >
        bname,
      </if>
      <if test="bauthor != null" >
        bauthor,
      </if>
      <if test="btime != null" >
        btime,
      </if>
      <if test="btype != null" >
        btype,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="bid != null" >
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null" >
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="bauthor != null" >
        #{bauthor,jdbcType=VARCHAR},
      </if>
      <if test="btime != null" >
        #{btime,jdbcType=TIMESTAMP},
      </if>
      <if test="btype != null" >
        #{btype,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="org.entity.BookManage" >
    update bookmanage
    <set >
      <if test="bname != null" >
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="bauthor != null" >
        bauthor = #{bauthor,jdbcType=VARCHAR},
      </if>
      <if test="btime != null" >
        btime = #{btime,jdbcType=TIMESTAMP},
      </if>
      <if test="btype != null" >
        btype = #{btype,jdbcType=INTEGER},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="org.entity.BookManage" >
    update bookmanage
    set bname = #{bname,jdbcType=VARCHAR},
      bauthor = #{bauthor,jdbcType=VARCHAR},
      btime = #{btime,jdbcType=TIMESTAMP},
      btype = #{btype,jdbcType=INTEGER}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
</mapper>


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
XML Oracle Java
mybatis反向生成实体类、dao层以及映射文件
mybatis反向生成实体类、dao层以及映射文件
14 1
|
3月前
|
Java 数据库连接 Maven
使用mybatis插件generator生成实体类,dao层和mapper映射
使用mybatis插件generator生成实体类,dao层和mapper映射
55 0
|
8月前
|
SQL XML Java
Mybatis的mapper接口实现原理
Mybatis的mapper接口实现原理
132 0
|
8天前
|
Java 数据库连接 mybatis
MyBatis中Mapper接口和dao区别是什么?
MyBatis中Mapper接口和dao区别是什么?
|
9月前
|
SQL 缓存 Java
MyBatis核心 - SqlSession如何通过Mapper接口生成Mapper对象
从 SqlSessionFactoryBuilder - SqlSessionFactory - SqlSession - Mapeper实例对象 的过程
98 0
|
5月前
|
Java 数据库连接 mybatis
|
10月前
|
SQL Java 数据库连接
使用 MyBatis 的映射文件调用 mapper 接口时有哪些要求?
使用 MyBatis 的映射文件调用 mapper 接口时有哪些要求?
59 0
|
11月前
|
Java 数据库连接 mybatis
MyBatis实现基于Mapper接口代理Dao的CURD
MyBatis实现基于Mapper接口代理Dao的CURD
|
SQL XML 安全
Mapper接口和XML配置|学习笔记
快速学习Mapper接口和XML配置
139 0