mybatis辅助神器--自动生成model,dao,mapper

前言

今天分享一个用于mybatis生成代码的神器,经过简单的配置我们可以自动生成数据库里相应的表的model,dao,mapper文件,先上图看下效果

效果
  1. 假如我们数据库有这样一张class

    image

  2. 通过简单配置我们就可以自动生成相应的代码文件:

    • 实体类:

      public class Cla {
          private Integer cid;
      
          private String cname;
      
          private String clocation;
      
          public Integer getCid() {
              return cid;
          }
      
          public void setCid(Integer cid) {
              this.cid = cid;
          }
      
          public String getCname() {
              return cname;
          }
      
          public void setCname(String cname) {
              this.cname = cname == null ? null : cname.trim();
          }
      
          public String getClocation() {
              return clocation;
          }
      
          public void setClocation(String clocation) {
              this.clocation = clocation == null ? null : clocation.trim();
          }
      }
      
    • 接口:

      public interface ClaMapper {
          int deleteByPrimaryKey(Integer cid);
      
          int insert(Cla record);
      
          int insertSelective(Cla record);
      
          Cla selectByPrimaryKey(Integer cid);
      
          int updateByPrimaryKeySelective(Cla record);
      
          int updateByPrimaryKey(Cla record);
      }
      
    • mapper.xml:

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "./mybatis-3-mapper.dtd" >
      <mapper namespace="com.lys.dao.ClaMapper" >
        <resultMap id="BaseResultMap" type="com.lys.model.Cla" >
          <id column="cId" property="cid" jdbcType="INTEGER" />
          <result column="cName" property="cname" jdbcType="VARCHAR" />
          <result column="cLocation" property="clocation" jdbcType="VARCHAR" />
        </resultMap>
        <sql id="Base_Column_List" >
          cId, cName, cLocation
        </sql>
        <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
          select 
          <include refid="Base_Column_List" />
          from class
          where cId = #{cid,jdbcType=INTEGER}
        </select>
        <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
          delete from class
          where cId = #{cid,jdbcType=INTEGER}
        </delete>
        <insert id="insert" parameterType="com.lys.model.Cla" >
          insert into class (cId, cName, cLocation
            )
          values (#{cid,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{clocation,jdbcType=VARCHAR}
            )
        </insert>
        <insert id="insertSelective" parameterType="com.lys.model.Cla" >
          insert into class
          <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="cid != null" >
              cId,
            </if>
            <if test="cname != null" >
              cName,
            </if>
            <if test="clocation != null" >
              cLocation,
            </if>
          </trim>
          <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="cid != null" >
              #{cid,jdbcType=INTEGER},
            </if>
            <if test="cname != null" >
              #{cname,jdbcType=VARCHAR},
            </if>
            <if test="clocation != null" >
              #{clocation,jdbcType=VARCHAR},
            </if>
          </trim>
        </insert>
        <update id="updateByPrimaryKeySelective" parameterType="com.lys.model.Cla" >
          update class
          <set >
            <if test="cname != null" >
              cName = #{cname,jdbcType=VARCHAR},
            </if>
            <if test="clocation != null" >
              cLocation = #{clocation,jdbcType=VARCHAR},
            </if>
          </set>
          where cId = #{cid,jdbcType=INTEGER}
        </update>
        <update id="updateByPrimaryKey" parameterType="com.lys.model.Cla" >
          update class
          set cName = #{cname,jdbcType=VARCHAR},
            cLocation = #{clocation,jdbcType=VARCHAR}
          where cId = #{cid,jdbcType=INTEGER}
        </update>
      </mapper>
      
做法

12

首先我们要准备这样一个工具:链接:https://pan.baidu.com/s/1kUGwQUtIHYCFImArBPF32A
提取码:1314

下载解压后是这样的:

image

进入lib文件:(注意:根据自己电脑上安装MySQL的版本,里面的驱动包可以自己选择性替换,如果驱动包的版本和MySQL的版本不对应,会出错的)

image

打开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="mysql-connector-java-8.0.17.jar"/>
	<context id="mysqltables" targetRuntime="MyBatis3" defaultModelType="conditional">
		<!-- 注释生成-->
		<commentGenerator>
			<property name="suppressDate" value="true"/>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		
		<!--数据库链接URL,用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/school?useSSL=FALSE&amp;serverTimezone=Asia/Shanghai" userId="root" password="root">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
        
        <!--自动生成的文件的位置可以根据实际需要进行更改调整-->
		<!-- 生成模型的包名和位置-->
		<javaModelGenerator targetPackage="com.lys.model" targetProject="src">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		<!-- 生成映射文件的包名和位置-->
		<sqlMapGenerator targetPackage="com.lys.mapper" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置-->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.lys.dao" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		<!-- 要生成哪些表?  配置相应的表名和实体类的名-->
		<table tableName="class" domainObjectName="Cla" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
			</context>
</generatorConfiguration>
<!-- java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite -->

配置好保存上面的文件,然后打开cmd,进入lib目录下:

image

然后输入指令:
image
看到成功的提示,就OK了

然后进入当前的src目录下,就能看到自动生成的代码文件了

image

总结

通过上面工具生成部分代码,在需要的地方直接粘贴过去就行,一定程度上可以减少些重复性工作,当然,工具不唯一,还有其他工具,感兴趣的小伙伴可以去尝试一下哦

0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员-小李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值