• 在IDEA创建文件模板——以创建MyBatis的mapper.xml文件模板为例


    1、本篇博客解决的问题

    我们在编码的过程中,经常会需要写一些配置文件。这些配置文件往往都有固定的格式或者说固定必须有的内容。例如XML文件中需要有的文件头,用来控制这个XML文件中能够写的标签和标签之间的嵌套先后关系。

    我们经常写,当我们需要创建一个这样的文件的时候,可以直接将别的类似的文件复制粘贴过来,然后改吧改吧就可以用了。或者到官网或者其他的网站搜索一下,然后复制过来。

    以上的两种方法都是可以使用的,这里再介绍一种解决方法。如果我们使用IDEA编码,在创建文件的时候,可以直接将这个文件的模板创建出来不就可以了吗?这样我们也不需要复制粘贴了,直接在模板上面修改填充就可以了。

    那么我们怎么在IDEA中创建这个模板呢?

    2、在IDEA中创建文件模板的步骤

    我本人使用的IDEA版本是2021.1.2。不同的版本之间,在操作上可能会有点差别。

    操作步骤如下所示:

    1、点击IDEA上方导航栏的File,然后在下拉菜单中点击Settings  进入到如下界面中。在如下界面中选择Editor,再选择File and Code Templates(文件和代码模板),然后点击  File 。(因为我们要添加的是一个文件模板)。然后再点击File下面的    +  

     2、点击  +  以后进入到如下界面

     然后给这个模板起一个名字,也就是上图中的Name文本框,例如可以是 MyBatis-mapper。然后是Extension文本框,需要我们填写这个文件的扩展名,例如是xml。

    3、以上完成以后,可以将我们这个模板的内容写到下方的空白处即可。最后点击右下角的Apply(应用)和OK

    3、MyBatis中的mapper.xml文件模板

    这里我就直接粘贴代码了,读者可以将下面的代码复制粘贴到上图中的空白文本框处形成模板内容。

    1. mapper
    2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.ruoyi.project.exceptSolve.medicate.mapper.MedicateExceptSolveMapper">
    5. <resultMap id="MedicateExceptResult" type="MedicateExcept">
    6. <result property="time" column="time">result>
    7. <result property="medicateLevel" column="medicate_level" />
    8. <result property="flowmeter" column="flowmeter" />
    9. <result property="reactionLevel" column="reaction_level" />
    10. <result property="reactionTurbidity" column="reaction_turbidity" />
    11. resultMap>
    12. <select id="selectMedicateExceptSolveList" parameterType="MedicateExceptSolve" resultMap="MedicateExceptSolveResult">
    13. select * from medicate_except_solve
    14. <where>
    15. <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and time between #{params.beginTime} and #{params.endTime}if>
    16. where>
    17. select>
    18. <delete id="deleteMedicateExceptSolve">
    19. delete from medicate_except_solve
    20. delete>
    21. <select id="selectMedicateExcept" resultMap="MedicateExceptResult">
    22. select * from medicate_except
    23. select>
    24. <insert id="insertMedicateExceptSolve" parameterType="MedicateExceptSolve">
    25. insert into medicate_except_solve values (#{time},#{solve})
    26. insert>
    27. <select id="selectMethodByFaultId" parameterType="Long" resultType="java.lang.String">
    28. select method from method where fault_id=#{faultId}
    29. select>
    30. <select id="selectMinStandardid1" resultType="Double">
    31. select min from standard where standard_id=1
    32. select>
    33. <select id="selectStudentById" resultType="com.dcy.domain.Student">
    34. select id,name,email,age from student where id=#{studentId}
    35. select>
    36. <insert id="insertStudent">
    37. insert into student values (#{id},#{name},#{email},#{age})
    38. insert>
    39. <update id="updateUser" parameterType="User">
    40. update sys_user
    41. <set>
    42. <if test="deptId != null and deptId != 0">dept_id = #{deptId},if>
    43. <if test="loginName != null and loginName != ''">login_name = #{loginName},if>
    44. <if test="userName != null and userName != ''">user_name = #{userName},if>
    45. <if test="userType != null and userType != ''">user_type = #{userType},if>
    46. <if test="email != null and email != ''">email = #{email},if>
    47. <if test="phonenumber != null and phonenumber != ''">phonenumber = #{phonenumber},if>
    48. <if test="sex != null and sex != ''">sex = #{sex},if>
    49. <if test="avatar != null and avatar != ''">avatar = #{avatar},if>
    50. <if test="password != null and password != ''">password = #{password},if>
    51. <if test="salt != null and salt != ''">salt = #{salt},if>
    52. <if test="status != null and status != ''">status = #{status},if>
    53. <if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},if>
    54. <if test="loginDate != null">login_date = #{loginDate},if>
    55. <if test="pwdUpdateDate != null">pwd_update_date = #{pwdUpdateDate},if>
    56. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},if>
    57. <if test="remark != null">remark = #{remark},if>
    58. update_time = sysdate()
    59. set>
    60. where user_id = #{userId}
    61. update>
    62. <select id="selectDeptCount" parameterType="Dept" resultType="int">
    63. select count(1) from sys_dept
    64. where del_flag = '0'
    65. <if test="deptId != null and deptId != 0"> and dept_id = #{deptId} if>
    66. <if test="parentId != null and parentId != 0"> and parent_id = #{parentId} if>
    67. select>
    68. <delete id="deleteUserRole" parameterType="Long">
    69. delete from sys_user_role where user_id in
    70. <foreach collection="array" item="userId" open="(" separator="," close=")">
    71. #{userId}
    72. foreach>
    73. delete>
    74. mapper>

    创建好的截图如下所示:

     最后点击Apply和OK就可以了。

    当我们在包里面New一个文件的时候,可选框中会出现这个名字的选项的。图标是对应XML文件的图标。我们只需要填写文件名即可。

  • 相关阅读:
    2022-08-13 达梦创建实例与用户密码过短
    使用js开发一个快速打开前端项目的alfred插件
    Linux常用命令、tomcat常用命令
    C++实现观察者模式(包含源码)
    【DVWA】19. Insecure CAPTCHA 不安全的验证码(全等级)
    【微服务】spring webflux响应式编程使用详解
    数据库设计三范式
    2023年11月咸阳市《网络建设与运维》赛题解析
    Yapi 1.10.3迁移踩坑记
    代码随想录:二叉树11-12
  • 原文地址:https://blog.csdn.net/weixin_46281472/article/details/126035324